cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject [7/8] cordova-lib git commit: CB-9108 Handle version ranges when add platform with --usegit.
Date Thu, 04 Jun 2015 17:36:23 GMT
CB-9108 Handle version ranges when add platform with --usegit.

Adding a platform using a version range didn't work with --usegit, since it would try to use
that version range as a git tag. This was a problem if you added the platform using 'platform
add <platform>@<range>', and also because our pinned platforms now use tilde versions
(which are shorthand for a version range).

Fix is to use npm to find the most recent version that matches the range, and use that.

We need to do the same when installing from npm, when we do our check to see if the requested
version has previously been downloaded using git.


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

Branch: refs/heads/5.1.x
Commit: 39b941ad51967f89dd05520b2a49c19149fa1e6c
Parents: bb02750
Author: Tim Barham <tim.barham@microsoft.com>
Authored: Tue Jun 2 14:14:57 2015 -0700
Committer: Steve Gill <stevengill97@gmail.com>
Committed: Thu Jun 4 10:25:46 2015 -0700

----------------------------------------------------------------------
 cordova-lib/src/cordova/lazy_load.js | 48 +++++++++++++++++++----------
 cordova-lib/src/cordova/util.js      | 50 +++++++++++++++++++++++++++++--
 2 files changed, 80 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/39b941ad/cordova-lib/src/cordova/lazy_load.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/lazy_load.js b/cordova-lib/src/cordova/lazy_load.js
index aff56a4..c73d1d2 100644
--- a/cordova-lib/src/cordova/lazy_load.js
+++ b/cordova-lib/src/cordova/lazy_load.js
@@ -29,7 +29,7 @@ var path          = require('path'),
     events        = require('../events'),
     request       = require('request'),
     config        = require('./config'),
-    HooksRunner        = require('../hooks/HooksRunner'),
+    HooksRunner   = require('../hooks/HooksRunner'),
     zlib          = require('zlib'),
     tar           = require('tar'),
     URL           = require('url'),
@@ -107,12 +107,16 @@ function cordova_git(platform) {
         return Q.reject(new Error('Cordova library "' + platform.name + '" not recognized.'));
     }
     plat = mixed_platforms[platform.name];
-    if (/^...*:/.test(plat.url)) {
-        plat.url = plat.url + ';a=snapshot;h=' + platform.version + ';sf=tgz';
-    }
     plat.id = 'cordova';
