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 2013/03/28 21:04:27 UTC

git commit: CB-2811: shell and module-level hooks should receive project root as parameter.

Updated Branches:
  refs/heads/master 37b92ff47 -> 69a49e484


CB-2811: shell and module-level hooks should receive project root as parameter.


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/69a49e48
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/69a49e48
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/69a49e48

Branch: refs/heads/master
Commit: 69a49e484a33fc54a87aeeeac77bb43fcdecf094
Parents: 37b92ff
Author: Fil Maj <ma...@gmail.com>
Authored: Thu Mar 28 13:03:53 2013 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Mar 28 13:03:53 2013 -0700

----------------------------------------------------------------------
 README.md           |    6 +++++-
 cordova.js          |   15 ++++++++++++---
 spec/hooker.spec.js |   45 ++++++++++++++++++++++++++++++++++++++++++---
 src/events.js       |    1 -
 src/hooker.js       |    5 +++--
 5 files changed, 62 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/69a49e48/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index f177fbd..48517a2 100644
--- a/README.md
+++ b/README.md
@@ -102,7 +102,9 @@ This file is what you should be editing to modify your application's metadata. A
 
 # Hooks
 
-Projects created by cordova-cli have `before` and `after` hooks for each [project command](#project_commands). There are two types of hooks: project-specific ones and module-level ones.
+Projects created by cordova-cli have `before` and `after` hooks for each [project command](#project_commands).
+
+There are two types of hooks: project-specific ones and module-level ones. Both of these types of hooks receive the project root folder as a parameter.
 
 ## Project-specific Hooks
 
@@ -116,6 +118,8 @@ These are located under the `.cordova/hooks` directory in the root of your cordo
 
 If you are using cordova-cli as a module within a larger node application, you can also use the standard `EventEmitter` methods to attach to the events. The events include `before_build`, `before_compile`, `before_docs`, `before_emulate`, `before_platform_add`, `before_platform_ls`, `before_platform_rm`, `before_plugin_add`, `before_plugin_ls`, `before_plugin_rm` and `before_prepare`. Additionally, there are `after_` flavours of all the above events.
 
+Once you `require('cordova')` in your node project, you will have the usual `EventEmitter` methods available (`on`, `off` or `removeListener`, and `emit` or `trigger`).
+
 # Examples
 
 ## Creating a new cordova project

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/69a49e48/cordova.js
----------------------------------------------------------------------
diff --git a/cordova.js b/cordova.js
index b8f5328..73d3a64 100755
--- a/cordova.js
+++ b/cordova.js
@@ -25,6 +25,14 @@ var cordova_events = require('./src/events'),
     fs             = require('fs'),
     compile        = require('./src/compile');
 
+var off = function() {
+    cordova_events.removeListener.apply(cordova_events, arguments);
+};
+
+var emit = function() {
+    cordova_events.emit.apply(cordova_events, arguments);
+};
+
 module.exports = {
     help:      require('./src/help'),
     create:    require('./src/create'),
@@ -40,9 +48,10 @@ module.exports = {
     on:        function() {
         cordova_events.on.apply(cordova_events, arguments);
     },
-    emit:      function() {
-        cordova_events.emit.apply(cordova_events, arguments);
-    },
+    off:       off,
+    removeListener:off,
+    emit:      emit,
+    trigger:   emit,
     build:     function() {
         var projectRoot = util.isCordova(process.cwd());
         if (!projectRoot) {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/69a49e48/spec/hooker.spec.js
----------------------------------------------------------------------
diff --git a/spec/hooker.spec.js b/spec/hooker.spec.js
index 1090267..4a0ca7d 100644
--- a/spec/hooker.spec.js
+++ b/spec/hooker.spec.js
@@ -1,4 +1,3 @@
-
 /**
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
@@ -90,8 +89,48 @@ describe('hooker', function() {
                     returnValue = h.fire('before_build');
                 }).not.toThrow();
                 expect(returnValue).toBe(true);
-                expect(s.calls[0].args[0]).toMatch(/0.sh$/);
-                expect(s.calls[1].args[0]).toMatch(/1.sh$/);
+                expect(s.calls[0].args[0]).toMatch(/0.sh/);
+                expect(s.calls[1].args[0]).toMatch(/1.sh/);
+            });
+            it('should pass the project root folder as parameter into the project-level hooks', function() {
+                var hook = path.join(tempDir, '.cordova', 'hooks', 'before_build');
+                shell.cp(path.join(hooks, 'test', '0.sh'), path.join(hook, '.'));
+                fs.readdirSync(hook).forEach(function(script) {
+                    fs.chmodSync(path.join(hook, script), '754');
+                });
+                var returnValue;
+                var s = spyOn(shell, 'exec').andReturn({code:0});
+                expect(function() {
+                    returnValue = h.fire('before_build');
+                }).not.toThrow();
+                expect(returnValue).toBe(true);
+                var paramRegex = new RegExp('0.sh "'+tempDir+'"$');
+                expect(s.calls[0].args[0]).toMatch(paramRegex);
+            });
+            describe('module-level hooks', function() {
+                var handler = jasmine.createSpy();
+                var test_event = 'before_build';
+                afterEach(function() {
+                    cordova.off(test_event, handler);
+                    handler.reset();
+                });
+
+                it('should fire handlers using cordova.on', function() {
+                    cordova.on(test_event, handler);
+                    h.fire(test_event);
+                    expect(handler).toHaveBeenCalled();
+                });
+                it('should pass the project root folder as parameter into the module-level handlers', function() {
+                    cordova.on(test_event, handler);
+                    h.fire('before_build');
+                    expect(handler).toHaveBeenCalledWith(tempDir);
+                });
+                it('should be able to stop listening to events using cordova.off', function() {
+                    cordova.on(test_event, handler);
+                    cordova.off(test_event, handler);
+                    h.fire('before_build');
+                    expect(handler).not.toHaveBeenCalled();
+                });
             });
         });
     });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/69a49e48/src/events.js
----------------------------------------------------------------------
diff --git a/src/events.js b/src/events.js
index 625b7e8..be40fec 100644
--- a/src/events.js
+++ b/src/events.js
@@ -1,4 +1,3 @@
-
 /**
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/69a49e48/src/hooker.js
----------------------------------------------------------------------
diff --git a/src/hooker.js b/src/hooker.js
index 8903fdb..5a0eb3c 100644
--- a/src/hooker.js
+++ b/src/hooker.js
@@ -35,14 +35,15 @@ module.exports.prototype = {
         if (!(fs.existsSync(dir))) return true; // hooks directory got axed post-create; ignore.
 
         // Fire JS hook/event
-        events.emit(hook);
+        events.emit(hook, this.root);
 
         // Fire script-based hooks
         var contents = fs.readdirSync(dir);
+        var self = this;
         contents.forEach(function(script) {
             var fullpath = path.join(dir, script);
             if (fs.statSync(fullpath).isDirectory()) return; // skip directories if they're in there.
-            var status = shell.exec(fullpath);
+            var status = shell.exec(fullpath + ' "' + self.root + '"');
             if (status.code !== 0) throw new Error('Script "' + path.basename(script) + '"' + 'in the ' + hook + ' hook exited with non-zero status code. Aborting.');
         });
         return true;