Updated Branches: refs/heads/master2 da2fab0f9 -> 1ee6a36f5
npm version 2.8.13. [CB-3462] fixed issue with build command hooks/callback not firing appropriately. refactored out build into own file. cleaned up cli-level tests. Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/1ee6a36f Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/1ee6a36f Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/1ee6a36f Branch: refs/heads/master2 Commit: 1ee6a36f5b4b69ba4cf430eadd19d7e8a22a6aad Parents: da2fab0 Author: Fil Maj <[email protected]> Authored: Fri Jun 7 00:30:07 2013 -0700 Committer: Fil Maj <[email protected]> Committed: Fri Jun 7 00:30:07 2013 -0700 ---------------------------------------------------------------------- cordova.js | 28 +------- package.json | 2 +- spec/cordova-cli/build.spec.js | 126 ++++++++++++++++++++++++++++++++ spec/cordova-cli/compile.spec.js | 49 +++---------- spec/cordova-cli/emulate.spec.js | 45 +++++------ spec/cordova-cli/platform.spec.js | 41 ++++------ spec/cordova-cli/prepare.spec.js | 22 ++---- src/build.js | 87 ++++++++++++++++++++++ src/emulate.js | 4 +- src/util.js | 1 - 10 files changed, 271 insertions(+), 134 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/cordova.js ---------------------------------------------------------------------- diff --git a/cordova.js b/cordova.js index 694089c..35b8134 100644 --- a/cordova.js +++ b/cordova.js @@ -57,31 +57,5 @@ module.exports = { }, emit: emit, trigger: emit, - build: function() { - var projectRoot = util.isCordova(process.cwd()); - if (!projectRoot) { - throw new Error('Current working directory is not a Cordova-based project.'); - } - var platforms_dir = path.join(projectRoot, 'platforms'); - var platforms = fs.readdirSync(platforms_dir); - if (platforms.length === 0) { - throw new Error('No platforms added! `cordova platform add <platform>` to add a platform.'); - } - - // fire build hooks - var hooks = new hooker(projectRoot); - hooks.fire('before_build'); - - var prep_args = Array.prototype.slice.call(arguments, 0); - var compile_args = Array.prototype.slice.call(arguments, 0); - - var self = this; - compile_args = compile_args.concat(function() { - hooks.fire('after_build'); - }); - prep_args = prep_args.concat(function() { - module.exports.compile.apply(self, compile_args); - }); - module.exports.prepare.apply(this, prep_args); - } + build: require('./src/build') }; http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/package.json ---------------------------------------------------------------------- diff --git a/package.json b/package.json index f7f781e..8171370 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova", - "version": "2.8.12", + "version": "2.8.13", "preferGlobal": "true", "description": "Cordova command line interface tool", "main": "cordova", http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/spec/cordova-cli/build.spec.js ---------------------------------------------------------------------- diff --git a/spec/cordova-cli/build.spec.js b/spec/cordova-cli/build.spec.js new file mode 100644 index 0000000..7b30414 --- /dev/null +++ b/spec/cordova-cli/build.spec.js @@ -0,0 +1,126 @@ +/** + 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 cordova = require('../../cordova'), + shell = require('shelljs'), + path = require('path'), + fs = require('fs'), + events = require('../../src/events'), + hooker = require('../../src/hooker'), + tempDir = path.join(__dirname, '..', '..', 'temp'); + +var cwd = process.cwd(); + +describe('build command', function() { + beforeEach(function() { + shell.rm('-rf', tempDir); + shell.mkdir('-p', tempDir); + }); + + describe('failure', function() { + afterEach(function() { + process.chdir(cwd); + spyOn(shell, 'exec'); + }); + it('should not run inside a Cordova-based project with no added platforms', function() { + cordova.create(tempDir); + process.chdir(tempDir); + expect(function() { + cordova.build(); + }).toThrow(); + }); + it('should not run outside of a Cordova-based project', function() { + shell.mkdir('-p', tempDir); + process.chdir(tempDir); + expect(function() { + cordova.build(); + }).toThrow(); + }); + }); + + describe('success', function() { + beforeEach(function() { + cordova.create(tempDir); + process.chdir(tempDir); + }); + afterEach(function() { + process.chdir(cwd); + }); + it('should run inside a Cordova-based project with at least one added platform and call both prepare and compile', function(done) { + var prepare_spy = spyOn(cordova, 'prepare').andCallFake(function(platforms, cb) { + cb(); + }); + var compile_spy = spyOn(cordova, 'compile').andCallFake(function(platforms, cb) { + cb(); + }); + + cordova.build(['android','ios'], function(err) { + expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios'], jasmine.any(Function)); + expect(compile_spy).toHaveBeenCalledWith(['android', 'ios'], jasmine.any(Function)); + done(); + }); + }); + }); + + describe('hooks', function() { + var hook_spy; + var prepare_spy; + var compile_spy; + beforeEach(function() { + hook_spy = spyOn(hooker.prototype, 'fire').andCallFake(function(hook, opts, cb) { + cb(); + }); + prepare_spy = spyOn(cordova, 'prepare').andCallFake(function(platforms, cb) { + cb(); + }); + compile_spy = spyOn(cordova, 'compile').andCallFake(function(platforms, cb) { + cb(); + }); + cordova.create(tempDir); + process.chdir(tempDir); + }); + afterEach(function() { + hook_spy.reset(); + prepare_spy.reset(); + compile_spy.reset(); + process.chdir(cwd); + }); + + describe('when platforms are added', function() { + it('should fire before hooks through the hooker module', function() { + cordova.build(['android', 'ios']); + expect(hook_spy).toHaveBeenCalledWith('before_build', {platforms:['android', 'ios']}, jasmine.any(Function)); + }); + it('should fire after hooks through the hooker module', function(done) { + cordova.build('android', function() { + expect(hook_spy).toHaveBeenCalledWith('after_build', {platforms:['android']}, jasmine.any(Function)); + done(); + }); + }); + }); + + describe('with no platforms added', function() { + it('should not fire the hooker', function() { + expect(function() { + cordova.build(); + }).toThrow(); + expect(hook_spy).not.toHaveBeenCalled(); + }); + }); + }); +}); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/spec/cordova-cli/compile.spec.js ---------------------------------------------------------------------- diff --git a/spec/cordova-cli/compile.spec.js b/spec/cordova-cli/compile.spec.js index 0ebfc1a..b79d889 100644 --- a/spec/cordova-cli/compile.spec.js +++ b/spec/cordova-cli/compile.spec.js @@ -22,10 +22,7 @@ var cordova = require('../../cordova'), fs = require('fs'), events = require('../../src/events'), hooker = require('../../src/hooker'), - fixtures = path.join(__dirname, '..', 'fixtures'), - hooks = path.join(fixtures, 'hooks'), - tempDir = path.join(__dirname, '..', '..', 'temp'), - cordova_project = path.join(fixtures, 'projects', 'cordova'); + tempDir = path.join(__dirname, '..', '..', 'temp'); var cwd = process.cwd(); @@ -38,7 +35,6 @@ describe('compile command', function() { describe('failure', function() { afterEach(function() { process.chdir(cwd); - spyOn(shell, 'exec'); }); it('should not run inside a Cordova-based project with no added platforms', function() { cordova.create(tempDir); @@ -59,40 +55,21 @@ describe('compile command', function() { describe('success', function() { beforeEach(function() { cordova.create(tempDir); - shell.cp('-Rf', path.join(cordova_project, 'platforms', 'android'), path.join(tempDir, 'platforms')); process.chdir(tempDir); }); afterEach(function() { process.chdir(cwd); }); - it('should run inside a Cordova-based project with at least one added platform', function() { - // move platform project fixtures over to fake cordova into thinking platforms were added - // TODO: possibly add this to helper? - var sh_spy = spyOn(shell, 'exec'); - - expect(function() { - cordova.compile(); + it('should run inside a Cordova-based project with at least one added platform and shell out to a build command', function(done) { + var sh_spy = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { + cb(0, 'mhmm'); + }); + cordova.compile(['android', 'ios'], function(err) { expect(sh_spy).toHaveBeenCalled(); expect(sh_spy.mostRecentCall.args[0]).toMatch(/cordova.build"$/gi); - }).not.toThrow(); - }); - }); - - /* Is this a repeat of the util.spec.js test? */ - it('should not treat a .gitignore file as a platform', function() { - var gitignore = path.join(cordova_project, 'platforms', '.gitignore'); - fs.writeFileSync(gitignore, 'somethinghere', 'utf-8'); - this.after(function() { - shell.rm('-f', gitignore); - process.chdir(cwd); + done(); + }); }); - - var s = spyOn(shell, 'exec'); - process.chdir(cordova_project); - cordova.compile(); - for (call in s.calls) { - expect(s.calls[call].args[0]).not.toMatch(/\.gitignore/); - } }); describe('hooks', function() { @@ -115,14 +92,9 @@ describe('compile command', function() { }); describe('when platforms are added', function() { - beforeEach(function() { - shell.mkdir(path.join(tempDir, 'platforms', 'android')); - shell.mkdir(path.join(tempDir, 'platforms', 'blackberry')); - }); - it('should fire before hooks through the hooker module', function() { - cordova.compile(); - expect(hook_spy).toHaveBeenCalledWith('before_compile', {platforms:['android', 'blackberry']}, jasmine.any(Function)); + cordova.compile(['android', 'ios']); + expect(hook_spy).toHaveBeenCalledWith('before_compile', {platforms:['android', 'ios']}, jasmine.any(Function)); }); it('should fire after hooks through the hooker module', function(done) { cordova.compile('android', function() { @@ -138,7 +110,6 @@ describe('compile command', function() { cordova.compile(); }).toThrow(); expect(hook_spy).not.toHaveBeenCalled(); - expect(hook_spy).not.toHaveBeenCalled(); }); }); }); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/spec/cordova-cli/emulate.spec.js ---------------------------------------------------------------------- diff --git a/spec/cordova-cli/emulate.spec.js b/spec/cordova-cli/emulate.spec.js index 7e4850b..4f8e14b 100644 --- a/spec/cordova-cli/emulate.spec.js +++ b/spec/cordova-cli/emulate.spec.js @@ -21,13 +21,8 @@ var cordova = require('../../cordova'), shell = require('shelljs'), path = require('path'), fs = require('fs'), - config_parser = require('../../src/config_parser'), - android_parser = require('../../src/metadata/android_parser'), hooker = require('../../src/hooker'), - fixtures = path.join(__dirname, '..', 'fixtures'), - hooks = path.join(fixtures, 'hooks'), - tempDir = path.join(__dirname, '..', '..', 'temp'), - cordova_project = path.join(fixtures, 'projects', 'cordova'); + tempDir = path.join(__dirname, '..', '..', 'temp'); var cwd = process.cwd(); @@ -59,32 +54,38 @@ describe('emulate command', function() { describe('success', function() { beforeEach(function() { - shell.cp('-Rf', path.join(cordova_project, 'platforms', 'android'), path.join(tempDir, 'platforms')); process.chdir(tempDir); + spyOn(cordova, 'prepare').andCallFake(function(ps, cb) { + cb(); + }); }); afterEach(function() { process.chdir(cwd); }); - it('should run inside a Cordova-based project with at least one added platform', function() { - var s = spyOn(shell, 'exec'); - var a_spy = spyOn(android_parser.prototype, 'update_project'); - expect(function() { - cordova.emulate(); - a_spy.mostRecentCall.args[1](); // fake out android parser + it('should run inside a Cordova-based project with at least one added platform', function(done) { + var s = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { + cb(0, 'yokay'); + }); + cordova.emulate(['android', 'beer'], function(err) { expect(s).toHaveBeenCalled(); expect(s.mostRecentCall.args[0]).toMatch(/cordova.run" --emulator$/gi); - }).not.toThrow(); + done(); + }); }); }); describe('hooks', function() { var hook_spy; var shell_spy; + var prepare_spy; beforeEach(function() { hook_spy = spyOn(hooker.prototype, 'fire').andCallFake(function(hook, opts, cb) { if (cb) cb(); else opts(); }); + prepare_spy = spyOn(cordova, 'prepare').andCallFake(function(ps, cb) { + cb(); + }); shell_spy = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { cb(0, 'yup'); // fake out shell so system thinks every shell-out is successful }); @@ -92,20 +93,17 @@ describe('emulate command', function() { }); afterEach(function() { hook_spy.reset(); + prepare_spy.reset(); shell_spy.reset(); process.chdir(cwd); }); describe('when platforms are added', function() { - var android_platform = path.join(tempDir, 'platforms', 'android'); - beforeEach(function() { - shell.mkdir('-p', path.join(android_platform, 'assets', 'www')); - fs.writeFileSync(path.join(android_platform, 'AndroidManifest.xml'), '<xml></xml>', 'utf-8'); - }); - it('should fire before hooks through the hooker module', function() { - - cordova.emulate(); - expect(hook_spy).toHaveBeenCalledWith('before_emulate', {platforms:['android']}, jasmine.any(Function)); + it('should fire before hooks through the hooker module', function(done) { + cordova.emulate(['android', 'ios'], function(err) { + expect(hook_spy).toHaveBeenCalledWith('before_emulate', {platforms:['android', 'ios']}, jasmine.any(Function)); + done(); + }); }); it('should fire after hooks through the hooker module', function(done) { cordova.emulate('android', function() { @@ -121,7 +119,6 @@ describe('emulate command', function() { cordova.emulate(); }).toThrow(); expect(hook_spy).not.toHaveBeenCalled(); - expect(hook_spy).not.toHaveBeenCalled(); }); }); }); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/spec/cordova-cli/platform.spec.js ---------------------------------------------------------------------- diff --git a/spec/cordova-cli/platform.spec.js b/spec/cordova-cli/platform.spec.js index 4e21aad..fed1f3b 100644 --- a/spec/cordova-cli/platform.spec.js +++ b/spec/cordova-cli/platform.spec.js @@ -19,20 +19,13 @@ var cordova = require('../../cordova'), path = require('path'), shell = require('shelljs'), - request = require('request'), fs = require('fs'), - et = require('elementtree'), - config_parser = require('../../src/config_parser'), - helper = require('./helper'), util = require('../../src/util'), hooker = require('../../src/hooker'), + platform = require('../../src/platform'), platforms = require('../../platforms'), - platform = require('../../src/platform'), tempDir = path.join(__dirname, '..', '..', 'temp'); - android_parser = require('../../src/metadata/android_parser'), - ios_parser = require('../../src/metadata/ios_parser'), - blackberry_parser = require('../../src/metadata/blackberry_parser'), - cordova_project = path.join(__dirname, '..', 'fixtures', 'projects', 'cordova'); + android_parser = require('../../src/metadata/android_parser'); var cwd = process.cwd(); @@ -110,14 +103,14 @@ describe('platform command', function() { process.chdir(cwd); }); - it('should handle multiple platforms', function() { + it('should handle multiple platforms and shell out to specified platform\'s bin/create', function() { spyOn(platform, 'supports').andCallFake(function(target, callback) { callback(null); }); var sh = spyOn(shell, 'exec'); cordova.platform('add', ['foo', 'bar']); var foo_create = path.join('foo', 'bin', 'create'); - var bar_create = path.join('bar', 'bin', 'create'); + var bar_create = path.join('bar', 'bin', 'create'); expect(sh.argsForCall[0][0]).toContain(foo_create); expect(sh.argsForCall[1][0]).toContain(bar_create); }); @@ -197,24 +190,22 @@ describe('platform command', function() { }); describe('add hooks', function() { var sh, cr; - var fake_reqs_check = function() { - cr.mostRecentCall.args[0](false); - }; - var fake_create = function(a_path) { - shell.mkdir('-p', a_path); - fs.writeFileSync(path.join(a_path, 'AndroidManifest.xml'), 'hi', 'utf-8'); - sh.mostRecentCall.args[2](0, ''); - }; beforeEach(function() { - sh = spyOn(shell, 'exec'); - cr = spyOn(android_parser, 'check_requirements'); + sh = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { + var a_path = path.join(tempDir, 'platforms','android'); + shell.mkdir('-p',a_path); + fs.writeFileSync(path.join(a_path, 'AndroidManifest.xml'), 'hi', 'utf-8'); + cb(0, 'mkay'); + }); + cr = spyOn(android_parser.prototype, 'update_project').andCallFake(function(cfg, cb) { + cb(); + }); + spyOn(platform, 'supports').andCallFake(function (t, cb) { + cb(); + }); }); it('should fire before and after hooks through the hooker module', function() { - var ap = spyOn(android_parser.prototype, 'update_project'); cordova.platform('add', 'android'); - fake_reqs_check(); - fake_create(path.join(tempDir, 'platforms', 'android')); - ap.mostRecentCall.args[1](); // fake out update_project expect(s).toHaveBeenCalledWith('before_platform_add', {platforms:['android']}, jasmine.any(Function)); expect(s).toHaveBeenCalledWith('after_platform_add', {platforms:['android']}, jasmine.any(Function)); }); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/spec/cordova-cli/prepare.spec.js ---------------------------------------------------------------------- diff --git a/spec/cordova-cli/prepare.spec.js b/spec/cordova-cli/prepare.spec.js index 94ca13b..d7c7d28 100644 --- a/spec/cordova-cli/prepare.spec.js +++ b/spec/cordova-cli/prepare.spec.js @@ -50,21 +50,15 @@ describe('prepare command', function() { }).toThrow(); }); - it('should run inside a Cordova-based project with at least one added platform', function() { - // move platform project fixtures over to fake cordova into thinking platforms were added - // TODO: possibly add this to helper? - this.after(function() { - process.chdir(cwd); + it('should run inside a Cordova-based project with at least one added platform', function(done) { + process.chdir(tempDir); + var android_path = path.join(tempDir, 'platforms', 'android'); + shell.mkdir(android_path); + fs.writeFileSync(path.join(android_path, 'AndroidManifest.xml'), 'hi', 'utf-8'); + spyOn(plugman, 'prepare'); + cordova.prepare(['android'], function(err) { + done(); }); - - spyOn(shell, 'exec'); - expect(function() { - shell.cp('-Rf', path.join(cordova_project, 'platforms', 'android'), path.join(tempDir, 'platforms')); - process.chdir(tempDir); - var a_parser_spy = spyOn(android_parser.prototype, 'update_project'); - cordova.prepare(); - expect(a_parser_spy).toHaveBeenCalled(); - }).not.toThrow(); }); it('should not run outside of a Cordova-based project', function() { this.after(function() { http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/src/build.js ---------------------------------------------------------------------- diff --git a/src/build.js b/src/build.js new file mode 100644 index 0000000..adc6749 --- /dev/null +++ b/src/build.js @@ -0,0 +1,87 @@ +/** + 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 util = require('./util'), + path = require('path'), + config_parser = require('./config_parser'), + fs = require('fs'), + shell = require('shelljs'), + et = require('elementtree'), + hooker = require('./hooker'), + events = require('./events'), + n = require('ncallbacks'); + +module.exports = function build(platformList, callback) { + var projectRoot = util.isCordova(process.cwd()); + if (!projectRoot) { + var err = new Error('Current working directory is not a Cordova-based project.'); + if (callback) callback(err); + else throw err; + return; + } + + if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) { + platformList = util.listPlatforms(projectRoot); + } else if (typeof platformList == 'string') platformList = [platformList]; + else if (platformList instanceof Function && callback === undefined) { + callback = platformList; + platformList = util.listPlatforms(projectRoot); + } + + if (platformList.length === 0) { + var err = new Error('No platforms added! `cordova platform add <platform>` to add a platform.'); + if (callback) callback(err); + else throw err; + return; + } + + // fire build hooks + var hooks = new hooker(projectRoot); + var opts = { + platforms:platformList + }; + hooks.fire('before_build', opts, function(err) { + if (err) { + if (callback) callback(err); + else throw err; + } else { + require('../cordova').prepare(platformList, function(err) { + if (err) { + if (callback) callback(err); + else throw err; + } else { + require('../cordova').compile(platformList, function(err) { + if (err) { + if (callback) callback(err); + else throw err; + } else { + hooks.fire('after_build', opts, function(err) { + if (err) { + if (callback) callback(err); + else throw err; + } else { + if (callback) callback(); + } + }); + } + }); + } + }); + } + });; +}; http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/src/emulate.js ---------------------------------------------------------------------- diff --git a/src/emulate.js b/src/emulate.js index b8bc4ad..5553fed 100644 --- a/src/emulate.js +++ b/src/emulate.js @@ -23,9 +23,7 @@ var cordova_util = require('./util'), platforms = require('../platforms'), platform = require('./platform'), events = require('./events'), - prepare = require('./prepare'), fs = require('fs'), - ls = fs.readdirSync, n = require('ncallbacks'), hooker = require('../src/hooker'), util = require('util'); @@ -105,7 +103,7 @@ module.exports = function emulate (platformList, callback) { }); // Run a prepare first! - prepare(platformList, function(err) { + require('../cordova').prepare(platformList, function(err) { if (err) { if (callback) callback(err); else throw err; http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/1ee6a36f/src/util.js ---------------------------------------------------------------------- diff --git a/src/util.js b/src/util.js index 1ec7813..8260b28 100644 --- a/src/util.js +++ b/src/util.js @@ -1,4 +1,3 @@ - /** Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
