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