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 2014/03/27 22:57:21 UTC

[6/8] git commit: CB-6357 platform check - install each platform to determine working + version number

CB-6357 platform check - install each platform to determine working + version number


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

Branch: refs/heads/master
Commit: 4bc9e7017d61a5c3e02ba7a2d2037a5e4f21e38c
Parents: 39eabf8
Author: Josh Soref <js...@blackberry.com>
Authored: Wed Mar 26 10:43:21 2014 -0400
Committer: Josh Soref <js...@blackberry.com>
Committed: Thu Mar 27 00:03:48 2014 -0400

----------------------------------------------------------------------
 spec/platform.spec.js | 34 +++++++++++++++---
 src/platform.js       | 87 ++++++++++++++++++++++++++++++++++------------
 2 files changed, 93 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/4bc9e701/spec/platform.spec.js
----------------------------------------------------------------------
diff --git a/spec/platform.spec.js b/spec/platform.spec.js
index 0ad2fee..65a22b7 100644
--- a/spec/platform.spec.js
+++ b/spec/platform.spec.js
@@ -172,6 +172,12 @@ describe('platform command', function() {
             });
         });
         describe('`add`', function() {
+            beforeEach(function() {
+                supported_platforms.forEach(function(p) {
+                    platforms[p].parser.check_requirements = function(){return Q();};
+                });
+            });
+
             it('should shell out to specified platform\'s bin/create, using the version that is specified in platforms manifest', function(done) {
                 cordova.raw.platform('add', 'android').then(function() {
                     expect(spawn.mostRecentCall.args.join()).toMatch(/lib.android.cordova.\d.\d.\d[\d\-\w]*.bin.create/gi);
@@ -303,17 +309,17 @@ describe('platform command', function() {
                 current: {
                     uri: "https://localhost",
                     version: "3.3.0",
-                    parser: undefined
+                    parser: function(){}
                 },
                 stale: {
                     uri: "https://localhost",
                     version: "3.3.0",
-                    parser: undefined
+                    parser: function(){}
                 },
                 newer: {
                     uri: "https://localhost",
                     version: "3.3.0",
-                    parser: undefined
+                    parser: function(){}
                 }
             };
             beforeEach(function() {
@@ -336,10 +342,22 @@ describe('platform command', function() {
                 });
             });
             it('check platforms current, stale, newer', function() {
-                existsSync.andReturn(true);
+                existsSync.andCallFake(function(dir) {
+                    if (/cordova-platform-check.*version/.test(dir)) {
+                        return true;
+                    }
+                    if (/cordova-platform-check/.test(dir)) {
+                        return false;
+                    }
+                    return true;
+                });
+                var create = spyOn(cordova.raw, 'create').andCallFake(function() { return Q() });
+
                 spawn.andCallFake(function(cmd) {
                     var out;
-                    if (/current/.test(cmd)) {
+                    if (/cordova-platform-check/.test(cmd)) {
+                        out = '3.3.0';
+                    } else if (/current/.test(cmd)) {
                         out = '3.3.0';
                     } else if (/stale/.test(cmd)) {
                         out = '3.2.0';
@@ -387,6 +405,12 @@ describe('platform command', function() {
             });
         });
         describe('add hooks', function() {
+            beforeEach(function() {
+                supported_platforms.forEach(function(p) {
+                    platforms[p].parser.check_requirements = function(){return Q();};
+                });
+            });
+
             it('should fire before and after hooks through the hooker module', function(done) {
                 cordova.raw.platform('add', 'android').then(function() {
                     expect(fire).toHaveBeenCalledWith('before_platform_add', {platforms:['android']});

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/4bc9e701/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index dc5f378..0470569 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -17,10 +17,12 @@
     under the License.
 */
 var config            = require('./config'),
+    cordova           = require('../cordova'),
     cordova_util      = require('./util'),
     ConfigParser     = require('./ConfigParser'),
     util              = require('util'),
     fs                = require('fs'),
+    os                = require('os'),
     path              = require('path'),
     hooker            = require('./hooker'),
     events            = require('./events'),
@@ -126,39 +128,78 @@ function update(hooks, projectRoot, targets, opts) {
 }
 
 function check(hooks, projectRoot) {
-            var platforms_on_fs = cordova_util.listPlatforms(projectRoot);
-            return hooks.fire('before_platform_ls')
-            .then(function() {
-                // Acquire the version number of each platform we have installed, and output that too.
-                return Q.all(platforms_on_fs.map(function(p) {
-                    return getVersionFromScript(path.join(projectRoot, 'platforms', p, 'cordova', 'version'), null)
-                    .then(function(v) {
-                        if (!v) {
-                            return null;
-                        }
-                        var avail = platforms[p].version;
-                        if (semver.gt(avail, v)) {
-                            return p + ' @ ' + v + ' could be updated to: ' + avail;
+        var platformsText = [],
+            platforms_on_fs = cordova_util.listPlatforms(projectRoot),
+            scratch = path.join(os.tmpdir(), "cordova-platform-check-"+Date.now()),
+            listeners = events._events;
+            events._events = {};
+        var result = Q.defer();
+        cordova.raw.create(scratch)
+        .then(function () {
+            var h = new hooker(scratch);
+            // Acquire the version number of each platform we have installed, and output that too.
+            Q.all(platforms_on_fs.map(function(p) {
+                var d = Q.defer();
+                add(h, scratch, [p], {spawnoutput: {stdio: 'ignore'}})
+                .catch(function () {
+                    /* If a platform doesn't install, then we can't realistically suggest updating */
+                    d.resolve();
+                }).then(function() {
+                    var d_avail = Q.defer(),
+                        d_cur = Q.defer();
+                    getVersionFromScript(path.join(scratch, 'platforms', p, 'cordova', 'version'), null)
+                    .catch(function () {
+                        /* Platform version script failed, we can't work with this */
+                        d_avail.resolve('');
+                    })
+                    .then(function(avail) {
+                        if (!avail) {
+                            /* Platform version script was silent, we can't work with this */
+                            d_avail.resolve('');
+                        } else {
+                            d_avail.resolve(avail);
                         }
-                        return '';
-                    }, function(v) {
-                        var avail = platforms[p].version;
-                        return p + ' @ broken could be updated to: ' + avail
                     });
-                }));
-            }).then(function(platformsText) {
+                    getVersionFromScript(path.join(projectRoot, 'platforms', p, 'cordova', 'version'), null)
+                    .catch(function () {
+                        d_cur.resolve('broken');
+                    }).then(function(v) {
+                        d_cur.resolve(v || '');
+                    });
+                    Q.all([d_avail.promise, d_cur.promise]).spread(function (avail, v) {
+                        var m;
+                        if (avail && (!v || v == 'broken' || semver.gt(avail, v))) {
+                            m = p + ' @ ' + (v || 'unknown') + ' could be updated to: ' + avail;
+                            platformsText.push(m);
+                        }
+                        d.resolve(m);
+                    })
+                    .catch(function () {
+                        return '?';
+                    })
+                    .done();
+                });
+                return d.promise;
+            })).then(function() {
                 var results = '';
+                events._events = listeners;
+                shell.rm('-rf', scratch);
                 if (platformsText) {
                     results = platformsText.filter(function (p) {return !!p}).join('\n');
                 }
                 if (!results) {
                     results = 'All platforms are up-to-date.';
                 }
-
                 events.emit('results', results);
-            }).then(function() {
-                return hooks.fire('after_platform_ls');
-            });
+                result.resolve();
+            })
+            .done();
+        }).catch(function (){
+            events._events = listeners;
+            shell.rm('-rf', scratch);
+        })
+        .done();
+        return result.promise;
 }
 
 function list(hooks, projectRoot) {