CB-8469 Create gradle build files as part of create script

Makes project imporatable by Android Studio before first build
This commit is contained in:
Andrew Grieve 2015-02-12 16:15:43 -05:00
parent 9baa27508a
commit db18e1480e
2 changed files with 93 additions and 65 deletions

View File

@ -115,6 +115,11 @@ function writeProjectProperties(projectPath, target_api) {
fs.writeFileSync(dstPath, data); fs.writeFileSync(dstPath, data);
} }
function prepBuildFiles(projectPath) {
var buildModule = require(path.join(path.resolve(projectPath), 'cordova', 'lib', 'build'));
buildModule.prepBuildFiles();
}
function copyBuildRules(projectPath) { function copyBuildRules(projectPath) {
var srcDir = path.join(ROOT, 'bin', 'templates', 'project'); var srcDir = path.join(ROOT, 'bin', 'templates', 'project');
@ -273,6 +278,7 @@ exports.createProject = function(project_path, package_name, project_name, proje
}); });
// Link it to local android install. // Link it to local android install.
writeProjectProperties(project_path, target_api); writeProjectProperties(project_path, target_api);
prepBuildFiles(project_path);
console.log(generateDoneMessage('create', use_shared_project)); console.log(generateDoneMessage('create', use_shared_project));
}); });
}; };
@ -313,6 +319,7 @@ exports.updateProject = function(projectPath, shared) {
copyBuildRules(projectPath); copyBuildRules(projectPath);
removeDebuggableFromManifest(projectPath); removeDebuggableFromManifest(projectPath);
writeProjectProperties(projectPath, target_api); writeProjectProperties(projectPath, target_api);
prepBuildFiles(project_path);
console.log(generateDoneMessage('update', shared)); console.log(generateDoneMessage('update', shared));
}); });
}; };

View File

