You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/10/04 09:41:40 UTC
[3/7] git commit: added hooks for build,
emulate and platform commands.
added hooks for build, emulate and platform commands.
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/78ee2a18
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/78ee2a18
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/78ee2a18
Branch: refs/heads/cordova-client
Commit: 78ee2a182d92638bdf779977a75b17de3646f60d
Parents: 5037bad
Author: Fil Maj <ma...@gmail.com>
Authored: Wed Oct 3 22:09:57 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Wed Oct 3 22:09:57 2012 -0700
----------------------------------------------------------------------
spec/build.spec.js | 3 +-
spec/emulate.spec.js | 41 ++++++++++++++++++++++++++++++++++
spec/platform.spec.js | 53 +++++++++++++++++++++++++++++++++++++++----
src/build.js | 9 +-----
src/emulate.js | 9 +++++++
src/platform.js | 21 +++++++++++++++-
6 files changed, 121 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/78ee2a18/spec/build.spec.js
----------------------------------------------------------------------
diff --git a/spec/build.spec.js b/spec/build.spec.js
index 5533b3c..f0c4c0e 100644
--- a/spec/build.spec.js
+++ b/spec/build.spec.js
@@ -273,7 +273,8 @@ describe('build command', function() {
expect(function() {
cordova.build();
}).toThrow();
- expect(s).not.toHaveBeenCalled();
+ expect(s).not.toHaveBeenCalledWith('before_build');
+ expect(s).not.toHaveBeenCalledWith('after_build');
});
});
});
http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/78ee2a18/spec/emulate.spec.js
----------------------------------------------------------------------
diff --git a/spec/emulate.spec.js b/spec/emulate.spec.js
index 189a0c2..3e48ab8 100644
--- a/spec/emulate.spec.js
+++ b/spec/emulate.spec.js
@@ -7,6 +7,7 @@ var cordova = require('../cordova'),
android_parser = require('../src/metadata/android_parser'),
ios_parser = require('../src/metadata/ios_parser'),
blackberry_parser = require('../src/metadata/blackberry_parser'),
+ hooker = require('../src/hooker'),
tempDir = path.join(__dirname, '..', 'temp');
var cwd = process.cwd();
@@ -239,4 +240,44 @@ describe('emulate command', function() {
});
});
});
+
+ describe('hooks', function() {
+ var s;
+ beforeEach(function() {
+ cordova.create(tempDir);
+ process.chdir(tempDir);
+ s = spyOn(hooker.prototype, 'fire').andReturn(true);
+ });
+ afterEach(function() {
+ process.chdir(cwd);
+ shell.rm('-rf', tempDir);
+ });
+
+ describe('when platforms are added', function() {
+ beforeEach(function() {
+ cordova.platform('add', 'android');
+ spyOn(shell, 'exec').andReturn({code:0});
+ });
+
+ it('should fire before hooks through the hooker module', function() {
+ cordova.emulate();
+ expect(s).toHaveBeenCalledWith('before_emulate');
+ });
+ it('should fire after hooks through the hooker module', function() {
+ cordova.emulate();
+ expect(s).toHaveBeenCalledWith('after_emulate');
+ });
+ });
+
+ describe('with no platforms added', function() {
+ it('should not fire the hooker', function() {
+ spyOn(shell, 'exec').andReturn({code:0});
+ expect(function() {
+ cordova.emulate();
+ }).toThrow();
+ expect(s).not.toHaveBeenCalledWith('before_emulate');
+ expect(s).not.toHaveBeenCalledWith('after_emulate');
+ });
+ });
+ });
});
http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/78ee2a18/spec/platform.spec.js
----------------------------------------------------------------------
diff --git a/spec/platform.spec.js b/spec/platform.spec.js
index 03128d8..e84ed31 100644
--- a/spec/platform.spec.js
+++ b/spec/platform.spec.js
@@ -6,13 +6,12 @@ var cordova = require('../cordova'),
config_parser = require('../src/config_parser'),
helper = require('./helper'),
util = require('../src/util'),
+ hooker = require('../src/hooker'),
platforms = require('../platforms'),
tempDir = path.join(__dirname, '..', 'temp');
-
-// globals for crazy spy hax
-android_parser = require('../src/metadata/android_parser');
-ios_parser = require('../src/metadata/ios_parser');
-blackberry_parser = require('../src/metadata/blackberry_parser');
+ android_parser = require('../src/metadata/android_parser'),
+ ios_parser = require('../src/metadata/ios_parser'),
+ blackberry_parser = require('../src/metadata/blackberry_parser');
var cwd = process.cwd();
@@ -233,4 +232,48 @@ describe('platform command', function() {
};
describe('`rm`', removing_tests('rm'));
describe('`remove`', removing_tests('remove'));
+
+ describe('hooks', function() {
+ var s;
+ beforeEach(function() {
+ cordova.create(tempDir);
+ process.chdir(tempDir);
+ s = spyOn(hooker.prototype, 'fire').andReturn(true);
+ });
+ afterEach(function() {
+ process.chdir(cwd);
+ shell.rm('-rf', tempDir);
+ });
+
+ describe('list (ls) hooks', function() {
+ it('should fire before hooks through the hooker module', function() {
+ cordova.platform();
+ expect(s).toHaveBeenCalledWith('before_platform_ls');
+ });
+ it('should fire after hooks through the hooker module', function() {
+ cordova.platform();
+ expect(s).toHaveBeenCalledWith('after_platform_ls');
+ });
+ });
+ describe('remove (rm) hooks', function() {
+ it('should fire before hooks through the hooker module', function() {
+ cordova.platform('rm', 'android');
+ expect(s).toHaveBeenCalledWith('before_platform_rm');
+ });
+ it('should fire after hooks through the hooker module', function() {
+ cordova.platform('rm', 'android');
+ expect(s).toHaveBeenCalledWith('after_platform_rm');
+ });
+ });
+ describe('add hooks', function() {
+ it('should fire before hooks through the hooker module', function() {
+ cordova.platform('add', 'android');
+ expect(s).toHaveBeenCalledWith('before_platform_add');
+ });
+ it('should fire after hooks through the hooker module', function() {
+ cordova.platform('add', 'android');
+ expect(s).toHaveBeenCalledWith('after_platform_add');
+ });
+ });
+ });
});
http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/78ee2a18/src/build.js
----------------------------------------------------------------------
diff --git a/src/build.js b/src/build.js
index 239582e..a0396b6 100644
--- a/src/build.js
+++ b/src/build.js
@@ -58,13 +58,11 @@ module.exports = function build (platforms, callback) {
if (!(hooks.fire('before_build'))) {
throw 'before_build hooks exited with non-zero code. Aborting build.';
}
- var fire_after_hooks = function() {
+
+ var end = n(platforms.length, function() {
if (!(hooks.fire('after_build'))) {
throw 'after_build hooks exited with non-zero code. Aborting.';
}
- };
-
- var end = n(platforms.length, function() {
if (callback) callback();
});
@@ -80,7 +78,6 @@ module.exports = function build (platforms, callback) {
// Update the related platform project from the config
parser.update_project(cfg);
shell_out_to_debug(projectRoot, 'android');
- fire_after_hooks();
end();
break;
case 'blackberry-10':
@@ -91,7 +88,6 @@ module.exports = function build (platforms, callback) {
parser.update_project(cfg, function() {
// Shell it
shell_out_to_debug(projectRoot, 'blackberry-10');
- fire_after_hooks();
end();
});
break;
@@ -102,7 +98,6 @@ module.exports = function build (platforms, callback) {
// Update the related platform project from the config
parser.update_project(cfg, function() {
shell_out_to_debug(projectRoot, 'ios');
- fire_after_hooks();
end();
});
break;
http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/78ee2a18/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index 5e59ec0..22509de 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -8,6 +8,7 @@ var cordova_util = require('./util'),
platform = require('./platform'),
fs = require('fs'),
n = require('ncallbacks'),
+ hooker = require('../src/hooker'),
util = require('util');
function shell_out_to_emulate(root, platform) {
@@ -49,7 +50,15 @@ module.exports = function emulate (platforms, callback) {
if (platforms.length === 0) throw 'No platforms added to this project. Please use `cordova platform add <platform>`.';
+ var hooks = new hooker(projectRoot);
+ if (!(hooks.fire('before_emulate'))) {
+ throw 'before_emulate hooks exited with non-zero code. Aborting build.';
+ }
+
var end = n(platforms.length, function() {
+ if (!(hooks.fire('after_emulate'))) {
+ throw 'after_emulate hooks exited with non-zero code. Aborting.';
+ }
if (callback) callback();
});
http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/78ee2a18/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index 305caf6..d69f7b2 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -6,6 +6,7 @@ var config_parser = require('./config_parser'),
android_parser= require('./metadata/android_parser'),
blackberry_parser= require('./metadata/blackberry_parser'),
ios_parser = require('./metadata/ios_parser'),
+ hooker = require('./hooker'),
shell = require('shelljs');
module.exports = function platform(command, target, callback) {
@@ -14,6 +15,9 @@ module.exports = function platform(command, target, callback) {
if (!projectRoot) {
throw 'Current working directory is not a Cordova-based project.';
}
+
+ var hooks = new hooker(projectRoot);
+
if (arguments.length === 0) command = 'ls';
var xml = path.join(projectRoot, 'www', 'config.xml');
@@ -22,9 +26,13 @@ module.exports = function platform(command, target, callback) {
switch(command) {
case 'ls':
case 'list':
+ // TODO before+after hooks here are awkward
+ hooks.fire('before_platform_ls');
+ hooks.fire('after_platform_ls');
return fs.readdirSync(path.join(projectRoot, 'platforms'));
break;
case 'add':
+ hooks.fire('before_platform_add');
var output = path.join(projectRoot, 'platforms', target);
// If the Cordova library for this platform is missing, get it.
@@ -54,21 +62,30 @@ module.exports = function platform(command, target, callback) {
case 'android':
var android = new android_parser(output);
android.update_project(cfg);
+ hooks.fire('after_platform_add');
if (callback) callback();
break;
case 'ios':
var ios = new ios_parser(output);
- ios.update_project(cfg, callback);
+ ios.update_project(cfg, function() {
+ hooks.fire('after_platform_add');
+ callback();
+ });
break;
case 'blackberry':
var bb = new blackberry_parser(output);
- bb.update_project(cfg, callback);
+ bb.update_project(cfg, function() {
+ hooks.fire('after_platform_add');
+ callback();
+ });
break;
}
break;
case 'rm':
case 'remove':
+ hooks.fire('before_platform_rm');
shell.rm('-rf', path.join(projectRoot, 'platforms', target));
+ hooks.fire('after_platform_rm');
break;
default:
throw ('Unrecognized command "' + command + '". Use either `add`, `remove`, or `list`.');