cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ger...@apache.org
Subject cordova-lib git commit: CB-8799 Save plugin/platform src and version to 'spec' attribute.
Date Thu, 09 Apr 2015 20:16:09 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master 87c9f230b -> a7e71227d


CB-8799 Save plugin/platform src and version to 'spec' attribute.

When --save option is used, plugin and platform source location or version value is saved
to 'spec' attribute in config.xml (source location takes priority over version). This replaces
'version' attribute for platforms, and 'src' and 'version' attributes for plugins.

Also:

* Updated semver dependency to 2.1.0 to support tilde range.
* Ensure when save platform version (to platforms.json and config.xml) we save the version
that was actually installed.
* Ensure when save platform version to config.xml we save it as a tilde range.

github: close #202


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

Branch: refs/heads/master
Commit: a7e71227d2157e677c3ce495341c83916f9b272f
Parents: 87c9f23
Author: Tim Barham <tim.barham@microsoft.com>
Authored: Mon Apr 6 23:42:29 2015 +1000
Committer: Gorkem Ercan <gorkem.ercan@gmail.com>
Committed: Thu Apr 9 15:25:21 2015 -0400

----------------------------------------------------------------------
 cordova-lib/package.json                        |   2 +-
 cordova-lib/spec-cordova/ConfigParser.spec.js   |  36 +++++--
 .../Projects/ProjectMetadata/config.xml         |  11 +-
 .../spec-cordova/project-metadata-apis.spec.js  |  24 ++++-
 cordova-lib/spec-cordova/test-config.xml        |  13 ++-
 cordova-lib/src/configparser/ConfigParser.js    |  72 +++++++------
 cordova-lib/src/cordova/platform.js             |  45 ++++----
 cordova-lib/src/cordova/plugin.js               | 105 +++++++++++++------
 cordova-lib/src/cordova/project_metadata.js     |  28 +++--
 cordova-lib/src/cordova/restore-util.js         |  31 +++---
 10 files changed, 239 insertions(+), 128 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/package.json
----------------------------------------------------------------------
diff --git a/cordova-lib/package.json b/cordova-lib/package.json
index 41b15ec..1ab74e8 100644
--- a/cordova-lib/package.json
+++ b/cordova-lib/package.json
@@ -35,7 +35,7 @@
     "q": "1.0.1",
     "rc": "0.5.2",
     "request": "2.47.0",
