Refactor ProjectBuilder to use class instead of prototype

This commit is contained in:
Gearoid M 2018-06-29 09:16:57 +09:00
parent 8ee3a73dd1
commit 350d35fb24

View File

@ -34,15 +34,16 @@ const TEMPLATE =
'# This file is automatically generated.\n' + '# This file is automatically generated.\n' +
'# Do not modify this file -- ' + MARKER + '\n'; '# Do not modify this file -- ' + MARKER + '\n';
function ProjectBuilder (projectRoot) { class ProjectBuilder {
constructor (projectRoot) {
this.root = projectRoot || path.resolve(__dirname, '../../..'); this.root = projectRoot || path.resolve(__dirname, '../../..');
this.binDirs = { this.binDirs = {
studio: path.join(this.root, 'app', 'build', 'outputs', 'apk'), studio: path.join(this.root, 'app', 'build', 'outputs', 'apk'),
gradle: path.join(this.root, 'app', 'build', 'outputs', 'apk') gradle: path.join(this.root, 'app', 'build', 'outputs', 'apk')
}; };
} }
ProjectBuilder.prototype.getArgs = function (cmd, opts) { getArgs (cmd, opts) {
if (cmd === 'release') { if (cmd === 'release') {
cmd = 'cdvBuildRelease'; cmd = 'cdvBuildRelease';
} else if (cmd === 'debug') { } else if (cmd === 'debug') {
@ -63,13 +64,12 @@ ProjectBuilder.prototype.getArgs = function (cmd, opts) {
// Shaves another 100ms, but produces a "try at own risk" warning. Not worth it (yet): // Shaves another 100ms, but produces a "try at own risk" warning. Not worth it (yet):
// args.push('-Dorg.gradle.parallel=true'); // args.push('-Dorg.gradle.parallel=true');
return args; return args;
}; }
/* /*
* This returns a promise * This returns a promise
*/ */
runGradleWrapper (gradle_cmd) {
ProjectBuilder.prototype.runGradleWrapper = function (gradle_cmd) {
var gradlePath = path.join(this.root, 'gradlew'); var gradlePath = path.join(this.root, 'gradlew');
var wrapperGradle = path.join(this.root, 'wrapper.gradle'); var wrapperGradle = path.join(this.root, 'wrapper.gradle');
if (fs.existsSync(gradlePath)) { if (fs.existsSync(gradlePath)) {
@ -77,9 +77,9 @@ ProjectBuilder.prototype.runGradleWrapper = function (gradle_cmd) {
} else { } else {
return spawn(gradle_cmd, ['-p', this.root, 'wrapper', '-b', wrapperGradle], { stdio: 'inherit' }); return spawn(gradle_cmd, ['-p', this.root, 'wrapper', '-b', wrapperGradle], { stdio: 'inherit' });
} }
}; }
ProjectBuilder.prototype.readProjectProperties = function () { readProjectProperties () {
function findAllUniq (data, r) { function findAllUniq (data, r) {
var s = {}; var s = {};
var m; var m;
@ -95,9 +95,9 @@ ProjectBuilder.prototype.readProjectProperties = function () {
gradleIncludes: findAllUniq(data, /^\s*cordova\.gradle\.include\.\d+=(.*)(?:\s|$)/mg), gradleIncludes: findAllUniq(data, /^\s*cordova\.gradle\.include\.\d+=(.*)(?:\s|$)/mg),
systemLibs: findAllUniq(data, /^\s*cordova\.system\.library\.\d+=(.*)(?:\s|$)/mg) systemLibs: findAllUniq(data, /^\s*cordova\.system\.library\.\d+=(.*)(?:\s|$)/mg)
}; };
}; }
ProjectBuilder.prototype.extractRealProjectNameFromManifest = function () { extractRealProjectNameFromManifest () {
var manifestPath = path.join(this.root, 'app', 'src', 'main', 'AndroidManifest.xml'); var manifestPath = path.join(this.root, 'app', 'src', 'main', 'AndroidManifest.xml');
var manifestData = fs.readFileSync(manifestPath, 'utf8'); var manifestData = fs.readFileSync(manifestPath, 'utf8');
var m = /<manifest[\s\S]*?package\s*=\s*"(.*?)"/i.exec(manifestData); var m = /<manifest[\s\S]*?package\s*=\s*"(.*?)"/i.exec(manifestData);
@ -108,10 +108,10 @@ ProjectBuilder.prototype.extractRealProjectNameFromManifest = function () {
var packageName = m[1]; var packageName = m[1];
var lastDotIndex = packageName.lastIndexOf('.'); var lastDotIndex = packageName.lastIndexOf('.');
return packageName.substring(lastDotIndex + 1); return packageName.substring(lastDotIndex + 1);
}; }
// Makes the project buildable, minus the gradle wrapper. // Makes the project buildable, minus the gradle wrapper.
ProjectBuilder.prototype.prepBuildFiles = function () { prepBuildFiles () {
// Update the version of build.gradle in each dependent library. // Update the version of build.gradle in each dependent library.
var pluginBuildGradle = path.join(this.root, 'cordova', 'lib', 'plugin-build.gradle'); var pluginBuildGradle = path.join(this.root, 'cordova', 'lib', 'plugin-build.gradle');
var propertiesObj = this.readProjectProperties(); var propertiesObj = this.readProjectProperties();
@ -208,9 +208,9 @@ ProjectBuilder.prototype.prepBuildFiles = function () {
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');
// This needs to be stored in the app gradle, not the root grade // This needs to be stored in the app gradle, not the root grade
fs.writeFileSync(path.join(this.root, 'app', 'build.gradle'), buildGradle); fs.writeFileSync(path.join(this.root, 'app', 'build.gradle'), buildGradle);
}; }
ProjectBuilder.prototype.prepEnv = function (opts) { prepEnv (opts) {
var self = this; var self = this;
return check_reqs.check_gradle() return check_reqs.check_gradle()
.then(function (gradlePath) { .then(function (gradlePath) {
@ -235,13 +235,13 @@ ProjectBuilder.prototype.prepEnv = function (opts) {
shell.rm('-f', propertiesFilePath); shell.rm('-f', propertiesFilePath);
} }
}); });
}; }
/* /*
* Builds the project with gradle. * Builds the project with gradle.
* Returns a promise. * Returns a promise.
*/ */
ProjectBuilder.prototype.build = function (opts) { build (opts) {
var wrapper = path.join(this.root, 'gradlew'); var wrapper = path.join(this.root, 'gradlew');
var args = this.getArgs(opts.buildType === 'debug' ? 'debug' : 'release', opts); var args = this.getArgs(opts.buildType === 'debug' ? 'debug' : 'release', opts);
@ -273,9 +273,9 @@ ProjectBuilder.prototype.build = function (opts) {
} }
return Q.reject(error); return Q.reject(error);
}); });
}; }
ProjectBuilder.prototype.clean = function (opts) { clean (opts) {
var builder = this; var builder = this;
var wrapper = path.join(this.root, 'gradlew'); var wrapper = path.join(this.root, 'gradlew');
var args = builder.getArgs('clean', opts); var args = builder.getArgs('clean', opts);
@ -292,15 +292,16 @@ ProjectBuilder.prototype.clean = function (opts) {
} }
}); });
}); });
}; }
ProjectBuilder.prototype.findOutputApks = function (build_type, arch) { findOutputApks (build_type, arch) {
var self = this; var self = this;
return Object.keys(this.binDirs).reduce(function (result, builderName) { return Object.keys(this.binDirs).reduce(function (result, builderName) {
var binDir = self.binDirs[builderName]; var binDir = self.binDirs[builderName];
return result.concat(findOutputApksHelper(binDir, build_type, builderName === 'ant' ? null : arch)); return result.concat(findOutputApksHelper(binDir, build_type, builderName === 'ant' ? null : arch));
}, []).sort(apkSorter); }, []).sort(apkSorter);
}; }
}
module.exports = ProjectBuilder; module.exports = ProjectBuilder;