@ -209,33 +209,9 @@ var builders = {
return args; return args;
}, },
prepEnv: function() { // Makes the project buildable, minus the gradle wrapper.
return check_reqs.check_gradle() prepBuildFiles: function() {
.then(function() {
// Copy the gradle wrapper on each build so that:
// A) we don't require the Android SDK at project creation time, and
// B) we always use the SDK's latest version of it.
var projectPath = ROOT; var projectPath = ROOT;
// check_reqs ensures that this is set.
var sdkDir = process.env['ANDROID_HOME'];
var wrapperDir = path.join(sdkDir, 'tools', 'templates', 'gradle', 'wrapper');
if (process.platform == 'win32') {
shell.cp('-f', path.join(wrapperDir, 'gradlew.bat'), projectPath);
} else {
shell.cp('-f', path.join(wrapperDir, 'gradlew'), projectPath);
}
shell.rm('-rf', path.join(projectPath, 'gradle', 'wrapper'));
shell.mkdir('-p', path.join(projectPath, 'gradle'));
shell.cp('-r', path.join(wrapperDir, 'gradle', 'wrapper'), path.join(projectPath, '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.
var distributionUrlRegex = /distributionUrl.*zip/;
var distributionUrl = 'distributionUrl=http\\://services.gradle.org/distributions/gradle-2.2.1-all.zip';
var gradleWrapperPropertiesPath = path.join(projectPath, 'gradle', 'wrapper', 'gradle-wrapper.properties');
shell.sed('-i', distributionUrlRegex, distributionUrl, gradleWrapperPropertiesPath);
// Update the version of build.gradle in each dependent library. // Update the version of build.gradle in each dependent library.
var pluginBuildGradle = path.join(projectPath, 'cordova', 'lib', 'plugin-build.gradle'); var pluginBuildGradle = path.join(projectPath, 'cordova', 'lib', 'plugin-build.gradle');
var propertiesObj = readProjectProperties(); var propertiesObj = readProjectProperties();
@ -290,6 +266,37 @@ var builders = {
}); });
buildGradle = buildGradle.replace(/(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/, '$1\n' + includeList + '$2'); buildGradle = buildGradle.replace(/(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/, '$1\n' + includeList + '$2');
fs.writeFileSync(path.join(projectPath, 'build.gradle'), buildGradle); fs.writeFileSync(path.join(projectPath, 'build.gradle'), buildGradle);
},
prepEnv: function() {
var self = this;
return check_reqs.check_gradle()
.then(function() {
return self.prepBuildFiles();
}).then(function() {
// Copy the gradle wrapper on each build so that:
// A) we don't require the Android SDK at project creation time, and
// B) we always use the SDK's latest version of it.
var projectPath = ROOT;
// check_reqs ensures that this is set.
var sdkDir = process.env['ANDROID_HOME'];
var wrapperDir = path.join(sdkDir, 'tools', 'templates', 'gradle', 'wrapper');
if (process.platform == 'win32') {
shell.cp('-f', path.join(wrapperDir, 'gradlew.bat'), projectPath);
} else {
shell.cp('-f', path.join(wrapperDir, 'gradlew'), projectPath);
}
shell.rm('-rf', path.join(projectPath, 'gradle', 'wrapper'));
shell.mkdir('-p', path.join(projectPath, 'gradle'));
shell.cp('-r', path.join(wrapperDir, 'gradle', 'wrapper'), path.join(projectPath, '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.
var distributionUrlRegex = /distributionUrl.*zip/;
var distributionUrl = 'distributionUrl=http\\://services.gradle.org/distributions/gradle-2.2.1-all.zip';
var gradleWrapperPropertiesPath = path.join(projectPath, 'gradle', 'wrapper', 'gradle-wrapper.properties');
shell.sed('-i', distributionUrlRegex, distributionUrl, gradleWrapperPropertiesPath);
}); });
}, },
@ -380,6 +387,8 @@ function parseOpts(options, resolvedTarget) {
// Don't need to do anything special to when building for device vs emulator. // Don't need to do anything special to when building for device vs emulator.
// iOS uses this flag to switch on architecture. // iOS uses this flag to switch on architecture.
break; break;
case 'prepenv' :
ret.prepEnv = true;
case 'nobuild' : case 'nobuild' :
ret.buildMethod = 'none'; ret.buildMethod = 'none';
break; break;
@ -429,8 +438,12 @@ module.exports.run = function(options, optResolvedTarget) {
var builder = builders[opts.buildMethod]; var builder = builders[opts.buildMethod];
return builder.prepEnv() return builder.prepEnv()
.then(function() { .then(function() {
return builder.build(opts.buildType, opts.arch, opts.extraArgs); if (opts.prepEnv) {
}).then(function() { console.log('Build file successfully prepared.');
return;
}
return builder.build(opts.buildType, opts.arch, opts.extraArgs)
.then(function() {
var apkPaths = builder.findOutputApks(opts.buildType, opts.arch); var apkPaths = builder.findOutputApks(opts.buildType, opts.arch);
console.log('Built the following apk(s):'); console.log('Built the following apk(s):');
console.log(' ' + apkPaths.join('\n ')); console.log(' ' + apkPaths.join('\n '));
@ -440,6 +453,13 @@ module.exports.run = function(options, optResolvedTarget) {
buildMethod: opts.buildMethod buildMethod: opts.buildMethod
}; };
}); });
});
};
// Called by plugman after installing plugins, and by create script after creating project.
module.exports.prepBuildFiles = function() {
var builder = builders['gradle'];
return builder.prepBuildFiles();
}; };
/* /*
@ -517,6 +537,7 @@ module.exports.help = function() {
console.log(' \'--ant\': will build project with ant'); console.log(' \'--ant\': will build project with ant');
console.log(' \'--gradle\': will build project with gradle (default)'); console.log(' \'--gradle\': will build project with gradle (default)');
console.log(' \'--nobuild\': will skip build process (useful when using run command)'); console.log(' \'--nobuild\': will skip build process (useful when using run command)');
console.log(' \'--prepenv\': don\'t build, but copy in build scripts where necessary');
console.log(' \'--versionCode=#\': Override versionCode for this build. Useful for uploading multiple APKs. Requires --gradle.'); console.log(' \'--versionCode=#\': Override versionCode for this build. Useful for uploading multiple APKs. Requires --gradle.');
console.log(' \'--minSdkVersion=#\': Override minSdkVersion for this build. Useful for uploading multiple APKs. Requires --gradle.'); console.log(' \'--minSdkVersion=#\': Override minSdkVersion for this build. Useful for uploading multiple APKs. Requires --gradle.');
console.log(' \'--gradleArg=<gradle command line arg>\': Extra args to pass to the gradle command. Use one flag per arg. Ex. --gradleArg=-PcdvBuildMultipleApks=true'); console.log(' \'--gradleArg=<gradle command line arg>\': Extra args to pass to the gradle command. Use one flag per arg. Ex. --gradleArg=-PcdvBuildMultipleApks=true');