CB-11346: fixed remove platform to work with unknown platforms

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

Branch: refs/heads/common-2.0.x
Commit: 4e3d6ca549644ed74a780259dfe17a7b7ca005d8
Parents: faf5477
Author: Steve Gill <stevengil...@gmail.com>
Authored: Fri Mar 10 16:43:56 2017 -0800
Committer: Steve Gill <stevengil...@gmail.com>
Committed: Thu Mar 16 22:55:40 2017 -0700

----------------------------------------------------------------------
 cordova-lib/src/cordova/platform.js          | 89 +++++++++++------------
 cordova-lib/src/cordova/util.js              | 16 ++--
 cordova-lib/src/platforms/PlatformApiPoly.js |  3 +
 cordova-lib/src/platforms/platforms.js       |  6 +-
 cordova-lib/src/plugman/install.js           |  4 -
 5 files changed, 60 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/4e3d6ca5/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js 
b/cordova-lib/src/cordova/platform.js
index 19e7355..bc616ef 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -85,17 +85,26 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, 
opts) {
 
         return promiseutil.Q_chainmap(targets, function(target) {
             // For each platform, download it and call its helper script.
-            var parts = target.split('@');
-            var platform = parts[0];
-            var spec = parts[1];
             var pkgJson;
-
+            var platform;
+            var spec;
+            var parts = target.split('@');
+            if (parts.length > 1 && parts[0] === '') {
+                //scoped package
+                platform = '@' + parts[1];
+                spec = parts[2];
+            } else {
+                platform = parts[0];
+                spec = parts[1];
+            }
             return Q.when().then(function() {
-                if (!(platform in platforms)) {
+                // if platform is a local path or url, it should be assigned
+                // to spec
+                if (cordova_util.isDirectory(path.resolve(platform)) || 
+                    cordova_util.isUrl(platform)) {
                     spec = platform;
                     platform = null;
                 }
-
                 if(platform === 'amazon-fireos') {
                     events.emit('warn', 'amazon-fireos has been deprecated. 
Please use android instead.');
                 }
@@ -124,10 +133,11 @@ function addHelper(cmd, hooksRunner, projectRoot, 
targets, opts) {
 
                 // 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) && !spec ){
+                if( (opts.save || autosave) && !spec && platforms[platform]) {
                     spec = platforms[platform].version;
                 }
-
+                
+                // Handle local paths
                 if (spec) {
                     var maybeDir = cordova_util.fixRelativePath(spec);
                     if (cordova_util.isDirectory(maybeDir)) {
@@ -376,9 +386,19 @@ function downloadPlatform(projectRoot, platform, version, 
opts) {
     });
 }
 
+/**
+ * Removes the cordova- prefix from the platform's name for known platforms.
+ * @param {string} name - platform name
+ * @returns {string} 
+ */
 function platformFromName(name) {
+    var platName = name;
     var platMatch = /^cordova-([a-z0-9-]+)$/.exec(name);
-    return platMatch && platMatch[1];
+    if(platMatch && (platMatch[1] in platforms)) {
+        platName = platMatch[1];
+        events.emit('verbose', 'Removing "cordova-" prefix from ' + name);
+    }
+    return platName; 
 }
 
 // Returns a Promise
@@ -392,20 +412,19 @@ function getPlatformDetailsFromDir(dir, platformIfKnown){
         var pkgPath = path.join(libDir, 'package.json');
         delete require.cache[pkgPath];
         var pkg = require(pkgPath);
-
         platform = platformFromName(pkg.name);
         version = pkg.version;
         delete require.cache[pkgPath];
     } catch(e) {
         // Older platforms didn't have package.json.
         platform = platformIfKnown || platformFromName(path.basename(dir));
-        var verFile = fs.existsSync(path.join(libDir, 'VERSION')) ? 
path.join(libDir, 'VERSION') :
+        var verFile = fs.existsSync(path.join(libDir, 'VERSION')) ? 
path.join(libDir, 'VERSION') : 
                       fs.existsSync(path.join(libDir, 'CordovaLib', 
'VERSION')) ? path.join(libDir, 'CordovaLib', 'VERSION') : null;
         if (verFile) {
             version = fs.readFileSync(verFile, 'UTF-8').trim();
         }
     }
-
+    
     // platform does NOT have to exist in 'platforms', but it should have a 
name, and a version
     if (!version || !platform) {
         return Q.reject(new CordovaError('The provided path does not seem to 
contain a ' +
@@ -420,10 +439,6 @@ function getPlatformDetailsFromDir(dir, platformIfKnown){
 }
 
 function getVersionFromConfigFile(platform, cfg) {
-    if(!platform || ( !(platform in platforms) )){
-        throw new CordovaError('Invalid platform: ' + platform);
-    }
-
     // Get appropriate version from config.xml
     var engine = _.find(cfg.getEngines(), function(eng){
         return eng.name.toLowerCase() === platform.toLowerCase();
@@ -668,7 +683,14 @@ function 
addDeprecatedInformationToPlatforms(platformsList){
     return platformsList;
 }
 
-// Returns a promise.
+/**
+ * Handles all cordova platform commands. 
+
+ * @param {string} command - Command to execute (add, rm, ls, update, save)
+ * @param {Object[]} targets - Array containing platforms to execute commands 
on
+ * @param {Object} opts
+ * @returns {Promise} 
+ */
 module.exports = platform;
 function platform(command, targets, opts) {
     // CB-10519 wrap function code into promise so throwing error
@@ -680,36 +702,9 @@ function platform(command, targets, opts) {
 
         if (arguments.length === 0) command = 'ls';
 
-        // Verify that targets look like platforms. Examples:
-        // - android
-        // - android@3.5.0
-        // - ../path/to/dir/with/platform/files
-        // - https://github.com/apache/cordova-android.git
-        if (targets) {
-            if (!(targets instanceof Array)) targets = [targets];
-            targets.forEach(function (t) {
-                // Trim the @version part if it's there.
-                var p = t.split('@')[0];
-                // OK if it's one of known platform names.
-                if (p in platforms) return;
-                // Not a known platform name, check if its a real path.
-                var pPath = path.resolve(t);
-                if (fs.existsSync(pPath)) return;
-
-                var msg;
-                // If target looks like a url, we will try cloning it with git
-                if (/[~:/\\.]/.test(t)) {
-                    return;
-                } else {
-                    // Neither path, git-url nor platform name - throw.
-                    msg = 'Platform "' + t +
-                    '" not recognized as a core cordova platform. See `' +
-                    cordova_util.binname + ' platform list`.'
-                    ;
-                }
-                throw new CordovaError(msg);
-            });
-        } else if (command == 'add' || command == 'rm') {
+        if (targets && !(targets instanceof Array)) targets = [targets];
+
+        if (!targets && (command == 'add' || command == 'rm')) {
             msg = 'You need to qualify `add` or `remove` with one or more 
platforms!';
             return Q.reject(new CordovaError(msg));
         }

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/4e3d6ca5/cordova-lib/src/cordova/util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/util.js b/cordova-lib/src/cordova/util.js
index 86d0cf7..0596c4b 100644
--- a/cordova-lib/src/cordova/util.js
+++ b/cordova-lib/src/cordova/util.js
@@ -182,7 +182,7 @@ function deleteSvnFolders(dir) {
     var contents = fs.readdirSync(dir);
     contents.forEach(function(entry) {
         var fullpath = path.join(dir, entry);
-        if (fs.statSync(fullpath).isDirectory()) {
+        if (isDirectory(fullpath)) {
             if (entry == '.svn') {
                 shell.rm('-rf', fullpath);
             } else module.exports.deleteSvnFolders(fullpath);
@@ -198,7 +198,7 @@ function listPlatforms(project_dir) {
     // get subdirs (that are actually dirs, and not files)
     var subdirs = fs.readdirSync(platforms_dir)
     .filter(function(file) {
-        return fs.statSync(path.join(platforms_dir, file)).isDirectory();
+        return isDirectory(path.join(platforms_dir, file));
     });
     return subdirs;
 }
@@ -222,13 +222,11 @@ function getInstalledPlatformsWithVersions(project_dir) {
 
 // list the directories in the path, ignoring any files
 function findPlugins(pluginPath) {
-    var plugins = [],
-        stats;
+    var plugins = [];
 
     if (fs.existsSync(pluginPath)) {
         plugins = fs.readdirSync(pluginPath).filter(function (fileName) {
-            stats = fs.statSync(path.join(pluginPath, fileName));
-            return fileName != '.svn' && fileName != 'CVS' && 
stats.isDirectory();
+            return fileName != '.svn' && fileName != 'CVS' && 
isDirectory(path.join(pluginPath, fileName));
         });
     }
 
@@ -338,6 +336,12 @@ function ensurePlatformOptionsCompatible (platformOptions) 
{
     return opts;
 }
 
+/**
+ * Checks to see if the argument is a directory
+ * 
+ * @param {string} dir - string representing path of directory
+ * @return {boolean}
+ */
 function isDirectory(dir) {
     try {
         return fs.lstatSync(dir).isDirectory();

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/4e3d6ca5/cordova-lib/src/platforms/PlatformApiPoly.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/platforms/PlatformApiPoly.js 
b/cordova-lib/src/platforms/PlatformApiPoly.js
index 3477a0f..dc78113 100644
--- a/cordova-lib/src/platforms/PlatformApiPoly.js
+++ b/cordova-lib/src/platforms/PlatformApiPoly.js
@@ -55,6 +55,9 @@ function PlatformApiPoly(platform, platformRootDir, events) {
     this.platform = platform;
     this.events = events || require('cordova-common').events;
 
+    if (!(platform in knownPlatforms))
+        throw new CordovaError('Unknown platform ' + platform);
+
     var ParserConstructor = require(knownPlatforms[platform].parser_file);
     this._parser = new ParserConstructor(this.root);
     this._handler = require(knownPlatforms[platform].handler_file);

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/4e3d6ca5/cordova-lib/src/platforms/platforms.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/platforms/platforms.js 
b/cordova-lib/src/platforms/platforms.js
index 0efcbfc..5e20a46 100644
--- a/cordova-lib/src/platforms/platforms.js
+++ b/cordova-lib/src/platforms/platforms.js
@@ -66,11 +66,15 @@ function getPlatformApi(platform, platformRootDir) {
         } catch (err) {
             // Check if platform already compatible w/ PlatformApi and show 
deprecation warning
             if (err && err.code === 'MODULE_NOT_FOUND') {
-                if (platforms[platform].apiCompatibleSince) {
+                if (platforms[platform] && 
platforms[platform].apiCompatibleSince) {
                     events.emit('warn', ' Using this version of Cordova with 
older version of cordova-' + platform +
                         ' is being deprecated. Consider upgrading to cordova-' 
+ platform + '@' +
                         platforms[platform].apiCompatibleSince + ' or newer.');
+                } else if (platforms[platform] === undefined) {
+                    // throw error because polyfill doesn't support non core 
platforms 
+                    throw new Error('The platform "' + platform + '" does not 
appear to be a valid cordova platform. It is missing API.js. '+ platform +' not 
supported.');
                 }
+
                 // else nothing - there is no Api.js and no deprecation 
information hence
                 // the platform just does not expose Api and we will use 
polyfill as usual
             } else {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/4e3d6ca5/cordova-lib/src/plugman/install.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/plugman/install.js 
b/cordova-lib/src/plugman/install.js
index 3b96a1a..4a3838e 100644
--- a/cordova-lib/src/plugman/install.js
+++ b/cordova-lib/src/plugman/install.js
@@ -76,10 +76,6 @@ module.exports = function installPlugin(platform, 
project_dir, id, plugins_dir,
 
     plugins_dir = plugins_dir || path.join(project_dir, 'cordova', 'plugins');
 
-    if (!platform_modules[platform]) {
-        return Q.reject(new CordovaError(platform + ' not supported.'));
-    }
-
     var current_stack = new action_stack();
     return possiblyFetch(id, plugins_dir, options)
     .then(function(plugin_dir) {


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

Reply via email to