-    "semver": "2.0.11",
+    "semver": "2.1.0",
     "shelljs": "0.3.0",
     "tar": "1.0.2",
     "through2": "0.6.3",

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/spec-cordova/ConfigParser.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/ConfigParser.spec.js b/cordova-lib/spec-cordova/ConfigParser.spec.js
index f8813e7..f3d0b8f 100644
--- a/cordova-lib/spec-cordova/ConfigParser.spec.js
+++ b/cordova-lib/spec-cordova/ConfigParser.spec.js
@@ -122,7 +122,9 @@ describe('config.xml parser', function () {
                    'org.apache.cordova.pluginwithversion',
                    'org.apache.cordova.pluginwithurlandversion',
                    'org.apache.cordova.justaplugin',
-                   'org.apache.cordova.legacyfeature'
+                   'org.apache.cordova.legacyfeatureversion',
+                   'org.apache.cordova.legacyfeatureurl',
+                   'org.apache.cordova.legacyfeatureversionandurl'
                ];
                var list = cfg.getPluginIdList();
                expect(list.length).toEqual(expectedList.length);
@@ -140,13 +142,17 @@ describe('config.xml parser', function () {
                 var plugin = cfg.getPlugin('org.apache.cordova.undefinedplugin');
                 expect(plugin).not.toBeDefined();
             });
-            it('should read plugin with src', function(){
+            it('should read plugin with src and store it in spec field', function(){
                 var plugin = cfg.getPlugin('org.apache.cordova.pluginwithurl');
-                expect(plugin.src).toEqual('http://cordova.apache.org/pluginwithurl');
+                expect(plugin.spec).toEqual('http://cordova.apache.org/pluginwithurl');
             });
-            it('should read plugin with version', function(){
+            it('should read plugin with version and store it in spec field', function(){
                 var plugin = cfg.getPlugin('org.apache.cordova.pluginwithversion');
-                expect(plugin.version).toEqual('1.1.1');
+                expect(plugin.spec).toEqual('1.1.1');
+            });
+            it('should read plugin with source and version and store source in spec field',
function(){
+                var plugin = cfg.getPlugin('org.apache.cordova.pluginwithurlandversion');
+                expect(plugin.spec).toEqual('http://cordova.apache.org/pluginwithurlandversion');
             });
             it('should read plugin variables', function () {
                 var plugin = cfg.getPlugin('org.apache.cordova.pluginwithvars');
@@ -179,14 +185,26 @@ describe('config.xml parser', function () {
                 expect(variables[0].attrib.name).toEqual('paraname');
                 expect(variables[0].attrib.value).toEqual('paravalue');
             });
-            it('should be able to read legacy feature entries', function(){
-                var plugin = cfg.getPlugin('org.apache.cordova.legacyfeature');
+            it('should be able to read legacy feature entries with a version', function(){
+                var plugin = cfg.getPlugin('org.apache.cordova.legacyfeatureversion');
                 expect(plugin).toBeDefined();
-                expect(plugin.name).toEqual('org.apache.cordova.legacyfeature');
-                expect(plugin.version).toEqual('1.2.3');
+                expect(plugin.name).toEqual('org.apache.cordova.legacyfeatureversion');
+                expect(plugin.spec).toEqual('1.2.3');
                 expect(plugin.variables).toBeDefined();
                 expect(plugin.variables.aVar).toEqual('aValue');
             });
+            it('should be able to read legacy feature entries with a url', function(){
+                var plugin = cfg.getPlugin('org.apache.cordova.legacyfeatureurl');
+                expect(plugin).toBeDefined();
+                expect(plugin.name).toEqual('org.apache.cordova.legacyfeatureurl');
+                expect(plugin.spec).toEqual('http://cordova.apache.org/legacyfeatureurl');
+            });
+            it('should be able to read legacy feature entries with a version and a url',
function(){
+                var plugin = cfg.getPlugin('org.apache.cordova.legacyfeatureversionandurl');
+                expect(plugin).toBeDefined();
+                expect(plugin.name).toEqual('org.apache.cordova.legacyfeatureversionandurl');
+                expect(plugin.spec).toEqual('http://cordova.apache.org/legacyfeatureversionandurl');
+            });
             it('it should remove given plugin', function(){
                 cfg.removePlugin('org.apache.cordova.justaplugin');
                 var plugins = cfg.doc.findall('plugin');

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/spec-cordova/Projects/ProjectMetadata/config.xml
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/Projects/ProjectMetadata/config.xml b/cordova-lib/spec-cordova/Projects/ProjectMetadata/config.xml
index f3efab4..a7df1ec 100644
--- a/cordova-lib/spec-cordova/Projects/ProjectMetadata/config.xml
+++ b/cordova-lib/spec-cordova/Projects/ProjectMetadata/config.xml
@@ -22,10 +22,11 @@
         <allow-intent href="itms:*" />
         <allow-intent href="itms-apps:*" />
     </platform>
-    <engine name="android" version="3.7.1" />
-    <engine name="browser" version="https://github.com/apache/cordova-browser.git" />
-    <plugin name="org.apache.cordova.device" version="0.3.0" />
-    <plugin name="org.apache.cordova.camera" src="https://github.com/apache/cordova-plugin-camera.git">
+    <engine name="android" version="3.7.1" /> <!-- this uses deprecated version
attribute-->
+    <engine name="browser" spec="https://github.com/apache/cordova-browser.git" />
<!-- this uses current spec attribute -->
+    <plugin name="org.apache.cordova.device" spec="0.3.0" /> <!-- this uses current
spec attribute -->
+    <plugin name="org.apache.cordova.camera" src="https://github.com/apache/cordova-plugin-camera.git">
<!-- this uses deprecated src attribute-->
         <variable name="TEST_VARIABLE" value="My Test Variable" />
-	</plugin>
+    </plugin>
+    <plugin name="org.apache.cordova.file" src="https://github.com/apache/cordova-plugin-file.git"
version="1.2.3" /> <!-- this uses outdated src and version attributes -->
 </widget>

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/spec-cordova/project-metadata-apis.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/project-metadata-apis.spec.js b/cordova-lib/spec-cordova/project-metadata-apis.spec.js
index 47ad74d..ae33c50 100644
--- a/cordova-lib/spec-cordova/project-metadata-apis.spec.js
+++ b/cordova-lib/spec-cordova/project-metadata-apis.spec.js
@@ -31,13 +31,13 @@ describe('retrieval of project metadata', function () {
             .then(function (platforms) {
                 expect(platforms.length).toBe(2);
 
-                // Android platform has version defined, not source
+                // Android platform has version defined in deprecated version field - should
still work.
                 var androidPlatform = findPlatform(platforms, 'android');
                 expect(androidPlatform).not.toBeNull();
                 expect(androidPlatform.version).toBe(androidVersion);
                 expect(androidPlatform.src).toBeUndefined();
 
-                // Browser platform has source defined in the version field
+                // Browser platform has source defined in the spec field.
                 var browserPlatform = findPlatform(platforms, 'browser');
                 expect(browserPlatform).not.toBeNull();
                 expect(browserPlatform.version).toBeUndefined();
@@ -54,11 +54,14 @@ describe('retrieval of project metadata', function () {
         var cameraVariableName = 'TEST_VARIABLE';
         var cameraVariableValue = 'My Test Variable';
 
+        var fileId = 'org.apache.cordova.file';
+        var fileSource = 'https://github.com/apache/cordova-plugin-file.git';
+
         cordova.raw.projectMetadata.getPlugins(projectRoot)
             .then(function (plugins) {
-                expect(plugins.length).toBe(2);
+                expect(plugins.length).toBe(3);
 
-                // Device plugin
+                // Device plugin uses current spec attribute to specify version - should
be returned in version field.
                 var devicePlugin = findPlugin(plugins, deviceId);
                 expect(devicePlugin).not.toBeNull();
                 expect(devicePlugin.version).toBe(deviceVersion);
@@ -69,7 +72,7 @@ describe('retrieval of project metadata', function () {
                 expect(Array.isArray(deviceVariables)).toBeTruthy();
                 expect(deviceVariables.length).toBe(0);
 
-                // Camera plugin
+                // Camera plugin uses deprecated src attribute - still should work.
                 var cameraPlugin = findPlugin(plugins, cameraId);
                 expect(cameraPlugin).not.toBeNull();
                 expect(cameraPlugin.src).toBe(cameraSrc);
@@ -80,6 +83,17 @@ describe('retrieval of project metadata', function () {
                 expect(cameraVariables.length).toBe(1);
                 expect(cameraVariables[0].name).toBe(cameraVariableName);
                 expect(cameraVariables[0].value).toBe(cameraVariableValue);
+
+                // File plugin uses deprecated src and version attributes - version should
be ignored.
+                var filePlugin = findPlugin(plugins, fileId);
+                expect(filePlugin).not.toBeNull();
+                expect(filePlugin.version).toBeUndefined();
+                expect(filePlugin.src).toBe(fileSource);
+
+                var fileVariables = filePlugin.variables;
+                expect(fileVariables).not.toBeNull();
+                expect(Array.isArray(fileVariables)).toBeTruthy();
+                expect(fileVariables.length).toBe(0);
             }).finally(done);
     });
 });

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/spec-cordova/test-config.xml
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/test-config.xml b/cordova-lib/spec-cordova/test-config.xml
index e092cf0..ac22580 100644
--- a/cordova-lib/spec-cordova/test-config.xml
+++ b/cordova-lib/spec-cordova/test-config.xml
@@ -26,9 +26,18 @@
     <plugin name="org.apache.cordova.pluginwithversion" version="1.1.1" />
     <plugin name="org.apache.cordova.pluginwithurlandversion" src="http://cordova.apache.org/pluginwithurlandversion"
version="1.1.1" />
     <plugin name="org.apache.cordova.justaplugin" />
-    <feature name="Legacy plugin entry">
-        <param name="id" value="org.apache.cordova.legacyfeature" />
+    <feature name="Legacy plugin entry with version">
+        <param name="id" value="org.apache.cordova.legacyfeatureversion" />
         <param name="version" value="1.2.3" />
         <param name="aVar" value="aValue" />
     </feature>
+    <feature name="Legacy plugin entry with url">
+        <param name="id" value="org.apache.cordova.legacyfeatureurl" />
+        <param name="url" value="http://cordova.apache.org/legacyfeatureurl" />
+    </feature>
+    <feature name="Legacy plugin entry with version and url">
+        <param name="id" value="org.apache.cordova.legacyfeatureversionandurl" />
+        <param name="version" value="1.2.3" />
+        <param name="url" value="http://cordova.apache.org/legacyfeatureversionandurl"
/>
+    </feature>
 </widget>

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/src/configparser/ConfigParser.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/configparser/ConfigParser.js b/cordova-lib/src/configparser/ConfigParser.js
index 1779cb2..68b04ac 100644
--- a/cordova-lib/src/configparser/ConfigParser.js
+++ b/cordova-lib/src/configparser/ConfigParser.js
@@ -22,7 +22,8 @@
 var et = require('elementtree'),
     xml= require('../util/xml-helpers'),
     CordovaError = require('../CordovaError'),
-    fs = require('fs');
+    fs = require('fs'),
+    events = require('../events');
 
 
 /** Wraps a config.xml file */
@@ -299,24 +300,21 @@ ConfigParser.prototype = {
      * Adds a plugin element. Does not check for duplicates.
      * @name addPlugin
      * @function
-     * @param {object} attributes name, version and src are supported
+     * @param {object} attributes name and spec are supported
      * @param {Array} variables name, value
      */
-    addPlugin: function(attributes, variables){
-        if ( !attributes && !attributes.name ) return;
+    addPlugin: function (attributes, variables) {
+        if (!attributes && !attributes.name) return;
         var el = new et.Element('plugin');
-        el.attrib.name =attributes.name;
-        if ( attributes.version) {
-            el.attrib.version =attributes.version;
+        el.attrib.name = attributes.name;
+        if (attributes.spec) {
+            el.attrib.spec = attributes.spec;
         }
-        if ( attributes.src){
-            el.attrib.src = attributes.src;
-        }
-        if(variables){
-            variables.forEach(function(variable){
+        if (variables) {
+            variables.forEach(function (variable) {
                 var v = new et.Element('variable');
-                v.attrib.name=variable.name;
-                v.attrib.value=variable.value;
+                v.attrib.name = variable.name;
+                v.attrib.value = variable.value;
                 el.append(v);
             });
         }
@@ -340,16 +338,15 @@ ConfigParser.prototype = {
         if (null === pluginElement) {
             var legacyFeature =  this.doc.find('./feature/param[@name="id"][@value="' + id
+ '"]/..');
             if(legacyFeature){
-                 var events = require('../events');
                  events.emit('log', 'Found deprecated feature entry for ' + id +' in config.xml.');
                 return featureToPlugin(legacyFeature);
             }
             return undefined;
         }
         var plugin = {};
+
         plugin.name = pluginElement.attrib.name;
-        plugin.version = pluginElement.attrib.version;
-        plugin.src = pluginElement.attrib.src;
+        plugin.spec = pluginElement.attrib.spec || pluginElement.attrib.src || pluginElement.attrib.version;
         plugin.variables = {};
         var variableElements = pluginElement.findall('variable');
         variableElements.forEach(function(varElement){
@@ -396,14 +393,14 @@ ConfigParser.prototype = {
     /**
      * Adds an engine. Does not check for duplicates.
      * @param  {String} name the engine name
-     * @param  {String} version engine version (optional)
+     * @param  {String} spec engine source location or version (optional)
      */
-    addEngine: function(name, version){
+    addEngine: function(name, spec){
         if(!name) return;
         var el = et.Element('engine');
         el.attrib.name = name;
-        if(version){
-            el.attrib.version = version;
+        if(spec){
+            el.attrib.spec = spec;
         }
         this.doc.getroot().append(el);
     },
@@ -424,10 +421,10 @@ ConfigParser.prototype = {
     getEngines: function(){
         var engines = this.doc.findall('./engine');
         return engines.map(function(engine){
-            var version = engine.attrib.version;
+            var spec = engine.attrib.spec || engine.attrib.version;
             return {
                 'name': engine.attrib.name,
-                'version': version ? version : null
+                'spec': spec ? spec : null
             };
         });
     },
@@ -436,25 +433,32 @@ ConfigParser.prototype = {
     }
 };
 
-function featureToPlugin(featureElement){
+function featureToPlugin(featureElement) {
     var plugin = {};
-    plugin.variables=[];
+    plugin.variables = [];
+    var pluginVersion,
+        pluginSrc;
+
     var nodes = featureElement.findall('param');
-    nodes.forEach(function(element){
+    nodes.forEach(function (element) {
         var n = element.attrib.name;
         var v = element.attrib.value;
-        if(n === 'id'){
+        if (n === 'id') {
             plugin.name = v;
-        }else
-        if(n === 'version'){
-            plugin.version = v;
-        }else
-        if(n === 'url' || n === 'installPath'){
-            plugin.src = v;
-        }else{
+        } else if (n === 'version') {
+            pluginVersion = v;
+        } else if (n === 'url' || n === 'installPath') {
+            pluginSrc = v;
+        } else {
             plugin.variables[n] = v;
         }
     });
+
+    var spec = pluginSrc || pluginVersion;
+    if (spec) {
+        plugin.spec = spec;
+    }
+
     return plugin;
 }
 module.exports = ConfigParser;

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index dd19950..fd49bd0 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -86,30 +86,32 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
             // For each platform, download it and call its helper script.
             var parts = target.split('@');
             var platform = parts[0];
-            var version = parts[1];
+            var spec = parts[1];
 
             return Q.when().then(function() {
                 if (!(platform in platforms)) {
-                    version = platform;
+                    spec = platform;
                     platform = null;
                 }
 
+                if (platform && !spec && cmd == 'add') {
+                    events.emit('verbose', 'No version supplied. Retrieving version from
config.xml...');
+                    spec = getVersionFromConfigFile(platform, cfg);
+                }
+
                 // If --save/autosave on && no version specified, use the pinned
version
                 // e.g: 'cordova platform add android --save', 'cordova platform update android
--save'
-                if( (opts.save || autosave) && !version ){
-                    version = platforms[platform].version;
-                }
-                if (platform && !version && cmd == 'add') {
-                    events.emit('verbose', 'No version supplied. Retrieving version from
config.xml...');
-                    version = getVersionFromConfigFile(platform, cfg);
+                if( (opts.save || autosave) && !spec ){
+                    spec = platforms[platform].version;
                 }
-                if (version) {
-                    var maybeDir = cordova_util.fixRelativePath(version);
+
+                if (spec) {
+                    var maybeDir = cordova_util.fixRelativePath(spec);
                     if (cordova_util.isDirectory(maybeDir)) {
                         return getPlatformDetailsFromDir(maybeDir, platform);
                     }
                 }
-                return downloadPlatform(projectRoot, platform, version, opts);
+                return downloadPlatform(projectRoot, platform, spec, opts);
             }).then(function(platDetails) {
                 platform = platDetails.platform;
                 var platformPath = path.join(projectRoot, 'platforms', platform);
@@ -165,18 +167,25 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                         return installPluginsForNewPlatform(platform, projectRoot, opts);
                     }
                 }).then(function() {
-                    // Save platform@version into platforms.json. i.e: 'android@https://github.com/apache/cordova-android.git'
-                    // If no version was specified, save the edge version
-                    var versionToSave = version || platforms[platform].version;
+                    var saveVersion = !spec || semver.validRange(spec, true);
+
+                    // Save platform@spec into platforms.json, where 'spec' is a version
or a soure location. If a
+                    // source location was specified, we always save that. Otherwise we save
the version that was
+                    // actually installed.
+                    var versionToSave = saveVersion ? platDetails.version : spec;
                     events.emit('verbose', 'saving ' + platform + '@' + versionToSave + '
into platforms.json');
                     platformMetadata.save(projectRoot, platform, versionToSave);
-                }).then(function() {
+
                     if(opts.save || autosave){
+                        // Similarly here, we save the source location if that was specified,
otherwise the version that
+                        // was installed. However, we save it with the "^" attribute.
+                        spec = saveVersion ? '^' + platDetails.version : spec;
+
                         // Save target into config.xml, overriding already existing settings
                         events.emit('log', '--save flag or autosave detected');
-                        events.emit('log', 'Saving ' + platform + '@' + version + ' into
config.xml file ...');
+                        events.emit('log', 'Saving ' + platform + '@' + spec + ' into config.xml
file ...');
                         cfg.removeEngine(platform);
-                        cfg.addEngine(platform, version);
+                        cfg.addEngine(platform, spec);
                         cfg.write();
                     }
                 });
@@ -281,7 +290,7 @@ function getVersionFromConfigFile(platform, cfg) {
         return eng.name.toLowerCase() === platform.toLowerCase();
     });
 
-    return engine && engine.version;
+    return engine && engine.spec;
 }
 
 function remove(hooksRunner, projectRoot, targets, opts) {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/src/cordova/plugin.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/plugin.js b/cordova-lib/src/cordova/plugin.js
index d9b5d18..632ce24 100644
--- a/cordova-lib/src/cordova/plugin.js
+++ b/cordova-lib/src/cordova/plugin.js
@@ -30,8 +30,7 @@ var cordova_util  = require('./util'),
     plugman       = require('../plugman/plugman'),
     pluginMapper  = require('cordova-registry-mapper').newToOld,
     events        = require('../events'),
-    metadata      = require('../plugman/util/metadata'),
-    _             = require('underscore');
+    metadata      = require('../plugman/util/metadata');
 
 // Returns a promise.
 module.exports = function plugin(command, targets, opts) {
@@ -148,18 +147,10 @@ module.exports = function plugin(command, targets, opts) {
 
                             var attributes = {};
                             attributes.name = pluginInfo.id;
-                            attributes.version = '^' + pluginInfo.version;
 
-                            var url = require('url');
-                            var uri = url.parse(target);
-                            if (uri.protocol && uri.protocol != 'file:' &&
uri.protocol[1] != ':' && !target.match(/^\w+:\\/)) {
-                                attributes.src = target;
-                            } else {
-                                var plugin_dir = cordova_util.fixRelativePath(path.join(target,
(opts.subdir || '.')));
-                                if (fs.existsSync(plugin_dir)) {
-                                    attributes.src = target;
-                                }
-                            }
+                            var src = parseSource(target, opts);
+                            attributes.spec = src ? src : '^' + pluginInfo.version;
+
                             var variables = [];
                             if (opts.cli_variables) {
                                 for (var varname in opts.cli_variables) {
@@ -335,26 +326,12 @@ function save(projectRoot, opts){
             return;
         }
 
-            
         var attribs = {name: pluginName};
+        var spec = getSpec(pluginSource, projectRoot, pluginName);
+        if (spec) {
+            attribs.spec = spec;
+        }
 
-        // Retrieve appropriate property: 'src' or 'version'
-        var src = (function(){
-            if(pluginSource.hasOwnProperty('url') || pluginSource.hasOwnProperty('path')){
-                return { src: (pluginSource.url || pluginSource.path) };
-            }
-            if(pluginSource.hasOwnProperty('id')){
-                var parts = pluginSource.id.split('@');
-                var version = parts[1];
-                if(version){
-                    return { version: version };
-                }
-            }
-            // If there's an id, but no version
-            // If there's no valid property('url', 'path', 'id')
-            return {};
-        }());
-        _.extend(attribs, src);
         var variables = getPluginVariables(plugin.variables);
         cfg.addPlugin(attribs, variables);
     });
@@ -368,7 +345,7 @@ function getPluginVariables(variables){
     if(!variables){
         return result;
     }
-    
+
     Object.keys(variables).forEach(function(pluginVar){
         result.push({name: pluginVar, value: variables[pluginVar]});
     });
@@ -378,7 +355,7 @@ function getPluginVariables(variables){
 
 function getVersionFromConfigFile(plugin, cfg){
     var pluginEntry = cfg.getPlugin(plugin);
-    return pluginEntry && (pluginEntry.src || pluginEntry.version); 
+    return pluginEntry && pluginEntry.spec;
 }
 
 function list(projectRoot, hooksRunner) {
@@ -440,3 +417,65 @@ function saveToConfigXmlOn(config_json, options){
     var autosave =  config_json.auto_save_plugins || false;
     return autosave || options.save;
 }
+
+function parseSource(target, opts) {
+    var url = require('url');
+    var uri = url.parse(target);
+    if (uri.protocol && uri.protocol != 'file:' && uri.protocol[1] != ':'
&& !target.match(/^\w+:\\/)) {
+        return target;
+    } else {
+        var plugin_dir = cordova_util.fixRelativePath(path.join(target, (opts.subdir || '.')));
+        if (fs.existsSync(plugin_dir)) {
+            return target;
+        }
+    }
+    return null;
+}
+
+function getSpec(pluginSource, projectRoot, pluginName) {
+    if (pluginSource.hasOwnProperty('url') || pluginSource.hasOwnProperty('path')) {
+        return pluginSource.url || pluginSource.path;
+    }
+
+    var version = null;
+    if (pluginSource.hasOwnProperty('id')) {
+        // Note that currently version is only saved here if it was explicitly specified
when the plugin was added.
+        var parts = pluginSource.id.split('@');
+        version = parts[1];
+        if (version) {
+            version = versionString(version);
+        }
+    }
+
+    if (!version) {
+        // Fallback on getting version from the plugin folder, if it's there
+        var pluginInfoProvider = new PluginInfoProvider();
+        var dir = path.join(projectRoot, 'plugins', pluginName);
+
+        try {
+            // pluginInfoProvider.get() will throw if directory does not exist.
+            var pluginInfo = pluginInfoProvider.get(dir);
+            if (pluginInfo) {
+                version = versionString(pluginInfo.version);
+            }
+        } catch (err) {
+        }
+    }
+
+    return version;
+}
+
+function versionString(version) {
+    var validVersion = semver.valid(version, true);
+    if (validVersion) {
+        return '^' + validVersion;
+    }
+
+    if (semver.validRange(version, true)) {
+        // Return what we were passed rather than the result of the validRange() call, as
that call makes modifications
+        // we don't want, like converting '^1.2.3' to '>=1.2.3-0 <2.0.0-0'
+        return version;
+    }
+
+    return null;
+}

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/src/cordova/project_metadata.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/project_metadata.js b/cordova-lib/src/cordova/project_metadata.js
index c7e23e2..d6c3098 100644
--- a/cordova-lib/src/cordova/project_metadata.js
+++ b/cordova-lib/src/cordova/project_metadata.js
@@ -33,11 +33,17 @@ function getPlatforms(projectRoot){
 
     // If an engine's 'version' property is really its source, map that to the appropriate
field.
     var engines = cfg.getEngines().map(function (engine) {
-        if (engine.version && !engine.src && !semver.validRange(engine.version))
{
-            engine.src = engine.version;
-            delete engine.version;
+        var result = {
+            name: engine.name
+        };
+
+        if (semver.validRange(engine.spec, true)) {
+            result.version = engine.spec;
+        } else {
+            result.src = engine.spec;
         }
-        return engine;
+
+        return result;
     });
 
     return Q(engines);
@@ -53,6 +59,16 @@ function getPlugins(projectRoot){
 
     // Map variables object to an array
     var plugins = cfg.getPlugins().map(function (plugin) {
+        var result = {
+            name: plugin.name
+        };
+
+        if (semver.validRange(plugin.spec, true)) {
+            result.version = plugin.spec;
+        } else {
+            result.src = plugin.spec;
+        }
+
         var variablesObject = plugin.variables;
         var variablesArray = [];
         if (variablesObject) {
@@ -63,8 +79,8 @@ function getPlugins(projectRoot){
                 });
             }
         }
-        plugin.variables = variablesArray;
-        return plugin;
+        result.variables = variablesArray;
+        return result;
     });
 
     return Q(plugins);

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a7e71227/cordova-lib/src/cordova/restore-util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/restore-util.js b/cordova-lib/src/cordova/restore-util.js
index 1b98c20..bfdafce 100644
--- a/cordova-lib/src/cordova/restore-util.js
+++ b/cordova-lib/src/cordova/restore-util.js
@@ -17,14 +17,15 @@
     under the License.
 */
 
-var cordova_util    = require('./util'),
-    ConfigParser     = require('../configparser/ConfigParser'),
-    path             = require('path'),
-    Q                = require('q'),
-    fs               = require('fs'),
-    plugin           = require('./plugin'),
-    events           = require('../events'),
-    cordova          = require('./cordova');
+var cordova_util = require('./util'),
+    ConfigParser = require('../configparser/ConfigParser'),
+    path         = require('path'),
+    Q            = require('q'),
+    fs           = require('fs'),
+    plugin       = require('./plugin'),
+    events       = require('../events'),
+    cordova      = require('./cordova'),
+    semver       = require('semver');
 
 exports.installPluginsFromConfigXML = installPluginsFromConfigXML;
 exports.installPlatformsFromConfigXML = installPlatformsFromConfigXML;
@@ -45,8 +46,8 @@ function installPlatformsFromConfigXML(platforms) {
         //if no platforms are specified we add all.
         if ((installAllPlatforms || platforms.indexOf(engine.name) > -1 ) && !platformAlreadyAdded)
{
             var t = engine.name;
-            if (engine.version) {
-                t += '@' + engine.version;
+            if (engine.spec) {
+                t += '@' + engine.spec;
             }
             return t;
         }
@@ -98,11 +99,11 @@ function installPluginsFromConfigXML(args) {
         events.emit('log', 'Discovered plugin "' + featureId + '" in config.xml. Installing
to the project');
         var pluginEntry = cfg.getPlugin(featureId);
 
-        // Install from given URL if defined or using a plugin id
-        var installFrom = pluginEntry.src || pluginEntry.name;
-        if( pluginEntry.version && !pluginEntry.src ){
-            installFrom += ('@' + pluginEntry.version);
-        }
+        // Install from given URL if defined or using a plugin id. If spec isn't a valid
version or version range,
+        // assume it is the location to install from.
+        var pluginSpec = pluginEntry.spec;
+        var installFrom = semver.validRange(pluginSpec, true) ? pluginEntry.name + '@' +
pluginSpec : pluginSpec;
+
         // Add feature preferences as CLI variables if have any
         var options = {cli_variables: pluginEntry.variables,
             searchpath: args.searchpath };


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


Mime
View raw message