You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2016/03/30 16:36:00 UTC
[1/2] cordova-lib git commit: CB-10822 Manage plugins/modules
metadata using PlatformJson
Repository: cordova-lib
Updated Branches:
refs/heads/master 9d8124bef -> 5737d74b2
CB-10822 Manage plugins/modules metadata using PlatformJson
Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/57f2b3f2
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/57f2b3f2
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/57f2b3f2
Branch: refs/heads/master
Commit: 57f2b3f2a4e6129dab7f9e039823b339bf26db3a
Parents: 9d8124b
Author: Vladimir Kotikov <v-...@microsoft.com>
Authored: Thu Mar 10 14:03:11 2016 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Wed Mar 30 15:33:40 2016 +0300
----------------------------------------------------------------------
cordova-common/package.json | 5 +-
cordova-common/spec/PlatformJson.spec.js | 160 ++++++++++++++++++++++++++
cordova-common/src/PlatformJson.js | 124 ++++++++++++++++++++
3 files changed, 287 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/57f2b3f2/cordova-common/package.json
----------------------------------------------------------------------
diff --git a/cordova-common/package.json b/cordova-common/package.json
index 20c4916..6c5e1f2 100644
--- a/cordova-common/package.json
+++ b/cordova-common/package.json
@@ -33,14 +33,15 @@
"plist": "^1.2.0",
"q": "^1.4.1",
"semver": "^5.0.1",
- "shelljs": "^0.5.1",
+ "shelljs": "^0.5.3",
"underscore": "^1.8.3",
"unorm": "^1.3.3"
},
"devDependencies": {
"istanbul": "^0.3.17",
"jasmine-node": "^1.14.5",
- "jshint": "^2.8.0"
+ "jshint": "^2.8.0",
+ "rewire": "^2.5.1"
},
"contributors": []
}
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/57f2b3f2/cordova-common/spec/PlatformJson.spec.js
----------------------------------------------------------------------
diff --git a/cordova-common/spec/PlatformJson.spec.js b/cordova-common/spec/PlatformJson.spec.js
new file mode 100644
index 0000000..c9310a2
--- /dev/null
+++ b/cordova-common/spec/PlatformJson.spec.js
@@ -0,0 +1,160 @@
+/**
+ 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 rewire = require('rewire');
+var PlatformJson = rewire('../src/PlatformJson');
+var ModuleMetadata = PlatformJson.__get__('ModuleMetadata');
+
+var FAKE_MODULE = {
+ name: 'fakeModule',
+ src: 'www/fakeModule.js',
+ clobbers: [{target: 'window.fakeClobber'}],
+ merges: [{target: 'window.fakeMerge'}],
+ runs: true
+};
+
+describe('PlatformJson class', function() {
+ it('should be constructable', function () {
+ expect(new PlatformJson()).toEqual(jasmine.any(PlatformJson));
+ });
+
+ describe('instance', function () {
+ var platformJson;
+ var fakePlugin;
+
+ beforeEach(function () {
+ platformJson = new PlatformJson('/fake/path', 'android');
+ fakePlugin = jasmine.createSpyObj('fakePlugin', ['getJsModules']);
+ fakePlugin.id = 'fakeId';
+ fakePlugin.version = '1.0.0';
+ fakePlugin.getJsModules.andReturn([FAKE_MODULE]);
+ });
+
+ describe('addPluginMetadata method', function () {
+ it('should not throw if root "modules" property is missing', function () {
+ expect(function () {
+ platformJson.addPluginMetadata(fakePlugin);
+ }).not.toThrow();
+ });
+
+ it('should add each module to "root.modules" array', function () {
+ platformJson.addPluginMetadata(fakePlugin);
+ expect(platformJson.root.modules.length).toBe(1);
+ expect(platformJson.root.modules[0]).toEqual(jasmine.any(ModuleMetadata));
+ });
+
+ it('shouldn\'t add module if there is already module with the same file added', function () {
+ platformJson.root.modules = [{
+ name: 'fakePlugin2',
+ file: 'plugins/fakeId/www/fakeModule.js'
+ }];
+
+ platformJson.addPluginMetadata(fakePlugin);
+ expect(platformJson.root.modules.length).toBe(1);
+ expect(platformJson.root.modules[0].name).toBe('fakePlugin2');
+ });
+
+ it('should add entry to plugin_metadata with corresponding version', function () {
+ platformJson.addPluginMetadata(fakePlugin);
+ expect(platformJson.root.plugin_metadata[fakePlugin.id]).toBe(fakePlugin.version);
+ });
+ });
+
+ describe('removePluginMetadata method', function () {
+ it('should not throw if root "modules" property is missing', function () {
+ expect(function () {
+ platformJson.removePluginMetadata(fakePlugin);
+ }).not.toThrow();
+ });
+
+ it('should remove plugin modules from "root.modules" array based on file path', function () {
+
+ var pluginPaths = [
+ 'plugins/fakeId/www/fakeModule.js',
+ 'plugins/otherPlugin/www/module1.js',
+ 'plugins/otherPlugin/www/module1.js'
+ ];
+
+ platformJson.root.modules = pluginPaths.map(function (p) { return {file: p}; });
+ platformJson.removePluginMetadata(fakePlugin);
+ var resultantPaths = platformJson.root.modules
+ .map(function (p) { return p.file; })
+ .filter(function (f) { return /fakeModule\.js$/.test(f); });
+
+ expect(resultantPaths.length).toBe(0);
+ });
+
+ it('should remove entry from plugin_metadata with corresponding version', function () {
+ platformJson.root.plugin_metadata = {};
+ platformJson.root.plugin_metadata[fakePlugin.id] = fakePlugin.version;
+ platformJson.removePluginMetadata(fakePlugin);
+ expect(platformJson.root.plugin_metadata[fakePlugin.id]).not.toBeDefined();
+ });
+ });
+
+ describe('generateMetadata method', function () {
+ it('should generate text metadata containing list of installed modules', function () {
+ var meta = platformJson.addPluginMetadata(fakePlugin).generateMetadata();
+ expect(typeof meta).toBe('string');
+ expect(meta.indexOf(JSON.stringify(platformJson.root.modules, null, 4))).toBeGreaterThan(0);
+ // expect(meta).toMatch(JSON.stringify(platformJson.root.modules, null, 4));
+ expect(meta).toMatch(JSON.stringify(platformJson.root.plugin_metadata, null, 4));
+ });
+ });
+ });
+});
+
+describe('ModuleMetadata class', function () {
+ it('should be constructable', function () {
+ var meta;
+ expect(function name(params) {
+ meta = new ModuleMetadata('fakePlugin', {src: 'www/fakeModule.js'});
+ }).not.toThrow();
+ expect(meta instanceof ModuleMetadata).toBeTruthy();
+ });
+
+ it('should throw if either pluginId or jsModule argument isn\'t specified', function () {
+ expect(ModuleMetadata).toThrow();
+ expect(function () { new ModuleMetadata('fakePlugin', {}); }).toThrow();
+ });
+
+ it('should guess module id either from name property of from module src', function () {
+ expect(new ModuleMetadata('fakePlugin', {name: 'fakeModule'}).id).toMatch(/fakeModule$/);
+ expect(new ModuleMetadata('fakePlugin', {src: 'www/fakeModule.js'}).id).toMatch(/fakeModule$/);
+ });
+
+ it('should read "clobbers" property from module', function () {
+ expect(new ModuleMetadata('fakePlugin', {name: 'fakeModule'}).clobbers).not.toBeDefined();
+ var metadata = new ModuleMetadata('fakePlugin', FAKE_MODULE);
+ expect(metadata.clobbers).toEqual(jasmine.any(Array));
+ expect(metadata.clobbers[0]).toBe(FAKE_MODULE.clobbers[0].target);
+ });
+
+ it('should read "merges" property from module', function () {
+ expect(new ModuleMetadata('fakePlugin', {name: 'fakeModule'}).merges).not.toBeDefined();
+ var metadata = new ModuleMetadata('fakePlugin', FAKE_MODULE);
+ expect(metadata.merges).toEqual(jasmine.any(Array));
+ expect(metadata.merges[0]).toBe(FAKE_MODULE.merges[0].target);
+ });
+
+ it('should read "runs" property from module', function () {
+ expect(new ModuleMetadata('fakePlugin', {name: 'fakeModule'}).runs).not.toBeDefined();
+ expect(new ModuleMetadata('fakePlugin', FAKE_MODULE).runs).toBe(true);
+ });
+});
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/57f2b3f2/cordova-common/src/PlatformJson.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/PlatformJson.js b/cordova-common/src/PlatformJson.js
index 793e976..4e2b287 100644
--- a/cordova-common/src/PlatformJson.js
+++ b/cordova-common/src/PlatformJson.js
@@ -91,6 +91,38 @@ PlatformJson.prototype.addPlugin = function(pluginId, variables, isTopLevel) {
return this;
};
+/**
+ * @chaining
+ * Generates and adds metadata for provided plugin into associated <platform>.json file
+ *
+ * @param {PluginInfo} pluginInfo A pluginInfo instance to add metadata from
+ * @returns {this} Current PlatformJson instance to allow calls chaining
+ */
+PlatformJson.prototype.addPluginMetadata = function (pluginInfo) {
+
+ var installedModules = this.root.modules || [];
+
+ var installedPaths = installedModules.map(function (installedModule) {
+ return installedModule.file;
+ });
+
+ var modulesToInstall = pluginInfo.getJsModules(this.platform)
+ .map(function (module) {
+ return new ModuleMetadata(pluginInfo.id, module);
+ })
+ .filter(function (metadata) {
+ // Filter out modules which are already added to metadata
+ return installedPaths.indexOf(metadata.file) === -1;
+ });
+
+ this.root.modules = installedModules.concat(modulesToInstall);
+
+ this.root.plugin_metadata = this.root.plugin_metadata || {};
+ this.root.plugin_metadata[pluginInfo.id] = pluginInfo.version;
+
+ return this;
+};
+
PlatformJson.prototype.removePlugin = function(pluginId, isTopLevel) {
var pluginsList = isTopLevel ?
this.root.installed_plugins :
@@ -101,6 +133,35 @@ PlatformJson.prototype.removePlugin = function(pluginId, isTopLevel) {
return this;
};
+/**
+ * @chaining
+ * Removes metadata for provided plugin from associated file
+ *
+ * @param {PluginInfo} pluginInfo A PluginInfo instance to which modules' metadata
+ * we need to remove
+ *
+ * @returns {this} Current PlatformJson instance to allow calls chaining
+ */
+PlatformJson.prototype.removePluginMetadata = function (pluginInfo) {
+ var modulesToRemove = pluginInfo.getJsModules(this.platform)
+ .map(function (jsModule) {
+ return ['plugins', pluginInfo.id, jsModule.src].join('/');
+ });
+
+ var installedModules = this.root.modules || [];
+ this.root.modules = installedModules
+ .filter(function (installedModule) {
+ // Leave only those metadatas which 'file' is not in removed modules
+ return (modulesToRemove.indexOf(installedModule.file) === -1);
+ });
+
+ if (this.root.plugin_metadata) {
+ delete this.root.plugin_metadata[pluginInfo.id];
+ }
+
+ return this;
+};
+
PlatformJson.prototype.addInstalledPluginToPrepareQueue = function(pluginDirName, vars, is_top_level) {
this.root.prepare_queue.installed.push({'plugin':pluginDirName, 'vars':vars, 'topLevel':is_top_level});
};
@@ -125,6 +186,39 @@ PlatformJson.prototype.makeTopLevel = function(pluginId) {
return this;
};
+/**
+ * Generates a metadata for all installed plugins and js modules. The resultant
+ * string is ready to be written to 'cordova_plugins.js'
+ *
+ * @returns {String} cordova_plugins.js contents
+ */
+PlatformJson.prototype.generateMetadata = function () {
+ return [
+ 'cordova.define(\'cordova/plugin_list\', function(require, exports, module) {',
+ 'module.exports = ' + JSON.stringify(this.root.modules, null, 4) + ';',
+ 'module.exports.metadata = ',
+ '// TOP OF METADATA',
+ JSON.stringify(this.root.plugin_metadata, null, 4) + ';',
+ '// BOTTOM OF METADATA',
+ '});' // Close cordova.define.
+ ].join('\n');
+};
+
+/**
+ * @chaining
+ * Generates and then saves metadata to specified file. Doesn't check if file exists.
+ *
+ * @param {String} destination File metadata will be written to
+ * @return {PlatformJson} PlatformJson instance
+ */
+PlatformJson.prototype.generateAndSaveMetadata = function (destination) {
+ var meta = this.generateMetadata();
+ shelljs.mkdir('-p', path.dirname(destination));
+ fs.writeFileSync(destination, meta, 'utf-8');
+
+ return this;
+};
+
// convert a munge from the old format ([file][parent][xml] = count) to the current one
function fix_munge(root) {
root.prepare_queue = root.prepare_queue || {installed:[], uninstalled:[]};
@@ -151,5 +245,35 @@ function fix_munge(root) {
return root;
}
+/**
+ * @constructor
+ * @class ModuleMetadata
+ *
+ * Creates a ModuleMetadata object that represents module entry in 'cordova_plugins.js'
+ * file at run time
+ *
+ * @param {String} pluginId Plugin id where this module installed from
+ * @param (JsModule|Object) jsModule A js-module entry from PluginInfo class to generate metadata for
+ */
+function ModuleMetadata (pluginId, jsModule) {
+
+ if (!pluginId) throw new TypeError('pluginId argument must be a valid plugin id');
+ if (!jsModule.src && !jsModule.name) throw new TypeError('jsModule argument must contain src or/and name properties');
+
+ this.id = pluginId + '.' + ( jsModule.name || jsModule.src.match(/([^\/]+)\.js/)[1] );
+ this.file = ['plugins', pluginId, jsModule.src].join('/');
+ this.pluginId = pluginId;
+
+ if (jsModule.clobbers && jsModule.clobbers.length > 0) {
+ this.clobbers = jsModule.clobbers.map(function(o) { return o.target; });
+ }
+ if (jsModule.merges && jsModule.merges.length > 0) {
+ this.merges = jsModule.merges.map(function(o) { return o.target; });
+ }
+ if (jsModule.runs) {
+ this.runs = true;
+ }
+}
+
module.exports = PlatformJson;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org
[2/2] cordova-lib git commit: CB-10833 Deduplicate common logic for
plugin installation/uninstallation
Posted by an...@apache.org.
CB-10833 Deduplicate common logic for plugin installation/uninstallation
This closes #414
Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/5737d74b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/5737d74b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/5737d74b
Branch: refs/heads/master
Commit: 5737d74b292a5681efb9c766538ac417ea07ab33
Parents: 57f2b3f
Author: Vladimir Kotikov <v-...@microsoft.com>
Authored: Wed Mar 9 22:26:05 2016 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Wed Mar 30 17:32:28 2016 +0300
----------------------------------------------------------------------
cordova-common/cordova-common.js | 5 +-
cordova-common/package.json | 1 +
cordova-common/spec/PluginManager.spec.js | 94 ++++++++++++++++
cordova-common/src/PluginManager.js | 148 +++++++++++++++++++++++++
4 files changed, 245 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/5737d74b/cordova-common/cordova-common.js
----------------------------------------------------------------------
diff --git a/cordova-common/cordova-common.js b/cordova-common/cordova-common.js
index 22e90a7..1f97b66 100644
--- a/cordova-common/cordova-common.js
+++ b/cordova-common/cordova-common.js
@@ -17,9 +17,6 @@
under the License.
*/
-/* jshint node:true */
-
-// For now expose plugman and cordova just as they were in the old repos
exports = module.exports = {
events: require('./src/events'),
superspawn: require('./src/superspawn'),
@@ -33,6 +30,8 @@ exports = module.exports = {
PluginInfo: require('./src/PluginInfo/PluginInfo.js'),
PluginInfoProvider: require('./src/PluginInfo/PluginInfoProvider.js'),
+
+ PluginManager: require('./src/PluginManager'),
ConfigChanges: require('./src/ConfigChanges/ConfigChanges.js'),
ConfigKeeper: require('./src/ConfigChanges/ConfigKeeper.js'),
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/5737d74b/cordova-common/package.json
----------------------------------------------------------------------
diff --git a/cordova-common/package.json b/cordova-common/package.json
index 6c5e1f2..5e4b89a 100644
--- a/cordova-common/package.json
+++ b/cordova-common/package.json
@@ -41,6 +41,7 @@
"istanbul": "^0.3.17",
"jasmine-node": "^1.14.5",
"jshint": "^2.8.0",
+ "promise-matchers": "^0.9.6",
"rewire": "^2.5.1"
},
"contributors": []
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/5737d74b/cordova-common/spec/PluginManager.spec.js
----------------------------------------------------------------------
diff --git a/cordova-common/spec/PluginManager.spec.js b/cordova-common/spec/PluginManager.spec.js
new file mode 100644
index 0000000..e99ab05
--- /dev/null
+++ b/cordova-common/spec/PluginManager.spec.js
@@ -0,0 +1,94 @@
+/**
+ 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.
+*/
+
+require ('promise-matchers');
+
+var Q = require('q');
+var path = require('path');
+var rewire = require('rewire');
+var PlatformJson = require('../src/PlatformJson');
+var PluginManager = rewire('../src/PluginManager');
+var PluginInfo = require('../src/PluginInfo/PluginInfo');
+var ConfigChanges = require('../src/ConfigChanges/ConfigChanges');
+
+var DUMMY_PLUGIN = path.join(__dirname, 'fixtures/plugins/org.test.plugins.dummyplugin');
+var FAKE_PLATFORM = 'cordova-atari';
+var FAKE_LOCATIONS = {
+ root: '/some/fake/path'
+};
+
+describe('PluginManager class', function() {
+
+ beforeEach(function () {
+ spyOn(PlatformJson, 'load');
+ spyOn(ConfigChanges, 'PlatformMunger');
+ });
+
+ it('should be constructable', function () {
+ expect(new PluginManager(FAKE_PLATFORM, FAKE_LOCATIONS)).toEqual(jasmine.any(PluginManager));
+ });
+
+ it('should return new instance for every PluginManager.get call', function () {
+ expect(PluginManager.get(FAKE_PLATFORM, FAKE_LOCATIONS)).toEqual(jasmine.any(PluginManager));
+ expect(PluginManager.get(FAKE_PLATFORM, FAKE_LOCATIONS))
+ .not.toBe(PluginManager.get(FAKE_PLATFORM, FAKE_LOCATIONS));
+ });
+
+ describe('instance', function () {
+ var actions, manager;
+ var FAKE_PROJECT;
+ var ActionStackOrig = PluginManager.__get__('ActionStack');
+
+ beforeEach(function () {
+ FAKE_PROJECT = jasmine.createSpyObj('project', ['getInstaller', 'getUninstaller', 'write']);
+ manager = new PluginManager('windows', FAKE_LOCATIONS, FAKE_PROJECT);
+ actions = jasmine.createSpyObj('actions', ['createAction', 'push', 'process']);
+ actions.process.andReturn(Q.resolve());
+ PluginManager.__set__('ActionStack', function () { return actions; });
+ });
+
+ afterEach(function () {
+ PluginManager.__set__('ActionStack', ActionStackOrig);
+ });
+
+ describe('addPlugin method', function () {
+ it('should return a promise', function () {
+ expect(Q.isPromise(manager.addPlugin(null, {}))).toBe(true);
+ });
+
+ it('should reject if "plugin" parameter is not specified or not a PluginInfo instance', function () {
+ expect(manager.addPlugin(null, {})).toHaveBeenRejected();
+ expect(manager.addPlugin({}, {})).toHaveBeenRejected();
+ expect(manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})).not.toHaveBeenRejected();
+ });
+
+ it('should iterate through all plugin\'s files and frameworks', function () {
+ manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})
+ .then(function () {
+ expect(FAKE_PROJECT.getInstaller.calls.length).toBe(16);
+ expect(FAKE_PROJECT.getUninstaller.calls.length).toBe(16);
+
+ expect(actions.push.calls.length).toBe(16);
+ expect(actions.process).toHaveBeenCalled();
+ expect(FAKE_PROJECT.write).toHaveBeenCalled();
+ });
+ });
+ });
+ });
+});
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/5737d74b/cordova-common/src/PluginManager.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/PluginManager.js b/cordova-common/src/PluginManager.js
new file mode 100644
index 0000000..ea4d688
--- /dev/null
+++ b/cordova-common/src/PluginManager.js
@@ -0,0 +1,148 @@
+/*
+ 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 Q = require('q');
+var fs = require('fs');
+var path = require('path');
+
+var ActionStack = require('./ActionStack');
+var PlatformJson = require('./PlatformJson');
+var CordovaError = require('./CordovaError/CordovaError');
+var PlatformMunger = require('./ConfigChanges/ConfigChanges').PlatformMunger;
+var PluginInfoProvider = require('./PluginInfo/PluginInfoProvider');
+
+/**
+ * @constructor
+ * @class PluginManager
+ * Represents an entity for adding/removing plugins for platforms
+ *
+ * @param {String} platform Platform name
+ * @param {Object} locations - Platform files and directories
+ * @param {IDEProject} ideProject The IDE project to add/remove plugin changes to/from
+ */
+function PluginManager(platform, locations, ideProject) {
+ this.platform = platform;
+ this.locations = locations;
+ this.project = ideProject;
+
+ var platformJson = PlatformJson.load(locations.root, platform);
+ this.munger = new PlatformMunger(platform, locations.root, platformJson, new PluginInfoProvider());
+}
+
+
+/**
+ * @constructs PluginManager
+ * A convenience shortcut to new PluginManager(...)
+ *
+ * @param {String} platform Platform name
+ * @param {Object} locations - Platform files and directories
+ * @param {IDEProject} ideProject The IDE project to add/remove plugin changes to/from
+ * @returns new PluginManager instance
+ */
+PluginManager.get = function(platform, locations, ideProject) {
+ return new PluginManager(platform, locations, ideProject);
+};
+
+PluginManager.INSTALL = 'install';
+PluginManager.UNINSTALL = 'uninstall';
+
+module.exports = PluginManager;
+
+/**
+ * Describes and implements common plugin installation/uninstallation routine. The flow is the following:
+ * * Validate and set defaults for options. Note that options are empty by default. Everything
+ * needed for platform IDE project must be passed from outside. Plugin variables (which
+ * are the part of the options) also must be already populated with 'PACKAGE_NAME' variable.
+ * * Collect all plugin's native and web files, get installers/uninstallers and process
+ * all these via ActionStack.
+ * * Save the IDE project, so the changes made by installers are persisted.
+ * * Generate config changes munge for plugin and apply it to all required files
+ * * Generate metadata for plugin and plugin modules and save it to 'cordova_plugins.js'
+ *
+ * @param {PluginInfo} plugin A PluginInfo structure representing plugin to install
+ * @param {Object} [options={}] An installation options. It is expected but is not necessary
+ * that options would contain 'variables' inner object with 'PACKAGE_NAME' field set by caller.
+ *
+ * @returns {Promise} Returns a Q promise, either resolved in case of success, rejected otherwise.
+ */
+PluginManager.prototype.doOperation = function (operation, plugin, options) {
+ if (operation !== PluginManager.INSTALL && operation !== PluginManager.UNINSTALL)
+ return Q.reject(new CordovaError('The parameter is incorrect. The opeation must be either "add" or "remove"'));
+
+ if (!plugin || plugin.constructor.name !== 'PluginInfo')
+ return Q.reject(new CordovaError('The parameter is incorrect. The first parameter should be a PluginInfo instance'));
+
+ // Set default to empty object to play safe when accesing properties
+ options = options || {};
+
+ var self = this;
+ var actions = new ActionStack();
+
+ // gather all files need to be handled during operation ...
+ plugin.getFilesAndFrameworks(this.platform)
+ .concat(plugin.getAssets(this.platform))
+ .concat(plugin.getJsModules(this.platform))
+ // ... put them into stack ...
+ .forEach(function(item) {
+ var installer = self.project.getInstaller(item.itemType);
+ var uninstaller = self.project.getUninstaller(item.itemType);
+ var actionArgs = [item, plugin, self.project, options];
+
+ var action;
+ if (operation === PluginManager.INSTALL) {
+ action = actions.createAction.apply(actions, [installer, actionArgs, uninstaller, actionArgs]);
+ } else /* op === PluginManager.UNINSTALL */{
+ action = actions.createAction.apply(actions, [uninstaller, actionArgs, installer, actionArgs]);
+ }
+ actions.push(action);
+ });
+
+ // ... and run through the action stack
+ return actions.process(this.platform)
+ .then(function () {
+ if (self.project.write) {
+ self.project.write();
+ }
+
+ if (operation === PluginManager.INSTALL) {
+ // Ignore passed `is_top_level` option since platform itself doesn't know
+ // anything about managing dependencies - it's responsibility of caller.
+ self.munger.add_plugin_changes(plugin, options.variables, /*is_top_level=*/true, /*should_increment=*/true);
+ self.munger.platformJson.addPluginMetadata(plugin);
+ } else {
+ self.munger.remove_plugin_changes(plugin, /*is_top_level=*/true);
+ self.munger.platformJson.removePluginMetadata(plugin);
+ }
+
+ // Save everything (munge and plugin/modules metadata)
+ self.munger.save_all();
+
+ var targetDir = options.usePlatformWww ? self.locations.platformWww : self.locations.www;
+ fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'),
+ self.munger.platformJson.generateMetadata(), 'utf-8');
+ });
+};
+
+PluginManager.prototype.addPlugin = function (plugin, installOptions) {
+ return this.doOperation(PluginManager.INSTALL, plugin, installOptions);
+};
+
+PluginManager.prototype.removePlugin = function (plugin, uninstallOptions) {
+ return this.doOperation(PluginManager.UNINSTALL, plugin, uninstallOptions);
+};
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org