You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2017/08/30 06:10:12 UTC

[2/4] cordova-lib git commit: CB-13145 : added variable-merge.js to deal with plugin.xml variables for uninstall

CB-13145 : added variable-merge.js to deal with plugin.xml variables for uninstall


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

Branch: refs/heads/master
Commit: 263502dae6f362b547f98c2a665d656874b56c26
Parents: 295f29c
Author: Audrey So <au...@apache.org>
Authored: Wed Aug 23 13:27:45 2017 -0700
Committer: Steve Gill <st...@gmail.com>
Committed: Tue Aug 29 22:32:09 2017 -0700

----------------------------------------------------------------------
 src/cordova/plugin/add.js     | 11 ++++---
 src/cordova/plugin/remove.js  | 10 +++---
 src/cordova/plugin/util.js    | 18 +++++++++--
 src/plugman/install.js        | 23 ++------------
 src/plugman/uninstall.js      |  5 +++
 src/plugman/variable-merge.js | 63 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 96 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/263502da/src/cordova/plugin/add.js
----------------------------------------------------------------------
diff --git a/src/cordova/plugin/add.js b/src/cordova/plugin/add.js
index e6f8283..66bec9c 100644
--- a/src/cordova/plugin/add.js
+++ b/src/cordova/plugin/add.js
@@ -29,7 +29,6 @@ var ConfigParser = require('cordova-common').ConfigParser;
 var CordovaError = require('cordova-common').CordovaError;
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
 var events = require('cordova-common').events;
-var shell = require('shelljs');
 var Q = require('q');
 var path = require('path');
 var fs = require('fs');
