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

This commit is contained in:
Joe Bowser 2017-03-08 18:09:49 -08:00
parent b2664bc469
commit 3cc4e5b440
2 changed files with 50 additions and 10 deletions

View File

@ -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');

View File

@ -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.