plugin hooks
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/df3fb0ad Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/df3fb0ad Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/df3fb0ad Branch: refs/heads/cordova-client Commit: df3fb0adc9ce01cc32a2db1e30a8309be9cb56bf Parents: 78ee2a1 Author: Fil Maj <maj....@gmail.com> Authored: Thu Oct 4 00:39:46 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Thu Oct 4 00:39:46 2012 -0700 ---------------------------------------------------------------------- spec/plugin.spec.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++ src/plugin.js | 11 +++++++++ 2 files changed, 66 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/df3fb0ad/spec/plugin.spec.js ---------------------------------------------------------------------- diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js index b6c9ae4..3c80671 100644 --- a/spec/plugin.spec.js +++ b/spec/plugin.spec.js @@ -2,6 +2,7 @@ var cordova = require('../cordova'), path = require('path'), shell = require('shelljs'), fs = require('fs'), + hooker = require('../src/hooker'), tempDir = path.join(__dirname, '..', 'temp'), fixturesDir = path.join(__dirname, 'fixtures'), testPlugin = path.join(fixturesDir, 'plugins', 'test'), @@ -232,5 +233,59 @@ describe('plugin 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.plugin(); + expect(s).toHaveBeenCalledWith('before_plugin_ls'); + }); + it('should fire after hooks through the hooker module', function() { + cordova.plugin(); + expect(s).toHaveBeenCalledWith('after_plugin_ls'); + }); + }); + describe('remove (rm) hooks', function() { + beforeEach(function() { + cordova.platform('add', 'android'); + spyOn(shell, 'exec').andReturn({code:0}); // fake call to pluginstall + cordova.plugin('add', androidPlugin); + s.reset(); + }); + it('should fire before hooks through the hooker module', function() { + cordova.plugin('rm', 'android'); + expect(s).toHaveBeenCalledWith('before_plugin_rm'); + }); + it('should fire after hooks through the hooker module', function() { + cordova.plugin('rm', 'android'); + expect(s).toHaveBeenCalledWith('after_plugin_rm'); + }); + }); + describe('add hooks', function() { + beforeEach(function() { + cordova.platform('add', 'android'); + spyOn(shell, 'exec').andReturn({code:0}); // fake call to pluginstall + }); + it('should fire before hooks through the hooker module', function() { + cordova.plugin('add', androidPlugin); + expect(s).toHaveBeenCalledWith('before_plugin_add'); + }); + it('should fire after hooks through the hooker module', function() { + cordova.plugin('add', androidPlugin); + expect(s).toHaveBeenCalledWith('after_plugin_add'); + }); + }); + }); }); http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/df3fb0ad/src/plugin.js ---------------------------------------------------------------------- diff --git a/src/plugin.js b/src/plugin.js index 9cedfdd..f4c7d73 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -5,6 +5,7 @@ var cordova_util = require('./util'), path = require('path'), shell = require('shelljs'), config_parser = require('./config_parser'), + hooker = require('./hooker'), platform = require('./platform'), plugin_parser = require('./plugin_parser'), ls = fs.readdirSync; @@ -17,6 +18,8 @@ module.exports = function plugin(command, target, callback) { } if (arguments.length === 0) command = 'ls'; + var hooks = new hooker(projectRoot); + var projectWww = path.join(projectRoot, 'www'); // Grab config info for the project @@ -36,6 +39,9 @@ module.exports = function plugin(command, target, callback) { switch(command) { case 'ls': case 'list': + // TODO awkward before+after hooks here + hooks.fire('before_plugin_ls'); + hooks.fire('after_plugin_ls'); if (plugins.length) { return plugins; } else return 'No plugins added. Use `cordova plugin add <plugin>`.'; @@ -69,6 +75,8 @@ module.exports = function plugin(command, target, callback) { throw 'Plugin "' + targetName + '" does not support any of your application\'s platforms. Plugin platforms: ' + pluginXml.platforms.join(', ') + '; your application\'s platforms: ' + platforms.join(', '); } + hooks.fire('before_plugin_add'); + var pluginWww = path.join(target, 'www'); var wwwContents = ls(pluginWww); var cli = path.join(__dirname, '..', 'node_modules', 'pluginstall', 'cli.js'); @@ -99,6 +107,7 @@ module.exports = function plugin(command, target, callback) { // Finally copy the plugin into the project shell.cp('-r', target, pluginPath); + hooks.fire('after_plugin_add'); if (callback) callback(); break; case 'rm': @@ -108,6 +117,7 @@ module.exports = function plugin(command, target, callback) { } // Check if we have the plugin. if (plugins.indexOf(targetName) > -1) { + hooks.fire('before_plugin_rm'); var pluginWww = path.join(pluginPath, target, 'www'); var wwwContents = ls(pluginWww); var cli = path.join(__dirname, '..', 'node_modules', 'pluginstall', 'cli.js'); @@ -144,6 +154,7 @@ module.exports = function plugin(command, target, callback) { // Finally remove the plugin dir from plugins/ shell.rm('-rf', path.join(pluginPath, target)); + hooks.fire('after_plugin_rm'); if (callback) callback(); } else { throw 'Plugin "' + targetName + '" not added to project.';