cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cordova-lib git commit: CB-9162 Adds support for default values for plugin variables.
Date Tue, 16 Jun 2015 20:04:10 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master 52c0fef78 -> e4e590461


CB-9162 Adds support for default values for plugin variables.

This closes #247


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

Branch: refs/heads/master
Commit: e4e5904619bab05705d62bce92a4c4cd0d45bb82
Parents: 52c0fef
Author: Olivier Goguel <ogoguel@MacBook-Pro-de-Olivier-BTO.local>
Authored: Fri Jun 12 08:57:02 2015 +0200
Committer: Tim Barham <tim.barham@microsoft.com>
Committed: Tue Jun 16 13:02:56 2015 -0700

----------------------------------------------------------------------
 .../org.test.defaultvariables/plugin.xml        | 19 +++++++++++++++++
 cordova-lib/spec-cordova/plugin.spec.js         |  8 +++++++
 .../spec-plugman/install-browserify.spec.js     | 12 ++++++++++-
 cordova-lib/spec-plugman/install.spec.js        |  7 ++++++-
 .../org.test.defaultvariables/plugin.xml        | 16 ++++++++++++++
 cordova-lib/src/PluginInfo.js                   | 15 ++++++++++---
 cordova-lib/src/cordova/plugin.js               | 20 ++++++++++++++----
 cordova-lib/src/plugman/install.js              | 22 +++++++++++++++-----
 8 files changed, 105 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/spec-cordova/fixtures/plugins/org.test.defaultvariables/plugin.xml
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/fixtures/plugins/org.test.defaultvariables/plugin.xml
b/cordova-lib/spec-cordova/fixtures/plugins/org.test.defaultvariables/plugin.xml
new file mode 100644
index 0000000..4aea92c
--- /dev/null
+++ b/cordova-lib/spec-cordova/fixtures/plugins/org.test.defaultvariables/plugin.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    id="org.test.defaultvariables"
+    version="3.0.0">
+
+    <name>Test plugin variables</name>
+
+    <preference name="DEFAULT" default="yes" />
+    <preference name="REQUIRED" />
+
+    <!-- android -->
+    <platform name="android">
+        <preference name="DEFAULT_ANDROID" default="yes" />
+        <preference name="REQUIRED_ANDROID" />
+    </platform>
+
+</plugin>

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/spec-cordova/plugin.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/plugin.spec.js b/cordova-lib/spec-cordova/plugin.spec.js
index 87a1a43..4593bfd 100644
--- a/cordova-lib/spec-cordova/plugin.spec.js
+++ b/cordova-lib/spec-cordova/plugin.spec.js
@@ -27,6 +27,7 @@ var tmpDir = helpers.tmpDir('plugin_test');
 var project = path.join(tmpDir, 'project');
 var pluginsDir = path.join(__dirname, 'fixtures', 'plugins');
 var pluginId = 'org.apache.cordova.fakeplugin1';
