From 7be9e880c28ab753ea32353bbe53836bb85663a9 Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Wed, 17 Feb 2016 16:37:58 +0300 Subject: [PATCH] CB-10618 Handle gradle frameworks on plugin installation/uninstallation This closes #259 --- bin/lib/create.js | 4 +- bin/templates/cordova/Api.js | 10 +++ bin/templates/cordova/lib/build.js | 5 -- package.json | 4 +- .../plugin.xml | 41 ++++++++++ spec/{create => unit}/create.spec.js | 0 spec/unit/plugin.spec.js | 77 +++++++++++++++++++ 7 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 spec/e2e/fixtures/cordova-plugin-fake-ios-frameworks/plugin.xml rename spec/{create => unit}/create.spec.js (100%) create mode 100644 spec/unit/plugin.spec.js diff --git a/bin/lib/create.js b/bin/lib/create.js index 0307fd56..d8def893 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -126,8 +126,8 @@ function writeProjectProperties(projectPath, target_api) { } function prepBuildFiles(projectPath) { - var buildModule = require(path.join(path.resolve(projectPath), 'cordova', 'lib', 'build')); - buildModule.prepBuildFiles(); + var buildModule = require(path.resolve(projectPath, 'cordova/lib/builders/builders')); + buildModule.getBuilder('gradle').prepBuildFiles(); } function copyBuildRules(projectPath) { diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js index 9a8938c2..ad6f71c6 100644 --- a/bin/templates/cordova/Api.js +++ b/bin/templates/cordova/Api.js @@ -219,6 +219,11 @@ Api.prototype.addPlugin = function (plugin, installOptions) { .add_plugin_changes(plugin, installOptions.variables, /*is_top_level=*/true, /*should_increment=*/true) .save_all(); + if (plugin.getFrameworks(self.platform).length > 0) { + self.events.emit('verbose', 'Updating build files since android plugin contained '); + require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles(); + } + var targetDir = installOptions.usePlatformWww ? self.locations.platformWww : self.locations.www; @@ -272,6 +277,11 @@ Api.prototype.removePlugin = function (plugin, uninstallOptions) { .remove_plugin_changes(plugin, /*is_top_level=*/true) .save_all(); + if (plugin.getFrameworks(self.platform).length > 0) { + self.events.emit('verbose', 'Updating build files since android plugin contained '); + require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles(); + } + var targetDir = uninstallOptions.usePlatformWww ? self.locations.platformWww : self.locations.www; diff --git a/bin/templates/cordova/lib/build.js b/bin/templates/cordova/lib/build.js index 0a2e5fab..e1263ef7 100644 --- a/bin/templates/cordova/lib/build.js +++ b/bin/templates/cordova/lib/build.js @@ -169,11 +169,6 @@ module.exports.run = function(options, optResolvedTarget) { }); }; -// Called by plugman after installing plugins, and by create script after creating project. -module.exports.prepBuildFiles = function() { - return builders.getBuilder('gradle').prepBuildFiles(); -}; - /* * Detects the architecture of a device/emulator * Returns "arm" or "x86". diff --git a/package.json b/package.json index cfdcbc25..d3c59421 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "apache" ], "scripts": { - "test": "npm run jshint && jasmine-node --color spec/create", + "test": "npm run jshint && jasmine-node --color spec/unit", "test-build": "jasmine-node --captureExceptions --color spec/e2e", "jshint": "node node_modules/jshint/bin/jshint bin && node node_modules/jshint/bin/jshint spec" }, @@ -43,4 +43,4 @@ "jshint": "^2.6.0", "promise-matchers": "~0" } -} \ No newline at end of file +} diff --git a/spec/e2e/fixtures/cordova-plugin-fake-ios-frameworks/plugin.xml b/spec/e2e/fixtures/cordova-plugin-fake-ios-frameworks/plugin.xml new file mode 100644 index 00000000..6df9acd1 --- /dev/null +++ b/spec/e2e/fixtures/cordova-plugin-fake-ios-frameworks/plugin.xml @@ -0,0 +1,41 @@ + + + + + Fake + + + Fake plugin to test plugin installation and properties parsing on Android. + + + Apache 2.0 + + + + + + + + + + diff --git a/spec/create/create.spec.js b/spec/unit/create.spec.js similarity index 100% rename from spec/create/create.spec.js rename to spec/unit/create.spec.js diff --git a/spec/unit/plugin.spec.js b/spec/unit/plugin.spec.js new file mode 100644 index 00000000..59fb709b --- /dev/null +++ b/spec/unit/plugin.spec.js @@ -0,0 +1,77 @@ +/** + 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. +*/ + +/* jshint node:true */ + +var Q = require('q'); +var os = require('os'); +var path = require('path'); +var common = require('cordova-common'); + +var AndroidProject = require('../../bin/templates/cordova/lib/AndroidProject'); +var builders = require('../../bin/templates/cordova/lib/builders/builders'); + +var PluginInfo = common.PluginInfo; + +var FIXTURES = path.join(__dirname, '../e2e/fixtures'); +var FAKE_PROJECT_DIR = path.join(os.tmpdir(), 'plugin-test-project'); + +describe('addPlugin method', function () { + var api, fail, gradleBuilder; + + beforeEach(function() { + var ActionStack = jasmine.createSpyObj('ActionStack', ['createAction', 'push', 'process']); + ActionStack.process.andReturn(Q()); + spyOn(common, 'ActionStack').andReturn(ActionStack); + + spyOn(AndroidProject, 'getProjectFile') + .andReturn(jasmine.createSpyObj('AndroidProject', ['getPackageName', 'write'])); + + var Api = require('../../bin/templates/cordova/Api'); + api = new Api('android', FAKE_PROJECT_DIR); + + spyOn(api, '_addModulesInfo'); + spyOn(api._munger, 'add_plugin_changes') + .andReturn(jasmine.createSpyObj('munger', ['save_all'])); + + fail = jasmine.createSpy('fail'); + gradleBuilder = jasmine.createSpyObj('gradleBuilder', ['prepBuildFiles']); + spyOn(builders, 'getBuilder').andReturn(gradleBuilder); + }); + + it('should call gradleBuilder.prepBuildFiles for every plugin with frameworks', function(done) { + api.addPlugin(new PluginInfo(path.join(FIXTURES, 'cordova-plugin-fake'))) + .catch(fail) + .fin(function () { + expect(fail).not.toHaveBeenCalled(); + expect(gradleBuilder.prepBuildFiles).toHaveBeenCalled(); + done(); + }); + }); + + it('shouldn\'t trigger gradleBuilder.prepBuildFiles for plugins without android frameworks', function(done) { + api.addPlugin(new PluginInfo(path.join(FIXTURES, 'cordova-plugin-fake-ios-frameworks'))) + .catch(fail) + .fin(function () { + expect(fail).not.toHaveBeenCalled(); + expect(gradleBuilder.prepBuildFiles).not.toHaveBeenCalled(); + done(); + }); + }); +});