@@ -50,7 +49,7 @@ function add (projectRoot, hooksRunner, opts) {
     if (!opts.plugins || !opts.plugins.length) {
         return Q.reject(new CordovaError('No plugin specified. Please specify a plugin to add. See `' + cordova_util.binname + ' plugin search`.'));
     }
-
+    var pluginInfo;
     var shouldRunPrepare = false;
     var pluginPath = path.join(projectRoot, 'plugins');
     var platformList = cordova_util.listPlatforms(projectRoot);
@@ -98,9 +97,11 @@ function add (projectRoot, hooksRunner, opts) {
                     });
                 }).then(function (directory) {
                     return pluginInfoProvider.get(directory);
-                }).then(function (pluginInfo) {
-                    
-                    plugin_util.mergeVariables(pluginInfo, cfg, opts);
+                }).then(function (plugInfoProvider) {
+                    pluginInfo = plugInfoProvider;
+                    return plugin_util.mergeVariables(pluginInfo, cfg, opts);
+                }).then(function (variables) {
+                    opts.cli_variables = variables;
 
                     // Iterate (in serial!) over all platforms in the project and install the plugin.
                     return chainMap(platformList, function (platform) {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/263502da/src/cordova/plugin/remove.js
----------------------------------------------------------------------
diff --git a/src/cordova/plugin/remove.js b/src/cordova/plugin/remove.js
index 76ad091..6f45dbd 100644
--- a/src/cordova/plugin/remove.js
+++ b/src/cordova/plugin/remove.js
@@ -49,7 +49,7 @@ function remove (projectRoot, targets, hooksRunner, opts) {
     return hooksRunner.fire('before_plugin_rm', opts)
         .then(function () {
             var pluginInfoProvider = new PluginInfoProvider();
-            var cli_variables;
+            var platformRoot;
             return opts.plugins.reduce(function (soFar, target) {
                 var validatedPluginId = module.exports.validatePluginId(target, plugins);
                 if (!validatedPluginId) {
@@ -63,15 +63,15 @@ function remove (projectRoot, targets, hooksRunner, opts) {
                 // reference from the platform's plugin config JSON.
                 return platformList.reduce(function (soFar, platform) {
                     return soFar.then(function () {
-                        var platformRoot = path.join(projectRoot, 'platforms', platform);
+                        platformRoot = path.join(projectRoot, 'platforms', platform);
                         var directory = path.join(pluginPath, target);
                         var pluginInfo = pluginInfoProvider.get(directory);
                         events.emit('verbose', 'Calling plugman.uninstall on plugin "' + target + '" for platform "' + platform + '"');
                         opts.force = opts.force || false;
-                        cli_variables = opts.cli_variables || {};
-
-                        plugin_util.mergeVariables(pluginInfo, cfg, opts);
 
+                        return plugin_util.mergeVariables(pluginInfo, cfg, opts);
+                    }).then(function (variables) {
+                        opts.cli_variables = variables;
                         return plugman.uninstall.uninstallPlatform(platform, platformRoot, target, pluginPath, opts)
                             .then(function (didPrepare) {
                                 // If platform does not returned anything we'll need

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/263502da/src/cordova/plugin/util.js
----------------------------------------------------------------------
diff --git a/src/cordova/plugin/util.js b/src/cordova/plugin/util.js
index d6bfeec..aaf7068 100644
--- a/src/cordova/plugin/util.js
+++ b/src/cordova/plugin/util.js
@@ -19,6 +19,10 @@
 
 var path = require('path');
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
+var shell = require('shelljs');
+var events = require('cordova-common').events;
+var Q = require('q');
+var CordovaError = require('cordova-common').CordovaError;
 
 module.exports.saveToConfigXmlOn = saveToConfigXmlOn;
 module.exports.getInstalledPlugins = getInstalledPlugins;
@@ -37,8 +41,16 @@ function saveToConfigXmlOn (config_json, options) {
     return autosave || options.save;
 }
 
-// merges cli variables and config.xml (cfg) variables
-// used when adding and removing
+/*
+ * Merges cli and config.xml variables.
+ *
+ * @param   {object}    pluginInfo
+ * @param   {object}    config.xml
+ * @param   {object}    options
+ *
+ * @return  {object}    object containing the new merged variables
+ */
+
 function mergeVariables (pluginInfo, cfg, opts) {
     // Validate top-level required variables
     var pluginVariables = pluginInfo.getPreferences();
@@ -62,5 +74,5 @@ function mergeVariables (pluginInfo, cfg, opts) {
         var msg = 'Variable(s) missing (use: --variable ' + missingVariables.join('=value --variable ') + '=value).';
         return Q.reject(new CordovaError(msg));
     }
-
+    return opts.cli_variables;
 }

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/263502da/src/plugman/install.js
----------------------------------------------------------------------
diff --git a/src/plugman/install.js b/src/plugman/install.js
index c70a99c..51c6baf 100644
--- a/src/plugman/install.js
+++ b/src/plugman/install.js
@@ -39,6 +39,7 @@ var cordovaUtil = require('../cordova/util');
 var superspawn = require('cordova-common').superspawn;
 var PluginInfo = require('cordova-common').PluginInfo;
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
+var variableMerge = require('../plugman/variable-merge');
 
 /* INSTALL FLOW
    ------------
@@ -311,27 +312,7 @@ function runInstall (actions, platform, project_dir, plugin_dir, plugins_dir, op
     }).then(function (engines) {
         return checkEngines(engines);
     }).then(function () {
-        var prefs = pluginInfo.getPreferences(platform);
-        var keys = underscore.keys(prefs);
-
-        options.cli_variables = options.cli_variables || {};
-        var missing_vars = underscore.difference(keys, Object.keys(options.cli_variables));
-
-        underscore.each(missing_vars, function (_key) {
-            var def = prefs[_key];
-            if (def) {
-                options.cli_variables[_key] = def;
-            }
-        });
-
-        // test missing vars once again after having default
-        missing_vars = underscore.difference(keys, Object.keys(options.cli_variables));
-
-        if (missing_vars.length > 0) {
-            throw new Error('Variable(s) missing: ' + missing_vars.join(', '));
-        }
-
-        filtered_variables = underscore.pick(options.cli_variables, keys);
+        filtered_variables = variableMerge.mergeVariables(plugin_dir, platform, options);
         install.filtered_variables = filtered_variables;
 
         // Check for dependencies

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/263502da/src/plugman/uninstall.js
----------------------------------------------------------------------
diff --git a/src/plugman/uninstall.js b/src/plugman/uninstall.js
index 7e6874b..562636e 100644
--- a/src/plugman/uninstall.js
+++ b/src/plugman/uninstall.js
@@ -36,6 +36,7 @@ var npmUninstall = require('cordova-fetch').uninstall;
 var superspawn = require('cordova-common').superspawn;
 var PlatformJson = require('cordova-common').PlatformJson;
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
+var variableMerge = require('../plugman/variable-merge');
 
 // possible options: cli_variables, www_dir
 // Returns a promise.
@@ -243,6 +244,9 @@ function runUninstallPlatform (actions, platform, project_dir, plugin_dir, plugi
     var pluginInfo = pluginInfoProvider.get(plugin_dir);
     var plugin_id = pluginInfo.id;
 
+    // Merge cli_variables and plugin.xml variables
+    var variables = variableMerge.mergeVariables(plugin_dir, platform, options); // eslint-disable-line
+
     // Deps info can be passed recusively
     var platformJson = PlatformJson.load(plugins_dir, platform);
     var depsInfo = options.depsInfo || dependencies.generateDependencyInfo(platformJson, plugins_dir, pluginInfoProvider);
@@ -300,6 +304,7 @@ function runUninstallPlatform (actions, platform, project_dir, plugin_dir, plugi
         // platform inside of the existing CLI project. This option is usually set by cordova-lib for CLI projects
         // but since we're running this code through plugman, we need to set it here implicitly
         options.usePlatformWww = true;
+        options.cli_variables = variables;
 
         var hooksRunner = new HooksRunner(projectRoot);
 

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/263502da/src/plugman/variable-merge.js
----------------------------------------------------------------------
diff --git a/src/plugman/variable-merge.js b/src/plugman/variable-merge.js
new file mode 100644
index 0000000..ea01722
--- /dev/null
+++ b/src/plugman/variable-merge.js
@@ -0,0 +1,63 @@
+/**
+    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 PluginInfoProvider = require('cordova-common').PluginInfoProvider;
+var underscore = require('underscore');
+
+module.exports.mergeVariables = mergeVariables;
+
+/*
+ * At this point, cli and config vars have already merged.
+ * Merges those vars (cli and config) with plugin.xml variables.
+ *
+ * @param   {string}    plugin directory
+ * @param   {string}    platform
+ * @param   {object}    options
+ *
+ * @return  {object}    list of filtered variables
+ */
+function mergeVariables (plugin_dir, platform, options) {
+    options.pluginInfoProvider = options.pluginInfoProvider || new PluginInfoProvider();
+    var pluginInfoProvider = options.pluginInfoProvider;
+    var pluginInfo = pluginInfoProvider.get(plugin_dir);
+    var filtered_variables = {};
+
+    var prefs = pluginInfo.getPreferences(platform);
+    var keys = underscore.keys(prefs);
+
+    options.cli_variables = options.cli_variables || {};
+    var missing_vars = underscore.difference(keys, Object.keys(options.cli_variables));
+
+    underscore.each(missing_vars, function (_key) {
+        var def = prefs[_key];
+        if (def) {
+            options.cli_variables[_key] = def;
+        }
+    });
+
+    // test missing vars once again after having default
+    missing_vars = underscore.difference(keys, Object.keys(options.cli_variables));
+
+    if (missing_vars.length > 0) {
+        throw new Error('Variable(s) missing: ' + missing_vars.join(', '));
+    }
+
+    filtered_variables = underscore.pick(options.cli_variables, keys);
+    return filtered_variables;
+}


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