+var org_test_defaultvariables = 'org.test.defaultvariables';
 
 describe('plugin end-to-end', function() {
     var results;
@@ -75,6 +76,13 @@ describe('plugin end-to-end', function() {
             return cordova.raw.plugin('ls');
         }).then(function() {
             expect(results).toMatch(/No plugins added/gi);
+        }).then(function() {
+            // Testing Default Variables plugin
+            return cordova.raw.plugin('add', path.join(pluginsDir, org_test_defaultvariables),{cli_variables:
{ REQUIRED:'yes', REQUIRED_ANDROID:'yes'}});
+         }).then(function() {
+            return cordova.raw.plugin('ls');
+        }).then(function() {
+            expect(results).toContain(org_test_defaultvariables);
         }).fail(function(err) {
             console.log(err.stack);
             expect(err).toBeUndefined();

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/spec-plugman/install-browserify.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-plugman/install-browserify.spec.js b/cordova-lib/spec-plugman/install-browserify.spec.js
index 39df471..e236779 100644
--- a/cordova-lib/spec-plugman/install-browserify.spec.js
+++ b/cordova-lib/spec-plugman/install-browserify.spec.js
@@ -46,6 +46,7 @@ var install = require('../src/plugman/install'),
         'com.cordova.engine-android' : path.join(plugins_dir, 'com.cordova.engine-android'),
         'org.test.plugins.childbrowser' : path.join(plugins_dir, 'org.test.plugins.childbrowser'),
         'com.adobe.vars' : path.join(plugins_dir, 'com.adobe.vars'),
+        'org.test.defaultvariables' : path.join(plugins_dir, 'org.test.defaultvariables'),
         'A' : path.join(plugins_dir, 'dependencies', 'A'),
         'B' : path.join(plugins_dir, 'dependencies', 'B'),
         'C' : path.join(plugins_dir, 'dependencies', 'C'),
@@ -143,7 +144,7 @@ describe('start', function() {
             }
         ).fail(
             function(error) {
-                expect(error).toBeUndefined();
+                expect(error).toEqual({});
             }
         );
         waitsFor(function() { return done; }, 'promise never resolved', 2000);
@@ -458,6 +459,15 @@ describe('install', function() {
                 expect(''+done).toContain('Variable(s) missing: API_KEY');
             });
         });
+         it('should not throw exception on default variables', function() {
+            runs(function() {
+                installPromise( install('android', project, plugins['org.test.defaultvariables'],
plugins_install_dir, { browserify: true , cli_variables:{API_KEY:'del7a' }}) );
+            });
+            waitsFor(function() { return done; }, 'install promise never resolved', 200);
+            runs(function() {
+                expect(''+done).toEqual('true');
+            });
+        });
         it('should throw if git is not found on the path and a remote url is requested',
function() {
             spyOn(fs, 'existsSync').andCallFake( fake['existsSync']['noPlugins'] );
             fetchSpy.andCallThrough();

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/spec-plugman/install.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-plugman/install.spec.js b/cordova-lib/spec-plugman/install.spec.js
index 67bc543..751268f 100644
--- a/cordova-lib/spec-plugman/install.spec.js
+++ b/cordova-lib/spec-plugman/install.spec.js
@@ -46,6 +46,7 @@ var install = require('../src/plugman/install'),
         'com.cordova.engine-android' : path.join(plugins_dir, 'com.cordova.engine-android'),
         'org.test.plugins.childbrowser' : path.join(plugins_dir, 'org.test.plugins.childbrowser'),
         'com.adobe.vars' : path.join(plugins_dir, 'com.adobe.vars'),
+        'org.test.defaultvariables' : path.join(plugins_dir, 'org.test.defaultvariables'),
         'A' : path.join(plugins_dir, 'dependencies', 'A'),
         'B' : path.join(plugins_dir, 'dependencies', 'B'),
         'C' : path.join(plugins_dir, 'dependencies', 'C'),
@@ -128,6 +129,10 @@ describe('start', function() {
             }
         ).then(
             function(){
+                return install('android', project, plugins['org.test.defaultvariables'],
plugins_install_dir, { cli_variables:{API_KEY:'batman'} });
+            }
+        ).then(
+            function(){
                 done = true;
                 results['prepareCount'] = prepare.callCount;
                 results['emit_results'] = [];
@@ -174,7 +179,7 @@ describe('install', function() {
 
     describe('success', function() {
         it('should call prepare after a successful install', function() {
-           expect(results['prepareCount']).toBe(4);
+           expect(results['prepareCount']).toBe(5);
         });
 
         it('should emit a results event with platform-agnostic <info>', function()
{

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/spec-plugman/plugins/org.test.defaultvariables/plugin.xml
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-plugman/plugins/org.test.defaultvariables/plugin.xml b/cordova-lib/spec-plugman/plugins/org.test.defaultvariables/plugin.xml
new file mode 100644
index 0000000..8d27d3d
--- /dev/null
+++ b/cordova-lib/spec-plugman/plugins/org.test.defaultvariables/plugin.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    id="org.test.defaultvariables"
+    version="3.0.0">
+
+    <name>Use Default Variables</name>
+
+    <!-- android -->
+    <platform name="android">
+            <preference name="ENABLE_LOG" default="yes" />
+            <preference name="API_KEY" />
+    </platform>
+
+</plugin>

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/src/PluginInfo.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/PluginInfo.js b/cordova-lib/src/PluginInfo.js
index 3190d95..08b0a97 100644
--- a/cordova-lib/src/PluginInfo.js
+++ b/cordova-lib/src/PluginInfo.js
@@ -45,13 +45,22 @@ function PluginInfo(dirname) {
     // Used to require a variable to be specified via --variable when installing the plugin.
     self.getPreferences = getPreferences;
     function getPreferences(platform) {
-        var prefs = _getTags(self._et, 'preference', platform, _parsePreference);
+        var arprefs = _getTags(self._et, 'preference', platform, _parsePreference);
+
+        var prefs= {};
+        for(var i in arprefs)
+        {
+            var pref=arprefs[i];
+            prefs[pref.preference]=pref.default;
+        }
+        // returns { key : default | null}
         return prefs;
     }
 
     function _parsePreference(prefTag) {
-        var pref = prefTag.attrib.name.toUpperCase();
-        return pref;
+        var name = prefTag.attrib.name.toUpperCase();
+        var def = prefTag.attrib.default || null;
+        return {preference: name, default: def};
     }
 
     // <asset>

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/src/cordova/plugin.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/plugin.js b/cordova-lib/src/cordova/plugin.js
index 785a9c3..0f90de2 100644
--- a/cordova-lib/src/cordova/plugin.js
+++ b/cordova-lib/src/cordova/plugin.js
@@ -166,10 +166,22 @@ module.exports = function plugin(command, targets, opts) {
                     })
                     .then(function(dir) {
                         // Validate top-level required variables
-                        var pluginVariables = pluginInfoProvider.get(dir).getPreferences(),
-                            missingVariables = pluginVariables.filter(function (v) {
-                                return !(v in opts.cli_variables);
-                            });
+                        var pluginVariables = pluginInfoProvider.get(dir).getPreferences();
+                        var requiredVariables = [];
+
+                        for(var i in pluginVariables) {
+                            var v = pluginVariables[i];
+                            // discard variables with default value
+                            if (!v) {
+                                requiredVariables.push(i);
+                            }
+                        }
+
+                        opts.cli_variables = opts.cli_variables || {}; 
+                        var missingVariables = requiredVariables.filter(function (v) {
+                            return !(v in opts.cli_variables);
+                        });
+
                         if (missingVariables.length) {
                             shell.rm('-rf', dir);
                             var msg = 'Variable(s) missing (use: --variable ' + missingVariables.join('=value
--variable ') + '=value).';

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e4e59046/cordova-lib/src/plugman/install.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/plugman/install.js b/cordova-lib/src/plugman/install.js
index 80ee2d8..5c20381 100644
--- a/cordova-lib/src/plugman/install.js
+++ b/cordova-lib/src/plugman/install.js
@@ -295,17 +295,29 @@ function runInstall(actions, platform, project_dir, plugin_dir, plugins_dir,
opt
     }).then(function(engines) {
         return checkEngines(engines);
     }).then(function() {
-            // checking preferences, if certain variables are not provided, we should throw.
             var prefs = pluginInfo.getPreferences(platform);
+            var keys = underscore.keys(prefs);
+
             options.cli_variables = options.cli_variables || {};
-            filtered_variables = underscore.pick(options.cli_variables, prefs);
-            var missing_vars = underscore.difference(prefs, Object.keys(options.cli_variables));
-            install.filtered_variables = filtered_variables;
+            var missing_vars = underscore.difference(keys, Object.keys(options.cli_variables));
+
+            underscore.each(missing_vars,function(_key) {
+                var def = prefs[_key];
+                if (def)
+                     // adding default variables
+                    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);
+            install.filtered_variables = filtered_variables;
+
             // Check for dependencies
             var dependencies = pluginInfo.getDependencies(platform);
             if(dependencies.length) {
@@ -559,7 +571,7 @@ function handleInstall(actions, pluginInfo, platform, project_dir, plugins_dir,
         } else {
             return plugman.prepare(project_dir, platform, plugins_dir, options.www_dir, options.is_top_level,
options.pluginInfoProvider);
         }
-	}).then (function() {
+    }).then (function() {
         events.emit('verbose', 'Install complete for ' + pluginInfo.id + ' on ' + platform
+ '.');
 
         if (platform == 'android' && semver.gte(options.platformVersion, '4.0.0-dev')
&& frameworkFiles.length > 0) {


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


Mime
View raw message