Repository: cordova-windows Updated Branches: refs/heads/master b0502ae45 -> e22e51e58
CB-11548 windows: Respect user-specified msbuild location This closes #184 Project: http://git-wip-us.apache.org/repos/asf/cordova-windows/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-windows/commit/e22e51e5 Tree: http://git-wip-us.apache.org/repos/asf/cordova-windows/tree/e22e51e5 Diff: http://git-wip-us.apache.org/repos/asf/cordova-windows/diff/e22e51e5 Branch: refs/heads/master Commit: e22e51e588c9303b7b338d9fa9ea266ab824a14f Parents: b0502ae Author: Vladimir Kotikov <[email protected]> Authored: Thu Jun 30 09:30:03 2016 +0200 Committer: Vladimir Kotikov <[email protected]> Committed: Fri Jul 8 16:15:53 2016 +0300 ---------------------------------------------------------------------- spec/unit/MSBuildTools.spec.js | 51 +++++++++++++++++++++++++-- spec/unit/build.spec.js | 57 ++++++++++++++++++++----------- template/cordova/lib/MSBuildTools.js | 22 +++++++++++- template/cordova/lib/build.js | 27 ++++++++++++--- 4 files changed, 129 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/e22e51e5/spec/unit/MSBuildTools.spec.js ---------------------------------------------------------------------- diff --git a/spec/unit/MSBuildTools.spec.js b/spec/unit/MSBuildTools.spec.js index 4850946..442479b 100644 --- a/spec/unit/MSBuildTools.spec.js +++ b/spec/unit/MSBuildTools.spec.js @@ -21,6 +21,7 @@ var shell = require('shelljs'); var rewire = require('rewire'); var platformRoot = '../../template'; var buildTools = rewire(platformRoot + '/cordova/lib/MSBuildTools.js'); +var MSBuildTools = buildTools.__get__('MSBuildTools'); var Version = require(platformRoot + '/cordova/lib/Version.js'); var fakeToolsPath = function (version) { @@ -134,8 +135,6 @@ describe('checkMSBuildVersion method', function(){ }); describe('MSBuildTools object', function(){ - var MSBuildTools = buildTools.__get__('MSBuildTools'); - it('spec.9 should have fields and methods defined', function() { var version = '14.0', toolsPath = fakeToolsPath(version), @@ -200,3 +199,51 @@ describe('getAvailableUAPVersions method', function(){ }); }); }); + +describe('getMSBuildToolsAt method', function () { + + var fakePath = '/some/fake/path'; + var messyPath = '/another/fake/path'; + var fakeVersion = '22.0.12635.5'; + var fakeVersionParsed = '22.0'; + + var fail = jasmine.createSpy('fail'); + var success = jasmine.createSpy('success'); + + var spawnOriginal = buildTools.__get__('spawn'); + var spawnSpy = jasmine.createSpy('spawn'); + + beforeEach(function () { + buildTools.__set__('spawn', spawnSpy); + }); + + afterEach(function () { + buildTools.__set__('spawn', spawnOriginal); + }); + + it('should return MSBuildTools instance', function (done) { + spawnSpy.andReturn(Q(fakeVersion)); + + buildTools.getMSBuildToolsAt(fakePath) + .then(function (tools) { + expect(tools).toEqual(jasmine.any(MSBuildTools)); + expect(tools.version).toBe(fakeVersionParsed); + expect(tools.path).toBe(fakePath); + }, fail) + .done(function () { + expect(fail).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should reject promise if no msbuild found', function (done) { + spawnSpy.andReturn(Q.reject()); + + buildTools.getMSBuildToolsAt(messyPath) + .then(success, fail) + .done(function () { + expect(success).not.toHaveBeenCalled(); + done(); + }); + }); +}); http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/e22e51e5/spec/unit/build.spec.js ---------------------------------------------------------------------- diff --git a/spec/unit/build.spec.js b/spec/unit/build.spec.js index 96b1fac..396baa2 100644 --- a/spec/unit/build.spec.js +++ b/spec/unit/build.spec.js @@ -26,7 +26,9 @@ var Q = require('q'), build = rewire(platformRoot + '/cordova/lib/build.js'); var utils = require(platformRoot + '/cordova/lib/utils'); +var package = require(platformRoot + '/cordova/lib/package'); var AppxManifest = require(platformRoot + '/cordova/lib/AppxManifest'); +var MSBuildTools = require(platformRoot + '/cordova/lib/MSBuildTools'); function createFindAvailableVersionMock(version, path, buildSpy) { build.__set__('MSBuildTools.findAvailableVersion', function() { @@ -88,6 +90,7 @@ describe('run method', function() { spyOn(prepare, 'applyPlatformConfig'); spyOn(prepare, 'addBOMSignature'); spyOn(prepare, 'updateBuildConfig'); + spyOn(package, 'getPackage').andReturn(Q({})); spyOn(AppxManifest, 'get').andReturn({ getIdentity: function () { @@ -119,34 +122,24 @@ describe('run method', function() { }); }); - it('spec.2 should reject if both debug and release args specified', function(done) { - var rejectSpy = jasmine.createSpy(), - buildSpy = jasmine.createSpy(); + it('spec.2 should throw if both debug and release args specified', function() { + var buildSpy = jasmine.createSpy(); createFindAvailableVersionMock('14.0', testPath, buildSpy); - build.run([ 'node', buildPath, '--release', '--debug' ]) - .fail(rejectSpy) - .finally(function() { - expect(buildSpy).not.toHaveBeenCalled(); - expect(rejectSpy).toHaveBeenCalled(); - done(); - }); + expect(function () { + build.run({release: true, debug: true}); + }).toThrow(); }); - it('spec.3 should reject if both phone and win args specified', function(done) { - var rejectSpy = jasmine.createSpy(), - buildSpy = jasmine.createSpy(); + it('spec.3 should throw if both phone and win args specified', function() { + var buildSpy = jasmine.createSpy(); createFindAvailableVersionMock('14.0', testPath, buildSpy); - build.run([ 'node', buildPath, '--phone', '--win' ]) - .fail(rejectSpy) - .finally(function() { - expect(buildSpy).not.toHaveBeenCalled(); - expect(rejectSpy).toHaveBeenCalled(); - done(); - }); + expect(function () { + build.run({argv: [ '--phone', '--win' ]}); + }).toThrow(); }); it('should respect build configuration from \'buildConfig\' option', function (done) { @@ -357,4 +350,28 @@ describe('run method', function() { done(); }); }); + + it('spec.14 should use user-specified msbuild if VSINSTALLDIR variable is set', function (done) { + var customMSBuildPath = '/some/path'; + var customMSBuildVersion = '15.0'; + process.env.VSINSTALLDIR = customMSBuildPath; + + spyOn(MSBuildTools, 'getMSBuildToolsAt') + .andReturn(Q({ + path: customMSBuildPath, + version: customMSBuildVersion, + buildProject: jasmine.createSpy('buildProject').andReturn(Q()) + })); + + var fail = jasmine.createSpy('fail'); + + build.run({}) + .fail(fail) + .finally(function() { + expect(fail).not.toHaveBeenCalled(); + expect(MSBuildTools.getMSBuildToolsAt).toHaveBeenCalledWith(customMSBuildPath); + delete process.env.VSINSTALLDIR; + done(); + }); + }); }); http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/e22e51e5/template/cordova/lib/MSBuildTools.js ---------------------------------------------------------------------- diff --git a/template/cordova/lib/MSBuildTools.js b/template/cordova/lib/MSBuildTools.js index 72208e8..1296f32 100644 --- a/template/cordova/lib/MSBuildTools.js +++ b/template/cordova/lib/MSBuildTools.js @@ -33,7 +33,7 @@ MSBuildTools.prototype.buildProject = function(projFile, buildType, buildarch, o events.emit('log', 'Building project: ' + projFile); events.emit('log', '\tConfiguration : ' + buildType); events.emit('log', '\tPlatform : ' + buildarch); - + var checkWinSDK = function (target_platform) { return require('./check_reqs').isWinSDKPresent(target_platform); }; @@ -95,6 +95,26 @@ module.exports.findAllAvailableVersions = function () { }); }; +/** + * Gets MSBuildTools instance for user-specified location + * + * @param {String} location FS location where to search for MSBuild + * @returns Promise<MSBuildTools> The MSBuildTools instance at specified location + */ +function getMSBuildToolsAt(location) { + var msbuildExe = path.resolve(location, 'msbuild'); + + // TODO: can we account on these params availability and printed version format? + return spawn(msbuildExe, ['-version', '-nologo']) + .then(function (output) { + // MSBuild prints its' version as 14.0.25123.0, so we pick only first 2 segments + var version = output.match(/^(\d+\.\d+)/)[1]; + return new MSBuildTools(version, location); + }); +} + +module.exports.getMSBuildToolsAt = getMSBuildToolsAt; + function checkMSBuildVersion(version) { return spawn('reg', ['query', 'HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\' + version, '/v', 'MSBuildToolsPath']) .then(function(output) { http://git-wip-us.apache.org/repos/asf/cordova-windows/blob/e22e51e5/template/cordova/lib/build.js ---------------------------------------------------------------------- diff --git a/template/cordova/lib/build.js b/template/cordova/lib/build.js index e389041..b69afa5 100644 --- a/template/cordova/lib/build.js +++ b/template/cordova/lib/build.js @@ -58,7 +58,23 @@ module.exports.run = function run (buildOptions) { var buildConfig = parseAndValidateArgs(buildOptions); - return MSBuildTools.findAllAvailableVersions() + return Q().then(function () { + // CB-something use VSINSTALLDIR environment if defined to find MSBuild + // If VSINSTALLDIR is not specified use default discovery mechanism + if (!process.env.VSINSTALLDIR) { + return MSBuildTools.findAllAvailableVersions(); + } + + events.emit('log', 'Found VSINSTALLDIR environment variable. Attempting to build project using that version of MSBuild'); + + return MSBuildTools.getMSBuildToolsAt(process.env.VSINSTALLDIR) + .then(function (tools) { return [tools]; }) + .catch(function (err) { + // If we failed to find msbuild at VSINSTALLDIR + // location we fall back to default discovery + return MSBuildTools.findAllAvailableVersions(); + }); + }) .then(function(msbuildTools) { // Apply build related configs prepare.updateBuildConfig(buildConfig); @@ -71,10 +87,11 @@ module.exports.run = function run (buildOptions) { } cleanIntermediates(); - return buildTargets(msbuildTools, buildConfig).then(function(pkg) { - events.emit('verbose', ' BUILD OUTPUT: ' + pkg.appx); - return pkg; - }); + return buildTargets(msbuildTools, buildConfig); + }) + .then(function(pkg) { + events.emit('verbose', ' BUILD OUTPUT: ' + pkg.appx); + return pkg; }); }; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
