Repository: cordova-lib
Updated Branches:
  refs/heads/master f661a6624 -> fcb612fcb


CB-9976 Reinstall plugins for platform if they were installed with 
cordova@<5.4.0. This closes #344


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/fcb612fc
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/fcb612fc
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/fcb612fc

Branch: refs/heads/master
Commit: fcb612fcbf8e74b709cab96c81e792c2df6ed53b
Parents: f661a66
Author: Vladimir Kotikov <v-vlk...@microsoft.com>
Authored: Mon Nov 16 18:19:47 2015 +0300
Committer: Vladimir Kotikov <v-vlk...@microsoft.com>
Committed: Wed Nov 18 17:44:14 2015 +0300

----------------------------------------------------------------------
 cordova-lib/src/cordova/prepare.js           | 98 +++++++++++++++++++----
 cordova-lib/src/platforms/PlatformApiPoly.js | 19 ++---
 2 files changed, 93 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/fcb612fc/cordova-lib/src/cordova/prepare.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/prepare.js 
b/cordova-lib/src/cordova/prepare.js
index 38c2e4d..b26449c 100644
--- a/cordova-lib/src/cordova/prepare.js
+++ b/cordova-lib/src/cordova/prepare.js
@@ -19,6 +19,9 @@
 
 var cordova_util      = require('./util'),
     ConfigParser      = require('cordova-common').ConfigParser,
+    PlatformJson      = require('cordova-common').PlatformJson,
+    PluginInfoProvider = require('cordova-common').PluginInfoProvider,
+    events            = require('cordova-common').events,
     platforms         = require('../platforms/platforms'),
     HooksRunner       = require('../hooks/HooksRunner'),
     Q                 = require('q'),
@@ -46,8 +49,7 @@ function prepare(options) {
             return platforms.getPlatformApi(p, 
platform_path).getPlatformInfo().locations.www;
         });
         options.paths = paths;
-    })
-    .then(function() {
+    }).then(function() {
         options = cordova_util.preProcessOptions(options);
         options.searchpath = options.searchpath || 
config_json.plugin_search_path;
         // Iterate over each added platform
@@ -83,21 +85,87 @@ function preparePlatforms (platformList, projectRoot, 
options) {
             }
         };
 
-        // platformApi prepare takes care of all functionality
-        // which previously had been executed by cordova.prepare:
-        //   - reset config.xml and then merge changes from project's one,
-        //   - update www directory from project's one and merge assets from 
platform_www,
-        //   - reapply config changes, made by plugins,
-        //   - update platform's project
-        // Please note that plugins' changes, such as installes js files, 
assets and
-        // config changes is not being reinstalled on each prepare.
-        var platformApi = platforms.getPlatformApi(platform);
-        return platformApi.prepare(project)
-        .then(function () {
-            if (options.browserify)
-                return browserify(project, platformApi);
+        // CB-9987 We need to reinstall the plugins for the platform it they 
were added by cordova@<5.4.0
+        return restoreMissingPluginsForPlatform(platform, projectRoot, options)
+        .then(function (argument) {
+            // platformApi prepare takes care of all functionality
+            // which previously had been executed by cordova.prepare:
+            //   - reset config.xml and then merge changes from project's one,
+            //   - update www directory from project's one and merge assets 
from platform_www,
+            //   - reapply config changes, made by plugins,
+            //   - update platform's project
+            // Please note that plugins' changes, such as installes js files, 
assets and
+            // config changes is not being reinstalled on each prepare.
+            var platformApi = platforms.getPlatformApi(platform);
+            return platformApi.prepare(project)
+            .then(function () {
+                if (options.browserify)
+                    return browserify(project, platformApi);
+            });
         });
     }));
 }
 
 module.exports.preparePlatforms = preparePlatforms;
