mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
refactor: save ProjectBuilder instance in Api instance (#1016)
This reduces dependence on the `builders` module and reduces repitition. This also facilitates another WIP refactoring I am working on.
This commit is contained in:
parent
d86cb99dd5
commit
ba5781c3bf
6
bin/templates/cordova/Api.js
vendored
6
bin/templates/cordova/Api.js
vendored
@ -85,6 +85,8 @@ class Api {
|
|||||||
build: path.join(this.root, 'build'),
|
build: path.join(this.root, 'build'),
|
||||||
javaSrc: path.join(appMain, 'java')
|
javaSrc: path.join(appMain, 'java')
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this._builder = require('./lib/builders/builders').getBuilder(this.root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -161,7 +163,7 @@ class Api {
|
|||||||
if (plugin.getFrameworks(this.platform).length === 0) return;
|
if (plugin.getFrameworks(this.platform).length === 0) return;
|
||||||
selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
|
selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
|
||||||
// This should pick the correct builder, not just get gradle
|
// This should pick the correct builder, not just get gradle
|
||||||
require('./lib/builders/builders').getBuilder().prepBuildFiles();
|
this._builder.prepBuildFiles();
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
// CB-11022 Return truthy value to prevent running prepare after
|
// CB-11022 Return truthy value to prevent running prepare after
|
||||||
.then(() => true);
|
.then(() => true);
|
||||||
@ -193,7 +195,7 @@ class Api {
|
|||||||
if (plugin.getFrameworks(this.platform).length === 0) return;
|
if (plugin.getFrameworks(this.platform).length === 0) return;
|
||||||
|
|
||||||
selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
|
selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
|
||||||
require('./lib/builders/builders').getBuilder().prepBuildFiles();
|
this._builder.prepBuildFiles();
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
// CB-11022 Return truthy value to prevent running prepare after
|
// CB-11022 Return truthy value to prevent running prepare after
|
||||||
.then(() => true);
|
.then(() => true);
|
||||||
|
5
bin/templates/cordova/lib/build.js
vendored
5
bin/templates/cordova/lib/build.js
vendored
@ -23,7 +23,6 @@ var nopt = require('nopt');
|
|||||||
|
|
||||||
var Adb = require('./Adb');
|
var Adb = require('./Adb');
|
||||||
|
|
||||||
var builders = require('./builders/builders');
|
|
||||||
var events = require('cordova-common').events;
|
var events = require('cordova-common').events;
|
||||||
const execa = require('execa');
|
const execa = require('execa');
|
||||||
var CordovaError = require('cordova-common').CordovaError;
|
var CordovaError = require('cordova-common').CordovaError;
|
||||||
@ -145,7 +144,7 @@ function parseOpts (options, resolvedTarget, projectRoot) {
|
|||||||
*/
|
*/
|
||||||
module.exports.runClean = function (options) {
|
module.exports.runClean = function (options) {
|
||||||
var opts = parseOpts(options, null, this.root);
|
var opts = parseOpts(options, null, this.root);
|
||||||
var builder = builders.getBuilder();
|
var builder = this._builder;
|
||||||
|
|
||||||
return builder.prepEnv(opts).then(function () {
|
return builder.prepEnv(opts).then(function () {
|
||||||
return builder.clean(opts);
|
return builder.clean(opts);
|
||||||
@ -166,7 +165,7 @@ module.exports.runClean = function (options) {
|
|||||||
*/
|
*/
|
||||||
module.exports.run = function (options, optResolvedTarget) {
|
module.exports.run = function (options, optResolvedTarget) {
|
||||||
var opts = parseOpts(options, optResolvedTarget, this.root);
|
var opts = parseOpts(options, optResolvedTarget, this.root);
|
||||||
var builder = builders.getBuilder();
|
var builder = this._builder;
|
||||||
|
|
||||||
return builder.prepEnv(opts).then(function () {
|
return builder.prepEnv(opts).then(function () {
|
||||||
if (opts.prepEnv) {
|
if (opts.prepEnv) {
|
||||||
|
3
bin/templates/cordova/lib/run.js
vendored
3
bin/templates/cordova/lib/run.js
vendored
@ -100,7 +100,6 @@ module.exports.run = function (runOptions) {
|
|||||||
});
|
});
|
||||||
}).then(function (resolvedTarget) {
|
}).then(function (resolvedTarget) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const builder = require('./builders/builders').getBuilder();
|
|
||||||
const buildOptions = require('./build').parseBuildOptions(runOptions, null, self.root);
|
const buildOptions = require('./build').parseBuildOptions(runOptions, null, self.root);
|
||||||
|
|
||||||
// Android app bundles cannot be deployed directly to the device
|
// Android app bundles cannot be deployed directly to the device
|
||||||
@ -110,7 +109,7 @@ module.exports.run = function (runOptions) {
|
|||||||
throw packageTypeErrorMessage;
|
throw packageTypeErrorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(builder.fetchBuildResults(buildOptions.buildType, buildOptions.arch));
|
resolve(self._builder.fetchBuildResults(buildOptions.buildType, buildOptions.arch));
|
||||||
}).then(function (buildResults) {
|
}).then(function (buildResults) {
|
||||||
if (resolvedTarget && resolvedTarget.isEmulator) {
|
if (resolvedTarget && resolvedTarget.isEmulator) {
|
||||||
return emulator.wait_for_boot(resolvedTarget.target).then(function () {
|
return emulator.wait_for_boot(resolvedTarget.target).then(function () {
|
||||||
|
@ -23,7 +23,6 @@ var common = require('cordova-common');
|
|||||||
var rewire = require('rewire');
|
var rewire = require('rewire');
|
||||||
|
|
||||||
var AndroidProject = require('../../bin/templates/cordova/lib/AndroidProject');
|
var AndroidProject = require('../../bin/templates/cordova/lib/AndroidProject');
|
||||||
var builders = require('../../bin/templates/cordova/lib/builders/builders');
|
|
||||||
|
|
||||||
var PluginInfo = common.PluginInfo;
|
var PluginInfo = common.PluginInfo;
|
||||||
|
|
||||||
@ -31,7 +30,7 @@ var FIXTURES = path.join(__dirname, '../e2e/fixtures');
|
|||||||
var FAKE_PROJECT_DIR = path.join(os.tmpdir(), 'plugin-test-project');
|
var FAKE_PROJECT_DIR = path.join(os.tmpdir(), 'plugin-test-project');
|
||||||
|
|
||||||
describe('addPlugin method', function () {
|
describe('addPlugin method', function () {
|
||||||
var api, Api, gradleBuilder;
|
var api, Api;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Api = rewire('../../bin/templates/cordova/Api');
|
Api = rewire('../../bin/templates/cordova/Api');
|
||||||
@ -49,22 +48,20 @@ describe('addPlugin method', function () {
|
|||||||
Api.__set__('selfEvents.emit', jasmine.createSpy());
|
Api.__set__('selfEvents.emit', jasmine.createSpy());
|
||||||
|
|
||||||
api = new Api('android', FAKE_PROJECT_DIR);
|
api = new Api('android', FAKE_PROJECT_DIR);
|
||||||
|
spyOn(api._builder, 'prepBuildFiles');
|
||||||
gradleBuilder = jasmine.createSpyObj('gradleBuilder', ['prepBuildFiles']);
|
|
||||||
spyOn(builders, 'getBuilder').and.returnValue(gradleBuilder);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const getPluginFixture = name => new PluginInfo(path.join(FIXTURES, name));
|
const getPluginFixture = name => new PluginInfo(path.join(FIXTURES, name));
|
||||||
|
|
||||||
it('Test#001 : should call gradleBuilder.prepBuildFiles for every plugin with frameworks', () => {
|
it('Test#001 : should call gradleBuilder.prepBuildFiles for every plugin with frameworks', () => {
|
||||||
return api.addPlugin(getPluginFixture('cordova-plugin-fake')).then(() => {
|
return api.addPlugin(getPluginFixture('cordova-plugin-fake')).then(() => {
|
||||||
expect(gradleBuilder.prepBuildFiles).toHaveBeenCalled();
|
expect(api._builder.prepBuildFiles).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Test#002 : shouldn\'t trigger gradleBuilder.prepBuildFiles for plugins without android frameworks', () => {
|
it('Test#002 : shouldn\'t trigger gradleBuilder.prepBuildFiles for plugins without android frameworks', () => {
|
||||||
return api.addPlugin(getPluginFixture('cordova-plugin-fake-ios-frameworks')).then(() => {
|
return api.addPlugin(getPluginFixture('cordova-plugin-fake-ios-frameworks')).then(() => {
|
||||||
expect(gradleBuilder.prepBuildFiles).not.toHaveBeenCalled();
|
expect(api._builder.prepBuildFiles).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const rewire = require('rewire');
|
const rewire = require('rewire');
|
||||||
|
const builders = require('../../bin/templates/cordova/lib/builders/builders');
|
||||||
|
|
||||||
describe('run', () => {
|
describe('run', () => {
|
||||||
let run;
|
let run;
|
||||||
@ -59,6 +60,11 @@ describe('run', () => {
|
|||||||
events: eventsSpyObj,
|
events: eventsSpyObj,
|
||||||
getInstallTarget: getInstallTargetSpy
|
getInstallTarget: getInstallTargetSpy
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// run needs `this` to behave like an Api instance
|
||||||
|
run.run = run.run.bind({
|
||||||
|
_builder: builders.getBuilder('FakeRootPath')
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should run on default device when no target arguments are specified', () => {
|
it('should run on default device when no target arguments are specified', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user