#! /usr/bin/env node
var process = require('process');
var fs = require('fs-extra');
var argv = require('minimist')(process.argv.slice(2));
var path = require('path');
var tmp = require('tmp');
var ncp = require('ncp');
var spawn = require('child_process').spawn;

var currentWorkingDirectory = process.cwd();
var filesToCopy = argv._;
var apkOutputFile = argv.o || path.join(currentWorkingDirectory, 'app.apk');

var positronRoot = path.join(__dirname, '..');

tmp.dir({keep: true}, function (err, tempPath, cleanup) {
  if (err) throw err;

  ncp(path.join(positronRoot, 'boilerplate'), tempPath, function (err) {
    if (err) throw err;

    filesToCopy.forEach(function (fileName) {
      fs.copySync(
        path.join(currentWorkingDirectory, fileName),
        path.join(tempPath, 'app', 'src', 'main', 'assets', fileName)
      );
    });

    var gradlew = spawn(path.join(tempPath, 'gradlew'), ['build'], {cwd: tempPath});

    console.log('Compiling apk...');

    gradlew.stdout.on('data', function (data) {
      console.log(data.toString());
    });

    gradlew.stderr.on('data', function (data) {
      console.log(data.toString());
    });

    gradlew.on('close', function () {
      fs.copySync(
        path.join(tempPath, 'app', 'build', 'outputs', 'apk', 'app-debug.apk'),
        apkOutputFile
      );

      console.log('Copied built apk to', apkOutputFile);
    });
  });
});

@@ -455,16 +455,320 @@
 '..');%0A%0A
+var androidHome = process.env.ANDROID_HOME;%0A%0Aif (!androidHome) %7B%0A  if (process.platform === 'darwin') %7B%0A    androidHome = path.join(process.env.HOME, 'Library', 'Android', 'sdk');%0A  %7D%0A%0A  if (!androidHome) %7B%0A    throw new Error('Please set ANDROID_HOME env var to the location of Android Studio');%0A  %7D%0A%7D%0A%0A
 tmp.dir(
@@ -1141,24 +1141,100 @@
 );%0A    %7D);%0A%0A
+    var env = Object.assign(%7B%7D, process.env, %7BANDROID_HOME: androidHome%7D);%0A%0A
     var grad
@@ -1302,16 +1302,26 @@
 tempPath
+, env: env
 %7D);%0A%0A   

Configure ANDROID_HOME on osx if not set