cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an-s...@apache.org
Subject cordova-windows git commit: CB-8954 Adds `requirements` command support to check_reqs module
Date Fri, 29 May 2015 09:49:32 GMT
Repository: cordova-windows
Updated Branches:
  refs/heads/master 0c8943d17 -> 782ea0839


CB-8954 Adds `requirements` command support to check_reqs module


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

Branch: refs/heads/master
Commit: 782ea083988706579d3f322ac111b084982310c3
Parents: 0c8943d
Author: Vladimir Kotikov <v-vlkoti@microsoft.com>
Authored: Fri May 29 12:49:04 2015 +0300
Committer: Vladimir Kotikov <v-vlkoti@microsoft.com>
Committed: Fri May 29 12:49:04 2015 +0300

----------------------------------------------------------------------
 bin/check_reqs               |   4 +-
 bin/lib/check_reqs.js        | 109 ++++++++++++++++++++++++--
 bin/lib/create.js            |   6 +-
 spec/unit/check_reqs.spec.js | 156 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 264 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/782ea083/bin/check_reqs
----------------------------------------------------------------------
diff --git a/bin/check_reqs b/bin/check_reqs
index 93e29bb..568ec8b 100755
--- a/bin/check_reqs
+++ b/bin/check_reqs
@@ -28,7 +28,7 @@ if (['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2])
>
   check_reqs.run().done(function success(msbuild) {
       console.log('Environment is supported; found MSBuild Tools version ' + msbuild.version
+ ' at ' + msbuild.path);
   }, function (err) {
-      console.error('Failed to check requirements due to', err);
+      console.error('Environment is not supported: ' + err);
       process.exit(2);
   });
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/782ea083/bin/lib/check_reqs.js
----------------------------------------------------------------------
diff --git a/bin/lib/check_reqs.js b/bin/lib/check_reqs.js
index bdd27f2..41b1754 100644
--- a/bin/lib/check_reqs.js
+++ b/bin/lib/check_reqs.js
@@ -17,17 +17,110 @@
        under the License.
 */
 
-var Q     = require('Q'),
-    os    = require('os'),
+/*jshint node:true*/
+
+var Q     = require('Q');
+
+var MSBuildTools;
+try {
     MSBuildTools = require('../../template/cordova/lib/MSBuildTools');
+} catch (ex) {
+    // If previous import fails, we're probably running this script
+    // from installed platform and the module location is different.
+    MSBuildTools = require('./MSBuildTools');
+}
+
+/**
+ * Check if current OS is supports building windows platform
+ * @return {Promise} Promise either fullfilled or rejected with error message.
+ */
+var checkOS = function () {
+    var platform = process.platform;
+    return (platform === 'win32') ?
+        Q.resolve(platform):
+        // Build Universal windows apps available for windows platform only, so we reject
on others platforms
+        Q.reject('Cordova tooling for Windows requires Windows OS to build project');
+};
+
+/**
+ * Checks if MSBuild tools is available.
+ * @return {Promise} Promise either fullfilled with MSBuild version
+ *                           or rejected with error message.
+ */
+var checkMSBuild = function () {
+    return MSBuildTools.findAvailableVersion()
+    .then(function (msbuildTools) {
+        return Q.resolve(msbuildTools.version);
+    }, function () {
+        return Q.reject('MSBuild tools not found. Please install MSBuild tools or VS 2013
from ' +
+            'https://www.visualstudio.com/downloads/download-visual-studio-vs');
+    });
+};
 
 module.exports.run = function () {
-    if (os.platform() != 'win32'){
-      // Build Universal windows apps available for windows platform only, so we reject on
others platforms
-        return Q.reject('ERROR: Cordova tooling for Windows requires Windows OS');
-    }
-    // Check whther MSBuild Tools are available
-    return MSBuildTools.findAvailableVersion();
+    return checkOS().then(function () {
+        return MSBuildTools.findAvailableVersion();
+    });
+};
+
+/**
+ * Object that represents one of requirements for current platform.
+ * @param {String}  id        The unique identifier for this requirements.
+ * @param {String}  name      The name of requirements. Human-readable field.
+ * @param {Boolean} isFatal   Marks the requirement as fatal. If such requirement will fail
+ *                            next requirements' checks will be skipped.
+ */
+var Requirement = function (id, name, isFatal) {
+    this.id = id;
+    this.name = name;
+    this.installed = false;
+    this.metadata = {};
+    this.isFatal = isFatal || false;
+};
+
+var requirements = [
+    new Requirement('os', 'Windows OS', true),
+    new Requirement('msbuild', 'MSBuild Tools')
+];
+
+// Define list of checks needs to be performed
+var checkFns = [checkOS, checkMSBuild];
+
+/**
+ * Methods that runs all checks one by one and returns a result of checks
+ * as an array of Requirement objects. This method intended to be used by cordova-lib check_reqs
method
+ *
+ * @return Promise<Requirement[]> Array of requirements. Due to implementation, promise
is always fulfilled.
+ */
+module.exports.check_all = function() {
+
+    var result = [];
+    var fatalIsHit = false;
+
+    // Then execute requirement checks one-by-one
+    return checkFns.reduce(function (promise, checkFn, idx) {
+        return promise.then(function () {
+            // If fatal requirement is failed,
+            // we don't need to check others
+            if (fatalIsHit) return Q();
+
+            var requirement = requirements[idx];
+            return checkFn()
+            .then(function (version) {
+                requirement.installed = true;
+                requirement.metadata.version = version;
+                result.push(requirement);
+            }, function (err) {
+                if (requirement.isFatal) fatalIsHit = true;
+                requirement.metadata.reason = err;
+                result.push(requirement);
+            });
+        });
+    }, Q())
+    .then(function () {
+        // When chain is completed, return requirements array to upstream API
+        return result;
+    });
 };
 
 module.exports.help = function () {

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/782ea083/bin/lib/create.js
----------------------------------------------------------------------
diff --git a/bin/lib/create.js b/bin/lib/create.js
index 55347b0..bc07b78 100644
--- a/bin/lib/create.js
+++ b/bin/lib/create.js
@@ -68,6 +68,10 @@ module.exports.run = function (argv) {
     // copy node_modules to cordova directory
     shell.cp('-r', path.join(root, 'node_modules'), path.join(projectPath, 'cordova'));
 
+    // copy check_reqs module to cordova directory
+    shell.cp('-rf', path.join(root, 'bin', 'check_reqs*'), path.join(projectPath, 'cordova'));
+    shell.cp('-rf', path.join(root, 'bin', 'lib', 'check_reqs*'), path.join(projectPath,
'cordova', 'lib'));
+
     if (templateOverrides && fs.existsSync(templateOverrides)) {
         console.log('Copying template overrides from ' + templateOverrides + ' to ' + projectPath);
         shell.cp('-rf', templateOverrides, projectPath);
@@ -105,4 +109,4 @@ module.exports.help = function () {
     console.log('examples:');
     console.log('    create C:\\Users\\anonymous\\Desktop\\MyProject');
     console.log('    create C:\\Users\\anonymous\\Desktop\\MyProject io.Cordova.Example AnApp');
-};
\ No newline at end of file
+};

http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/782ea083/spec/unit/check_reqs.spec.js
----------------------------------------------------------------------
diff --git a/spec/unit/check_reqs.spec.js b/spec/unit/check_reqs.spec.js
new file mode 100644
index 0000000..a317da8
--- /dev/null
+++ b/spec/unit/check_reqs.spec.js
@@ -0,0 +1,156 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
+
+var Q = require('q');
+var path = require('path');
+var rewire = require('rewire');
+var binPath = '../../bin';
+
+var check_reqs = rewire(path.join(binPath, 'lib/check_reqs.js'));
+
+describe('check_reqs module', function () {
+
+    describe('has Requirement object', function () {
+        var Requirement;
+        beforeEach(function () {
+            Requirement = check_reqs.__get__('Requirement');
+        });
+
+        it('that should be constructable', function () {
+            var requirement = new Requirement('someId', 'Some Name');
+            expect(requirement instanceof Requirement).toBeTruthy();
+        });
+
+        it('that should have fields defined', function () {
+            var requirement = new Requirement('someId', 'Some Name');
+            expect(requirement.id).toBe('someId');
+            expect(requirement.name).toBe('Some Name');
+            expect(requirement.installed).toBe(false);
+            expect(requirement.metadata).toBeDefined();
+            expect(requirement.isFatal).toBe(false);
+            var fatalReq = new Requirement('someId', 'Some Name', true);
+            expect(fatalReq.isFatal).toBe(true);
+        });
+    });
+
+    describe('has check_all method', function() {
+        // var consoleLogOriginal;
+
+        var Requirement,
+            originalrequirements, originalcheckFns,
+            fakeRequirements, fakeCheckFns,
+            checkSpy;
+
+        beforeEach(function () {
+            Requirement = check_reqs.__get__('Requirement');
+            originalrequirements = check_reqs.__get__('requirements');
+            originalcheckFns = check_reqs.__get__('checkFns');
+
+            fakeRequirements = [
+                new Requirement('1', 'First requirement'),
+                // Mark the second as fatal
+                new Requirement('2', 'Second requirement', true),
+                new Requirement('3', 'Third requirement')
+            ];
+
+            checkSpy = jasmine.createSpy('checkSpy');
+            fakeCheckFns = [
+                checkSpy.andReturn(Q('1.0')),
+                checkSpy.andReturn(Q('2.0')),
+                checkSpy.andReturn(Q('3.0'))
+            ];
+        });
+
+        afterEach(function() {
+            check_reqs.__set__('requirements', originalrequirements);
+            check_reqs.__set__('checkFns', originalcheckFns);
+        });
+
+        it('that should return a promise, fulfilled with an array of Requirements', function
(done) {
+            check_reqs.__set__('requirements', fakeRequirements);
+            check_reqs.__set__('checkFns', fakeCheckFns);
+
+            var checkResult = check_reqs.check_all();
+            expect(Q.isPromise(checkResult)).toBeTruthy();
+            checkResult.then(function (result) {
+                expect(result instanceof Array).toBeTruthy();
+                expect(result.length).toBe(3);
+                result.forEach(function (resultItem) {
+                    expect(resultItem instanceof Requirement).toBeTruthy();
+                    expect(resultItem.installed).toBeTruthy();
+                });
+                done();
+            });
+        });
+
+        it('that should not reject if one of requirements is not installed', function  (done)
{
+            check_reqs.__set__('requirements', fakeRequirements);
+            fakeCheckFns[0] = function () { return Q.reject('Error message'); };
+            check_reqs.__set__('checkFns', fakeCheckFns);
+
+            check_reqs.check_all()
+            .then(function (requirements) {
+                expect(requirements.length).toBe(3);
+                expect(requirements[0].installed).toBeFalsy();
+                done();
+            })
+            .catch(function  (error) {
+                expect(error).not.toBeDefined();
+                done();
+            });
+        });
+
+        it('that should reject if one of checks has internal erorrs', function  (done) {
+            check_reqs.__set__('requirements', fakeRequirements);
+            fakeCheckFns[0] = checkSpy.andThrow('Fatal error');
+            check_reqs.__set__('checkFns', fakeCheckFns);
+
+            check_reqs.check_all()
+            .then(function (requirements) {
+                expect(requirements).not.toBeDefined();
+                done();
+            })
+            .catch(function  (error) {
+                expect(error).toBe('Fatal error');
+                done();
+            });
+        });
+
+        it('that should not run other requirements checks if `fatal` requirement isn\'t installed',
function  (done) {
+            check_reqs.__set__('requirements', fakeRequirements);
+            // The second requirement is fatal, so we're setting up second check to fail
+            // fakeCheckFns[1] = function () { return Q.reject('Error message'); };
+            fakeCheckFns[1] = checkSpy.andReturn(Q.reject('Error message'));
+            check_reqs.__set__('checkFns', fakeCheckFns);
+
+            check_reqs.check_all()
+            .then(function (requirements) {
+                expect(requirements.length).toBe(2);
+                expect(requirements[1].isFatal).toBeTruthy();
+                expect(checkSpy.calls.length).toBe(2);
+                done();
+            })
+            .catch(function  (error) {
+                expect(error).not.toBeDefined();
+                done();
+            });
+        });
+    });
+
+});


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


Mime
View raw message