mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
CB-3445 Add gradle support clean command (plus some code cleanup)
* Don't run ant clean for gradle ever * Don't set sdk.dir since ANDROID_HOME is not always set * Don't export builders
This commit is contained in:
parent
c91b272648
commit
d56ea25816
@ -33,7 +33,7 @@ if(args[2] == '--help' ||
|
|||||||
build.help();
|
build.help();
|
||||||
} else {
|
} else {
|
||||||
reqs.run().done(function() {
|
reqs.run().done(function() {
|
||||||
return build.run.call(build.run, args.slice(2));
|
return build.run(args.slice(2));
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(2);
|
process.exit(2);
|
||||||
|
@ -19,18 +19,26 @@
|
|||||||
under the License.
|
under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var clean = require('./lib/clean'),
|
var build = require('./lib/build'),
|
||||||
reqs = require('./lib/check_reqs'),
|
reqs = require('./lib/check_reqs'),
|
||||||
args = process.argv;
|
args = process.argv;
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
// Usage support for when args are given
|
// Support basic help commands
|
||||||
if(args.length > 2) {
|
if(args[2] == '--help' ||
|
||||||
clean.help();
|
args[2] == '/?' ||
|
||||||
|
args[2] == '-h' ||
|
||||||
|
args[2] == 'help' ||
|
||||||
|
args[2] == '-help' ||
|
||||||
|
args[2] == '/help') {
|
||||||
|
console.log('Usage: ' + path.relative(process.cwd(), process.argv[1]));
|
||||||
|
console.log('Cleans the project directory.');
|
||||||
|
process.exit(0);
|
||||||
} else {
|
} else {
|
||||||
reqs.run().done(function() {
|
reqs.run().done(function() {
|
||||||
return clean.run();
|
return build.runClean(args.slice(2));
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
console.error('ERROR: ' + err);
|
console.error(err);
|
||||||
process.exit(2);
|
process.exit(2);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
137
bin/templates/cordova/lib/build.js
vendored
137
bin/templates/cordova/lib/build.js
vendored
@ -69,7 +69,7 @@ function copyGradleWrapper() {
|
|||||||
shell.cp('-r', path.join(wrapperDir, 'gradle', 'wrapper'), path.join(projectPath, 'gradle'));
|
shell.cp('-r', path.join(wrapperDir, 'gradle', 'wrapper'), path.join(projectPath, 'gradle'));
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.builders = {
|
var builders = {
|
||||||
ant: {
|
ant: {
|
||||||
getArgs: function(cmd) {
|
getArgs: function(cmd) {
|
||||||
var args = [cmd, '-f', path.join(ROOT, 'build.xml')];
|
var args = [cmd, '-f', path.join(ROOT, 'build.xml')];
|
||||||
@ -77,29 +77,45 @@ module.exports.builders = {
|
|||||||
if (hasCustomRules()) {
|
if (hasCustomRules()) {
|
||||||
args.push('-Dout.dir=ant-build', '-Dgen.absolute.dir=ant-gen');
|
args.push('-Dout.dir=ant-build', '-Dgen.absolute.dir=ant-gen');
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
// Specify sdk dir in case local properties are missing
|
|
||||||
args.push('-Dsdk.dir='+path.join(which.sync('android'), '../..'));
|
|
||||||
} catch(e) {
|
|
||||||
// Can't find android; don't push arg: assume all is okay
|
|
||||||
}
|
|
||||||
return args;
|
return args;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
prepEnv: function() {
|
||||||
|
return check_reqs.check_ant()
|
||||||
|
.then(function() {
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Builds the project with ant.
|
* Builds the project with ant.
|
||||||
* Returns a promise.
|
* Returns a promise.
|
||||||
*/
|
*/
|
||||||
build: function(build_type) {
|
build: function(build_type) {
|
||||||
|
// Without our custom_rules.xml, we need to clean before building.
|
||||||
|
var ret = Q();
|
||||||
|
if (!hasCustomRules()) {
|
||||||
|
// clean will call check_ant() for us.
|
||||||
|
ret = this.clean();
|
||||||
|
}
|
||||||
|
|
||||||
var builder = this;
|
var builder = this;
|
||||||
var args = builder.getArgs(build_type == "debug" ? 'debug' : 'release');
|
var args = this.getArgs(build_type == 'debug' ? 'debug' : 'release');
|
||||||
return Q().then(function() {
|
return check_reqs.check_ant()
|
||||||
|
.then(function() {
|
||||||
return spawn('ant', args);
|
return spawn('ant', args);
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
return builder.getOutputFiles();
|
return builder.getOutputFiles();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
clean: function() {
|
||||||
|
var args = this.getArgs('clean');
|
||||||
|
return check_reqs.check_ant()
|
||||||
|
.then(function() {
|
||||||
|
return spawn('ant', args);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// Find the recently-generated output APK files
|
// Find the recently-generated output APK files
|
||||||
// Ant only generates one output file; return it.
|
// Ant only generates one output file; return it.
|
||||||
getOutputFiles: function() {
|
getOutputFiles: function() {
|
||||||
@ -139,6 +155,10 @@ module.exports.builders = {
|
|||||||
return args;
|
return args;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
prepEnv: function() {
|
||||||
|
return Q();
|
||||||
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Builds the project with gradle.
|
* Builds the project with gradle.
|
||||||
* Returns a promise.
|
* Returns a promise.
|
||||||
@ -155,6 +175,18 @@ module.exports.builders = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
clean: function() {
|
||||||
|
var builder = this;
|
||||||
|
var wrapper = path.join(ROOT, 'gradlew');
|
||||||
|
var args = builder.getArgs('clean');
|
||||||
|
copyGradleWrapper();
|
||||||
|
return Q().then(function() {
|
||||||
|
return spawn(wrapper, args);
|
||||||
|
}).then(function() {
|
||||||
|
return builder.getOutputFiles(build_type);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// Find the recently-generated output APK files
|
// Find the recently-generated output APK files
|
||||||
// Gradle can generate multiple output files; return all of them.
|
// Gradle can generate multiple output files; return all of them.
|
||||||
getOutputFiles: function(build_type) {
|
getOutputFiles: function(build_type) {
|
||||||
@ -171,18 +203,31 @@ module.exports.builders = {
|
|||||||
});
|
});
|
||||||
return candidates;
|
return candidates;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
none: {
|
||||||
|
prepEnv: function() {
|
||||||
|
return Q();
|
||||||
|
},
|
||||||
|
build: function() {
|
||||||
|
console.log('Skipping build...');
|
||||||
|
return Q();
|
||||||
|
},
|
||||||
|
clean: function() {
|
||||||
|
return Q();
|
||||||
|
},
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
function parseOpts(options) {
|
||||||
* Builds the project with the specifed options
|
|
||||||
* Returns a promise.
|
|
||||||
*/
|
|
||||||
module.exports.run = function(options) {
|
|
||||||
|
|
||||||
// Backwards-compatibility: Allow a single string argument
|
// Backwards-compatibility: Allow a single string argument
|
||||||
if (typeof options == "string") options = [options];
|
if (typeof options == "string") options = [options];
|
||||||
|
|
||||||
|
var ret = {
|
||||||
|
buildType: 'debug',
|
||||||
|
buildMethod: process.env['ANDROID_BUILD'] || 'ant'
|
||||||
|
};
|
||||||
|
|
||||||
// Iterate through command line options
|
// Iterate through command line options
|
||||||
for (var i=0; options && (i < options.length); ++i) {
|
for (var i=0; options && (i < options.length); ++i) {
|
||||||
if (options[i].substring && options[i].substring(0,2) == "--") {
|
if (options[i].substring && options[i].substring(0,2) == "--") {
|
||||||
@ -190,21 +235,15 @@ module.exports.run = function(options) {
|
|||||||
switch(option) {
|
switch(option) {
|
||||||
case 'debug':
|
case 'debug':
|
||||||
case 'release':
|
case 'release':
|
||||||
if (build_type) {
|
ret.buildType = option;
|
||||||
return Q.reject('Multiple build types (' + build_type + ' and ' + option + ') specified.');
|
|
||||||
}
|
|
||||||
build_type = option;
|
|
||||||
break;
|
break;
|
||||||
case 'ant':
|
case 'ant':
|
||||||
case 'gradle':
|
case 'gradle':
|
||||||
if (build_method) {
|
ret.buildMethod = option;
|
||||||
return Q.reject('Multiple build methods (' + build_method + ' and ' + option + ') specified.');
|
|
||||||
}
|
|
||||||
build_method = option;
|
|
||||||
break;
|
break;
|
||||||
case 'nobuild' :
|
case 'nobuild' :
|
||||||
console.log('Skipping build...');
|
ret.buildMethod = 'none';
|
||||||
return Q();
|
break;
|
||||||
default :
|
default :
|
||||||
return Q.reject('Build option \'' + options[i] + '\' not recognized.');
|
return Q.reject('Build option \'' + options[i] + '\' not recognized.');
|
||||||
}
|
}
|
||||||
@ -212,34 +251,36 @@ module.exports.run = function(options) {
|
|||||||
return Q.reject('Build option \'' + options[i] + '\' not recognized.');
|
return Q.reject('Build option \'' + options[i] + '\' not recognized.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Defaults
|
return ret;
|
||||||
build_type = build_type || "debug";
|
}
|
||||||
build_method = build_method || process.env.ANDROID_BUILD || "ant";
|
|
||||||
|
|
||||||
// Get the builder
|
/*
|
||||||
var builder = module.exports.builders[build_method];
|
* Builds the project with the specifed options
|
||||||
|
* Returns a promise.
|
||||||
|
*/
|
||||||
|
module.exports.runClean = function(options) {
|
||||||
|
var opts = parseOpts(options);
|
||||||
|
var builder = builders[opts.buildMethod];
|
||||||
|
return builder.prepEnv(opts.buildType)
|
||||||
|
.then(function() {
|
||||||
|
return builder.clean();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Without our custom_rules.xml, we need to clean before building.
|
/*
|
||||||
var ret;
|
* Builds the project with the specifed options
|
||||||
if (!hasCustomRules()) {
|
* Returns a promise.
|
||||||
// clean will call check_ant() for us.
|
*/
|
||||||
ret = require('./clean').run();
|
module.exports.run = function(options) {
|
||||||
} else {
|
var opts = parseOpts(options);
|
||||||
ret = check_reqs.check_ant();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a promise for the actual build
|
var builder = builders[opts.buildMethod];
|
||||||
return ret.then(function() {
|
return builder.prepEnv(opts.buildType)
|
||||||
return builder.build.call(builder, build_type);
|
.then(function() {
|
||||||
|
return builder.build(opts.buildType);
|
||||||
}).then(function(apkFiles) {
|
}).then(function(apkFiles) {
|
||||||
var outputDir = path.join(ROOT, 'out');
|
var outputDir = path.join(ROOT, 'out');
|
||||||
try {
|
shell.mkdir('-p', outputDir);
|
||||||
fs.mkdirSync(outputDir);
|
|
||||||
} catch (e) {
|
|
||||||
if (e.code != "EEXIST") {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (var i=0; i < apkFiles.length; ++i) {
|
for (var i=0; i < apkFiles.length; ++i) {
|
||||||
shell.cp('-f', apkFiles[i], path.join(outputDir, path.basename(apkFiles[i])));
|
shell.cp('-f', apkFiles[i], path.join(outputDir, path.basename(apkFiles[i])));
|
||||||
}
|
}
|
||||||
|
43
bin/templates/cordova/lib/clean.js
vendored
43
bin/templates/cordova/lib/clean.js
vendored
@ -1,43 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
|
|
||||||
/*
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
|
||||||
or more contributor license agreements. See the NOTICE file
|
|
||||||
distributed with this work for additional information
|
|
||||||
regarding copyright ownership. The ASF licenses this file
|
|
||||||
to you under the Apache License, Version 2.0 (the
|
|
||||||
"License"); you may not use this file except in compliance
|
|
||||||
with the License. You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing,
|
|
||||||
software distributed under the License is distributed on an
|
|
||||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
KIND, either express or implied. See the License for the
|
|
||||||
specific language governing permissions and limitations
|
|
||||||
under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var build = require('./build'),
|
|
||||||
spawn = require('./spawn'),
|
|
||||||
path = require('path');
|
|
||||||
var check_reqs = require('./check_reqs');
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Cleans the project using ant
|
|
||||||
* Returns a promise.
|
|
||||||
*/
|
|
||||||
module.exports.run = function() {
|
|
||||||
var args = build.getAntArgs('clean');
|
|
||||||
return check_reqs.check_ant()
|
|
||||||
.then(function() {
|
|
||||||
return spawn('ant', args);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.help = function() {
|
|
||||||
console.log('Usage: ' + path.relative(process.cwd(), process.argv[1]));
|
|
||||||
console.log('Cleans the project directory.');
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user