+
+/**
+ * Ensures that plugins, installed with previous versions of CLI (<5.4.0) are
+ *   readded to platform correctly. Also triggers regeneration of
+ *   cordova_plugins.js file.
+ *
+ * @param   {String}  platform     Platform name to check for installed plugins
+ * @param   {String}  projectRoot  A current cordova project location
+ * @param   {Object}  [options]    Options that will be passed to
+ *   PlatformApi.pluginAdd/Remove. This object will be extended with plugin
+ *   variables, used to install the plugin initially (picked from "old"
+ *   plugins/<platform>.json)
+ *
+ * @return  {Promise}               Promise that'll be fulfilled if all the
+ *   plugins reinstalled properly.
+ */
+function restoreMissingPluginsForPlatform(platform, projectRoot, options) {
+    events.emit('verbose', 'Searching PlatformJson files for differences 
between project vs. platform installed plugins');
+
+    // Flow:
+    // 1. Compare <platform>.json file in <project>/plugins ("old") and 
platforms/<platform> ("new")
+    // 2. If there is any differences - merge "old" one into "new"
+    // 3. Reinstall plugins that are missing and was merged on previous step
+
+    var oldPlatformJson = PlatformJson.load(path.join(projectRoot, 'plugins'), 
platform);
+    var platformJson = PlatformJson.load(path.join(projectRoot, 'platforms', 
platform), platform);
+
+    var missingPlugins = Object.keys(oldPlatformJson.root.installed_plugins)
+        .concat(Object.keys(oldPlatformJson.root.dependent_plugins))
+        .reduce(function (result, candidate) {
+            if (!platformJson.isPluginInstalled(candidate))
+                result.push({name: candidate,
+                    // Note: isPluginInstalled is actually returns not a 
boolean,
+                    // but object which corresponds to this particular plugin
+                    variables: oldPlatformJson.isPluginInstalled(candidate)});
+
+            return result;
+        }, []);
+
+    if (missingPlugins.length === 0) {
+        events.emit('verbose', 'No differences found between project and ' +
+            platform + ' platform. Continuing...');
+        return Q.resolve();
+    }
+
+    var api = platforms.getPlatformApi(platform);
+    var provider = new PluginInfoProvider();
+    return missingPlugins.reduce(function (promise, plugin) {
+        return promise.then(function () {
+            var pluginOptions = options || {};
+            pluginOptions.variables = plugin.variables;
+            pluginOptions.usePlatformWww = true;
+
+            events.emit('verbose', 'Reinstalling missing plugin ' + 
plugin.name + ' to ' + platform + ' platform');
+            var pluginInfo = provider.get(path.join(projectRoot, 'plugins', 
plugin.name));
+            return api.removePlugin(pluginInfo, pluginOptions)
+            .then(function () {
+                return api.addPlugin(pluginInfo, pluginOptions);
+            });
+        });
+    }, Q());
+}

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/fcb612fc/cordova-lib/src/platforms/PlatformApiPoly.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/platforms/PlatformApiPoly.js 
b/cordova-lib/src/platforms/PlatformApiPoly.js
index 5563028..47277e8 100644
--- a/cordova-lib/src/platforms/PlatformApiPoly.js
+++ b/cordova-lib/src/platforms/PlatformApiPoly.js
@@ -577,6 +577,11 @@ PlatformApiPoly.prototype._addModulesInfo = 
function(plugin, targetDir) {
     });
 
     this._platformJson.root.modules = 
installedModules.concat(modulesToInstall);
+    if (!this._platformJson.root.plugin_metadata) {
+        this._platformJson.root.plugin_metadata = {};
+    }
+    this._platformJson.root.plugin_metadata[plugin.id] = plugin.version;
+
     this._writePluginModules(targetDir);
     this._platformJson.save();
 };
@@ -603,6 +608,10 @@ PlatformApiPoly.prototype._removeModulesInfo = 
function(plugin, targetDir) {
     });
 
     this._platformJson.root.modules = updatedModules;
+    if (this._platformJson.root.plugin_metadata) {
+        delete this._platformJson.root.plugin_metadata[plugin.id];
+    }
+
     this._writePluginModules(targetDir);
     this._platformJson.save();
 };
@@ -616,20 +625,12 @@ PlatformApiPoly.prototype._removeModulesInfo = 
function(plugin, targetDir) {
  *   directories.
  */
 PlatformApiPoly.prototype._writePluginModules = function (targetDir) {
-    var self = this;
     // Write out moduleObjects as JSON wrapped in a cordova module to 
cordova_plugins.js
     var final_contents = 'cordova.define(\'cordova/plugin_list\', 
function(require, exports, module) {\n';
     final_contents += 'module.exports = ' + 
JSON.stringify(this._platformJson.root.modules, null, '    ') + ';\n';
     final_contents += 'module.exports.metadata = \n';
     final_contents += '// TOP OF METADATA\n';
-
-    var pluginMetadata = Object.keys(this._platformJson.root.installed_plugins)
-    .reduce(function (metadata, plugin) {
-        metadata[plugin] = 
self._platformJson.root.installed_plugins[plugin].version;
-        return metadata;
-    }, {});
-
-    final_contents += JSON.stringify(pluginMetadata, null, '    ') + '\n';
+    final_contents += JSON.stringify(this._platformJson.root.plugin_metadata 
|| {}, null, '    ') + '\n';
     final_contents += '// BOTTOM OF METADATA\n';
     final_contents += '});'; // Close cordova.define.
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to