-    plat.version = platform.version;
-    return module.exports.custom(mixed_platforms, platform.name);
+
+    // We can't use a version range when getting from git, so if we have a range, find the
latest release on npm that matches.
+    return util.getLatestMatchingNpmVersion(platform.packageName, platform.version).then(function
(version) {
+        plat.version = version;
+        if (/^...*:/.test(plat.url)) {
+            plat.url = plat.url + ';a=snapshot;h=' + version + ';sf=tgz';
+        }
+        return module.exports.custom(mixed_platforms, platform.name);
+    });
 }
 
 function cordova_npm(platform) {
@@ -122,18 +126,22 @@ function cordova_npm(platform) {
     // Check if this version was already downloaded from git, if yes, use that copy.
     // TODO: remove this once we fully switch to npm workflow.
     var platdir = platforms[platform.name].altplatform || platform.name;
-    var git_dload_dir = path.join(util.libDirectory, platdir, 'cordova', platform.version);
-    if (fs.existsSync(git_dload_dir)) {
-        var subdir = platforms[platform.name].subdirectory;
-        if (subdir) {
-            git_dload_dir = path.join(git_dload_dir, subdir);
+    // If platform.version specifies a *range*, we need to determine what version we'll actually
get from npm (the
+    // latest version that matches the range) to know what local directory to look for.
+    return util.getLatestMatchingNpmVersion(platform.packageName, platform.version).then(function
(version) {
+        var git_dload_dir = path.join(util.libDirectory, platdir, 'cordova', version);
+        if (fs.existsSync(git_dload_dir)) {
+            var subdir = platforms[platform.name].subdirectory;
+            if (subdir) {
+                git_dload_dir = path.join(git_dload_dir, subdir);
+            }
+            events.emit('verbose', 'Platform files for "' + platform.name + '" previously
downloaded not from npm. Using that copy.');
+            return Q(git_dload_dir);
         }
-        events.emit('verbose', 'Platform files for "' + platform.name + '" previously downloaded
not from npm. Using that copy.');
-        return Q(git_dload_dir);
-    }
 
-    var pkg = platform.packageName + '@' + platform.version;
-    return exports.npm_cache_add(pkg);
+        var pkg = platform.packageName + '@' + platform.version;
+        return exports.npm_cache_add(pkg);
+    });
 }
 
 // Equivalent to a command like
@@ -248,6 +256,14 @@ function custom(platforms, platform) {
                 }
             });
             req.pipe(zlib.createUnzip())
+            .on('error', function(err) {
+                // Sometimes if the URL is bad (most likely unavailable version), and git-wip-us.apache.org
is
+                // particularly slow at responding, we hit an error because of bad data piped
to zlib.createUnzip()
+                // before we hit the request.get() callback above (with a 404 error). Handle
that gracefully. It is
+                // likely that we will end up calling d.reject() for an HTTP error in the
request() callback above, but
+                // in case not, reject with a useful error here.
+                d.reject(new Error('Unable to fetch platform ' + platform + '@' + version
+ ': Error: version not found.'));
+            })
             .pipe(tar.Extract({path:tmp_dir}))
             .on('error', function(err) {
                 shell.rm('-rf', tmp_dir);

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/39b941ad/cordova-lib/src/cordova/util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/util.js b/cordova-lib/src/cordova/util.js
index 1f598e8..0231ec0 100644
--- a/cordova-lib/src/cordova/util.js
+++ b/cordova-lib/src/cordova/util.js
@@ -22,12 +22,15 @@ var fs            = require('fs'),
     path          = require('path'),
     CordovaError  = require('../CordovaError'),
     shell         = require('shelljs'),
-    url           = require('url');
+    url           = require('url'),
+    npm           = require('npm'),
+    Q             = require('q'),
+    semver        = require('semver');
 
 // Global configuration paths
 var global_config_path = process.env['CORDOVA_HOME'];
 if (!global_config_path) {
-	var HOME = process.env[(process.platform.slice(0, 3) == 'win') ? 'USERPROFILE' : 'HOME'];
+    var HOME = process.env[(process.platform.slice(0, 3) == 'win') ? 'USERPROFILE' : 'HOME'];
     global_config_path = path.join(HOME, '.cordova');
 }
 
@@ -56,6 +59,8 @@ exports.fixRelativePath = fixRelativePath;
 exports.convertToRealPathSafe = convertToRealPathSafe;
 exports.isDirectory = isDirectory;
 exports.isUrl = isUrl;
+exports.getLatestMatchingNpmVersion = getLatestMatchingNpmVersion;
+exports.getAvailableNpmVersions = getAvailableNpmVersions;
 
 function isUrl(value) {
     var u = value && url.parse(value);
@@ -292,3 +297,44 @@ function addModuleProperty(module, symbol, modulePath, opt_wrap, opt_obj)
{
         });
     }
 }
+
+/**
+ * Returns the latest version of the specified module on npm that matches the specified version
or range.
+ * @param {string} module_name - npm module name.
+ * @param {string} version - semver version or range (loose allowed).
+ * @returns {Promise} Promise for version (a valid semver version if one is found, otherwise
whatever was provided).
+ */
+function getLatestMatchingNpmVersion(module_name, version) {
+    var validVersion = semver.valid(version, /* loose */ true);
+    if (validVersion) {
+        // This method is really intended to work with ranges, so if a version rather than
a range is specified, we just
+        // assume it is available and return it, bypassing the need for the npm call.
+        return Q(validVersion);
+    }
+
+    var validRange = semver.validRange(version, /* loose */ true);
+    if (!validRange) {
+        // Just return what we were passed
+        return Q(version);
+    }
+
+    return getAvailableNpmVersions(module_name).then(function (versions) {
+        return semver.maxSatisfying(versions, validRange) || version;
+    });
+}
+
+/**
+ * Returns a promise for an array of versions available for the specified npm module.
+ * @param {string} module_name - npm module name.
+ * @returns {Promise} Promise for an array of versions.
+ */
+function getAvailableNpmVersions(module_name) {
+    return Q.nfcall(npm.load).then(function () {
+        return Q.ninvoke(npm.commands, 'view', [module_name, 'versions'], /* silent = */
true).then(function (result) {
+            // result is an object in the form:
+            //     {'<version>': {versions: ['1.2.3', '1.2.4', ...]}}
+            // (where <version> is the latest version)
+            return result[Object.keys(result)[0]].versions;
+        });
+    });
+}


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


Mime
View raw message