CB-12524: This now fetches the template from  inside of the Android Studio 
directory, and falls back to a locally installed Gradle instance


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/3cc4e5b4
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/3cc4e5b4
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/3cc4e5b4

Branch: refs/heads/master
Commit: 3cc4e5b4405c7604d4c759cf8901f8ae6921e49a
Parents: b2664bc
Author: Joe Bowser <bows...@apache.org>
Authored: Wed Mar 8 18:09:49 2017 -0800
Committer: Joe Bowser <bows...@apache.org>
Committed: Mon Mar 13 13:51:36 2017 -0700

----------------------------------------------------------------------
 bin/lib/check_reqs.js                           | 38 ++++++++++++++++----
 .../cordova/lib/builders/GradleBuilder.js       | 22 +++++++++---
 2 files changed, 50 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/3cc4e5b4/bin/lib/check_reqs.js
----------------------------------------------------------------------
diff --git a/bin/lib/check_reqs.js b/bin/lib/check_reqs.js
index ac6fa4c..6b3a630 100644
--- a/bin/lib/check_reqs.js
+++ b/bin/lib/check_reqs.js
@@ -26,6 +26,7 @@ var shelljs = require('shelljs'),
     Q     = require('q'),
     path  = require('path'),
     fs    = require('fs'),
+    os    = require('os'),
     ROOT  = path.join(__dirname, '..', '..');
 var CordovaError = require('cordova-common').CordovaError;
 
@@ -78,21 +79,46 @@ module.exports.check_ant = function() {
     });
 };
 
+module.exports.get_gradle_wrapper = function() {
+    var androidStudioPath;
+    if(os.platform() == "darwin") {
+      androidStudioPath = path.join('/Applications', 'Android Studio.app', 
'Contents', 'gradle');
+    }
+
+    if(androidStudioPath != null && fs.existsSync(androidStudioPath)) {
+      var dirs = fs.readdirSync(androidStudioPath);
+      if(dirs[0].split('-')[0] == "gradle")
+      {
+        //Sweet, we found the path, let's return it.
+        var gradle_cmd = os.platform() == "win32" ? "gradle.bat" : "gradle";
+        return path.join(androidStudioPath, dirs[0], "bin", gradle_cmd);
+      }
+    } else {
+      //OK, let's try to check for Gradle!
+      return forgivingWhichSync('gradle');
+    }
+};
+
 // Returns a promise. Called only by build and clean commands.
 module.exports.check_gradle = function() {
     var sdkDir = process.env['ANDROID_HOME'];
+    var d = Q.defer();
     if (!sdkDir)
         return Q.reject(new CordovaError('Could not find gradle wrapper within 
Android SDK. Could not find Android SDK directory.\n' +
             'Might need to install Android SDK or set up \'ANDROID_HOME\' env 
variable.'));
 
-    var wrapperDir = path.join(sdkDir, 'tools', 'templates', 'gradle', 
'wrapper');
-    if (!fs.existsSync(wrapperDir)) {
-        return Q.reject(new CordovaError('Could not find gradle wrapper within 
Android SDK. Might need to update your Android SDK.\n' +
-            'Looked here: ' + wrapperDir));
-    }
-    return Q.when();
+    var path = this.get_gradle_wrapper();
+    console.log(path);
+    if(path != '')
+      d.resolve(path);
+    else
+      d.reject(new CordovaError('Could not find an installed version of Gradle 
either in Android Studio,\n' +
+                                'or on your system to install the gradle 
wrapper. Please include gradle \n' +
+                                'in your path, or install Android Studio'));
+    return d.promise;
 };
 
+
 // Returns a promise.
 module.exports.check_java = function() {
     var javacPath = forgivingWhichSync('javac');

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/3cc4e5b4/bin/templates/cordova/lib/builders/GradleBuilder.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/lib/builders/GradleBuilder.js 
b/bin/templates/cordova/lib/builders/GradleBuilder.js
index 403082b..2404777 100644
--- a/bin/templates/cordova/lib/builders/GradleBuilder.js
+++ b/bin/templates/cordova/lib/builders/GradleBuilder.js
@@ -65,6 +65,16 @@ GradleBuilder.prototype.getArgs = function(cmd, opts) {
     return args;
 };
 
+/*
+ * This returns a promise
+ */
+
+GradleBuilder.prototype.runGradleWrapper = function(gradle_cmd) {
+  if(!fs.existsSync(this.root, 'gradle'))
+    return spawn(gradle_cmd, ["wrapper"], {stdio: 'inherit'});
+}
+
+
 // Makes the project buildable, minus the gradle wrapper.
 GradleBuilder.prototype.prepBuildFiles = function() {
     // Update the version of build.gradle in each dependent library.
@@ -154,15 +164,19 @@ GradleBuilder.prototype.prepBuildFiles = function() {
     });
     buildGradle = buildGradle.replace(/(PLUGIN GRADLE EXTENSIONS 
START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/, '$1\n' + includeList + '$2');
     fs.writeFileSync(path.join(this.root, 'build.gradle'), buildGradle);
-    //Q sucks!!
-    return Q.when();
 };
 
 GradleBuilder.prototype.prepEnv = function(opts) {
     var self = this;
-    return self.prepBuildFiles().then(function() {
+    return check_reqs.check_gradle()
+      .then(function(gradlePath) {
+        return self.runGradleWrapper(gradlePath);
+      }).then(function() {
+          return self.prepBuildFiles();
+      }).then(function() {
         // We now copy the gradle out of the framework
         // This is a dirty patch to get the build working
+        /*
         var wrapperDir = path.join(self.root, 'CordovaLib');
         if (process.platform == 'win32') {
             shell.rm('-f', path.join(self.root, 'gradlew.bat'));
@@ -174,7 +188,7 @@ GradleBuilder.prototype.prepEnv = function(opts) {
         shell.rm('-rf', path.join(self.root, 'gradle', 'wrapper'));
         shell.mkdir('-p', path.join(self.root, 'gradle'));
         shell.cp('-r', path.join(wrapperDir, 'gradle', 'wrapper'), 
path.join(self.root, 'gradle'));
-
+*/
         // If the gradle distribution URL is set, make sure it points to 
version we want.
         // If it's not set, do nothing, assuming that we're using a future 
version of gradle that we don't want to mess with.
         // For some reason, using ^ and $ don't work.  This does the job, 
though.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to