From a90d167d6f2b8bda87ab45825563656a54561f5d Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Fri, 29 May 2015 09:52:06 +0300 Subject: [PATCH] CB-8954 Adds `requirements` command support to check_reqs module --- bin/lib/check_reqs.js | 107 ++++++++++++++++++++++++++++++++++++++---- bin/lib/create.js | 6 ++- 2 files changed, 104 insertions(+), 9 deletions(-) diff --git a/bin/lib/check_reqs.js b/bin/lib/check_reqs.js index c87a06f4..31252b8d 100644 --- a/bin/lib/check_reqs.js +++ b/bin/lib/check_reqs.js @@ -17,17 +17,108 @@ under the License. */ -var Q = require('Q'), - os = require('os'), +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 () { + // Check whether MSBuild Tools are available + 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; +}; + +/** + * 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 Array of requirements. Due to implementation, promise is always fulfilled. + */ +module.exports.check_all = function() { + + var requirements = [ + new Requirement('os', 'Windows OS', true), + new Requirement('msbuild', 'MSBuild Tools') + ]; + + var result = []; + var fatalIsHit = false; + + // Define list of checks needs to be performed + var checkFns = [checkOS, checkMSBuild]; + // 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 () { diff --git a/bin/lib/create.js b/bin/lib/create.js index ae11869f..281cd001 100644 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -70,6 +70,10 @@ module.exports.run = function (argv) { // CB-7618 node_modules must be copied to project folder shell.cp('-r', path.join(platformRoot, 'node_modules'), path.join(projectPath, 'cordova')); + // CB-8954 Copy check_reqs module, since it will be required by 'requirements' command + shell.cp('-r', path.join(platformRoot, 'bin', 'check_reqs*'), path.join(projectPath, 'cordova')); + shell.cp('-r', path.join(platformRoot, 'bin', 'lib', 'check_reqs*'), path.join(projectPath, 'cordova', 'lib')); + // if any custom template is provided, just copy it over created project if (customTemplate && fs.existsSync(customTemplate)) { console.log('Copying template overrides from ' + customTemplate + ' to ' + projectPath); @@ -122,4 +126,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 +};