reworked build flow. build tests now run fast! start of incorporating shelljs
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/commit/04bd2490 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/04bd2490 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/04bd2490 Branch: refs/heads/cordova-client Commit: 04bd24903d5f91cfeade08c07cb86b3c1e2af75d Parents: f9c043e Author: Fil Maj <maj....@gmail.com> Authored: Sat Sep 22 23:41:37 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Sat Sep 22 23:41:37 2012 -0700 ---------------------------------------------------------------------- package.json | 4 +- spec/build.spec.js | 23 ++---- spec/fixtures/projects/test/www/config.xml | 4 +- src/build.js | 101 ++++++++++++----------- 4 files changed, 65 insertions(+), 67 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/04bd2490/package.json ---------------------------------------------------------------------- diff --git a/package.json b/package.json index 9f419e1..01d0747 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,9 @@ "pluginstall":"git://github.com/imhotep/pluginstall.git", "node-xcode":"git://github.com/imhotep/node-xcode.git", "express":"3.0", - "asyncblock":"2.1.4" + "asyncblock":"2.1.4", + "shelljs":"0.0.4", + "ncallbacks":"1.0.0" }, "devDependencies": { "jasmine-node":">=1.0.0" http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/04bd2490/spec/build.spec.js ---------------------------------------------------------------------- diff --git a/spec/build.spec.js b/spec/build.spec.js index cd8ee9e..49b4bac 100644 --- a/spec/build.spec.js +++ b/spec/build.spec.js @@ -44,9 +44,11 @@ describe('build command', function() { waitsFor(function() { return cb.wasCalled; }, 'platform add android callback'); runs(function() { + var s = spyOn(require('shelljs'), 'exec').andReturn({code:0}); expect(function() { cordova.build(buildcb); }).not.toThrow(); + expect(s).toHaveBeenCalled(); }); waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000); }); @@ -81,37 +83,28 @@ describe('build command', function() { waitsFor(function() { return cb.wasCalled; }, 'platform add android callback'); runs(function() { + var s = spyOn(require('shelljs'), 'exec').andReturn({code:0}); cordova.build(buildcb); - }); - waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000); - runs(function() { - var binaryPath = path.join(tempDir, 'platforms','android','bin'); - // Check that "bin" dir of android native proj has at - // least one file ennding in ".apk" - expect(fs.readdirSync(binaryPath) - .filter(function(e) { - return e.indexOf('.apk', e.length - 4) !== -1; - }).length > 0).toBe(true); + expect(s.mostRecentCall.args[0].match(/android\/cordova\/debug > \/dev\/null$/)).not.toBeNull(); }); }); it('should shelll out to debug command on iOS', function() { var buildcb = jasmine.createSpy(); var cb = jasmine.createSpy(); + var s; runs(function() { cordova.platform('add', 'ios', cb); }); waitsFor(function() { return cb.wasCalled; }, 'platform add ios callback'); runs(function() { + s = spyOn(require('shelljs'), 'exec').andReturn({code:0}); cordova.build(buildcb); }); waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000); runs(function() { - var binaryPath = path.join(tempDir, 'platforms','ios','build'); - expect(fs.existsSync(binaryPath)).toBe(true); - - var appPath = path.join(binaryPath,"Hello Cordova.app"); - expect(fs.existsSync(appPath)).toBe(true); + expect(s).toHaveBeenCalled(); + expect(s.mostRecentCall.args[0].match(/ios\/cordova\/debug > \/dev\/null$/)).not.toBeNull(); }); }); }); http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/04bd2490/spec/fixtures/projects/test/www/config.xml ---------------------------------------------------------------------- diff --git a/spec/fixtures/projects/test/www/config.xml b/spec/fixtures/projects/test/www/config.xml index ef2dfd9..7926c49 100644 --- a/spec/fixtures/projects/test/www/config.xml +++ b/spec/fixtures/projects/test/www/config.xml @@ -1,2 +1,4 @@ <?xml version='1.0' encoding='utf-8'?> -<widget id="com.phonegap.hello-world" version="2.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
 <name>bond. james bond.</name></widget> \ No newline at end of file +<widget id="com.phonegap.hello-world" version="2.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0"> + <name>bond. james bond.</name> +</widget> http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/04bd2490/src/build.js ---------------------------------------------------------------------- diff --git a/src/build.js b/src/build.js index 178c5d1..f916bc6 100644 --- a/src/build.js +++ b/src/build.js @@ -1,15 +1,34 @@ var cordova_util = require('./util'), path = require('path'), exec = require('child_process').exec, - wrench = require('wrench'), - rmrf = wrench.rmdirSyncRecursive, - cpr = wrench.copyDirSyncRecursive, config_parser = require('./config_parser'), fs = require('fs'), - asyncblock = require('asyncblock'), + shell = require('shelljs'), + wrench = require('wrench'), + cpr = wrench.copyDirSyncRecursive, et = require('elementtree'), + android_parser= require('./metadata/android_parser'), + ios_parser = require('./metadata/ios_parser'), + n = require('ncallbacks'), util = require('util'); +function shell_out_to_debug(projectRoot, platform, www_target, js) { + // Clean out the existing www. + shell.rm('-rf', www_target); + + // Copy app assets into native package + cpr(path.join(projectRoot, 'www'), www_target); + + // Copy in the appropriate JS + var jsPath = path.join(www_target, 'cordova.js'); + fs.writeFileSync(jsPath, fs.readFileSync(js)); + + // shell out to debug command + var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'debug > /dev/null'); + var response = shell.exec(cmd, {silent:true}); + if (response.code > 0) throw 'An error occurred while building the ' + platform + ' project. ' + response.output; +} + module.exports = function build (callback) { var projectRoot = cordova_util.isCordova(process.cwd()); @@ -20,57 +39,39 @@ module.exports = function build (callback) { var xml = path.join(projectRoot, 'www', 'config.xml'); var assets = path.join(projectRoot, 'www'); var cfg = new config_parser(xml); - var name = cfg.name(); - var id = cfg.packageName(); var platforms = cfg.ls_platforms(); if (platforms.length === 0) throw 'No platforms added to this project. Please use `cordova platform add <platform>`.'; - asyncblock(function(flow) { - - // Iterate over each added platform - platforms.map(function(platform) { - // Figure out paths based on platform - var assetsPath, js; - switch (platform) { - case 'android': - assetsPath = path.join(projectRoot, 'platforms', 'android', 'assets', 'www'); - js = path.join(__dirname, '..', 'lib', 'android', 'framework', 'assets', 'js', 'cordova.android.js'); - - // update activity name - var stringsXml = path.join(projectRoot, 'platforms', 'android', 'res', 'values', 'strings.xml'); - var strings = new et.ElementTree(et.XML(fs.readFileSync(stringsXml, 'utf-8'))); - strings.find('string[@name="app_name"]').text = name; - fs.writeFileSync(stringsXml, strings.write({indent: 4}), 'utf-8'); - break; - case 'ios': - assetsPath = path.join(projectRoot, 'platforms', 'ios', 'www'); - js = path.join(__dirname, '..', 'lib', 'ios', 'CordovaLib', 'javascript', 'cordova.ios.js'); - - // TODO: update activity name - break; - } - - // Clean out the existing www. - rmrf(assetsPath); - - // Copy app assets into native package - cpr(assets, assetsPath); + var end = n(platforms.length, function() { + if (callback) callback(); + }); - // Copy in the appropriate JS - var jsPath = path.join(assetsPath, 'cordova.js'); - fs.writeFileSync(jsPath, fs.readFileSync(js)); + // Iterate over each added platform + platforms.forEach(function(platform) { + // Figure out paths based on platform + var assetsPath, js, parser; + switch (platform) { + case 'android': + assetsPath = path.join(projectRoot, 'platforms', 'android', 'assets', 'www'); + js = path.join(__dirname, '..', 'lib', 'android', 'framework', 'assets', 'js', 'cordova.android.js'); + parser = new android_parser(path.join(projectRoot, 'platforms', 'android')); + // Update the related platform project from the config + parser.update_from_config(cfg); + shell_out_to_debug(projectRoot, 'android', assetsPath, js); + end(); + break; + case 'ios': + assetsPath = path.join(projectRoot, 'platforms', 'ios', 'www'); + js = path.join(__dirname, '..', 'lib', 'ios', 'CordovaLib', 'javascript', 'cordova.ios.js'); + parser = new ios_parser(path.join(projectRoot, 'platforms', 'ios')); + // Update the related platform project from the config + parser.update_from_config(cfg, function() { + shell_out_to_debug(projectRoot, 'ios', assetsPath, js); + end(); + }); + break; + } - // shell out to debug command - var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'debug > /dev/null'); - exec(cmd, flow.set({ - key:'debug', - firstArgIsError:false, - responseFormat:['err', 'stdout', 'stderr'] - })); - var buffers = flow.get('debug'); - if (buffers.err) throw 'An error occurred while building the ' + platform + ' project. ' + buffers.err; - if (callback) callback(); - }); }); };