-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change cordova-lib to execute plugin hooks without the need of Cordova project structure #236
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
id="org.test.plugin-with-hooks" version="0.0.1"> | ||
|
||
<name>Plugin With Hooks</name> | ||
<license>Commercial</license> | ||
<keywords>cordova,device</keywords> | ||
<engines> | ||
<engine name="cordova" version=">=3.0.0" /> | ||
</engines> | ||
|
||
<hook type="before_plugin_install" src="scripts/beforeInstall.js" /> | ||
<hook type="after_plugin_install" src="scripts/afterInstall.js" /> | ||
<hook type="before_plugin_uninstall" src="scripts/beforeUninstall.js" /> | ||
</plugin> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = function(context) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = function(context) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = function(context) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,17 +33,21 @@ var isWindows = os.platform().slice(0, 3) === 'win'; | |
* Tries to create a HooksRunner for passed project root. | ||
* @constructor | ||
*/ | ||
function HooksRunner(projectRoot) { | ||
var root = cordovaUtil.isCordova(projectRoot); | ||
if (!root) throw new CordovaError('Not a Cordova project ("' + projectRoot + '"), can\'t use hooks.'); | ||
else this.projectRoot = root; | ||
function DefaultHooksRunner(projectRoot) { | ||
this.projectRoot = projectRoot; | ||
} | ||
|
||
DefaultHooksRunner.prototype.prepareOptions = function(opts) { | ||
opts = opts || {}; | ||
opts.projectRoot = this.projectRoot; | ||
return opts; | ||
}; | ||
|
||
/** | ||
* Fires all event handlers and scripts for a passed hook type. | ||
* Returns a promise. | ||
*/ | ||
HooksRunner.prototype.fire = function fire(hook, opts) { | ||
DefaultHooksRunner.prototype.fire = function fire(hook, opts) { | ||
// args check | ||
if (!hook) { | ||
throw new Error('hook type is not specified'); | ||
|
@@ -59,12 +63,24 @@ HooksRunner.prototype.fire = function fire(hook, opts) { | |
}); | ||
}; | ||
|
||
/** | ||
* Tries to create a CordovaHooksRunner for passed project root. | ||
* @constructor | ||
*/ | ||
function CordovaHooksRunner(projectRoot) { | ||
DefaultHooksRunner.call(this, projectRoot); | ||
var root = cordovaUtil.isCordova(projectRoot); | ||
if (!root) throw new CordovaError('Not a Cordova project ("' + projectRoot + '"), can\'t use hooks.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :nit please use |
||
else this.projectRoot = root; | ||
} | ||
|
||
require('util').inherits(CordovaHooksRunner, DefaultHooksRunner); | ||
|
||
/** | ||
* Refines passed options so that all required parameters are set. | ||
*/ | ||
HooksRunner.prototype.prepareOptions = function(opts) { | ||
opts = opts || {}; | ||
opts.projectRoot = this.projectRoot; | ||
CordovaHooksRunner.prototype.prepareOptions = function(opts) { | ||
opts = DefaultHooksRunner.prototype.prepareOptions.call(this, opts); | ||
opts.cordova = opts.cordova || {}; | ||
opts.cordova.platforms = opts.cordova.platforms || opts.platforms || cordovaUtil.listPlatforms(opts.projectRoot); | ||
opts.cordova.platforms = opts.cordova.platforms.map(function(platform) { return platform.split('@')[0]; } ); | ||
|
@@ -80,6 +96,15 @@ HooksRunner.prototype.prepareOptions = function(opts) { | |
return opts; | ||
}; | ||
|
||
function HooksRunner(projectRoot) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please provide the 'why' we are moving to have both CordovaHooksRunner and DefaultHooksRunner as a comment ? So that other maintainers are not confused as to the reason behind this shift. |
||
var root = cordovaUtil.isCordova(projectRoot); | ||
this.hooksRunner = root ? new CordovaHooksRunner(root) : new DefaultHooksRunner(projectRoot); | ||
} | ||
|
||
HooksRunner.prototype.fire = function fire(hook, opts) { | ||
return this.hooksRunner.fire(hook, opts); | ||
}; | ||
|
||
module.exports = HooksRunner; | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,11 @@ function getApplicationHookScriptsFromDir(dir) { | |
*/ | ||
function getScriptsFromConfigXml(hook, opts) { | ||
var configPath = cordovaUtil.projectConfig(opts.projectRoot); | ||
|
||
if (!(fs.existsSync(configPath))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a comment here that lets code readers know that there are cases when the cordova project structure is not available would be helpful. |
||
return []; | ||
} | ||
|
||
var configXml = new ConfigParser(configPath); | ||
|
||
return configXml.getHookScripts(hook, opts.cordova.platforms).map(function(scriptElement) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,16 +336,15 @@ function runInstall(actions, platform, project_dir, plugin_dir, plugins_dir, opt | |
} | ||
).then( | ||
function(){ | ||
var install_plugin_dir = path.join(plugins_dir, pluginInfo.id); | ||
var install_plugin_dir = path.join(plugins_dir, pluginInfo.id), | ||
run_hooks = options.hasOwnProperty('run_hooks') ? options.run_hooks : true; | ||
|
||
// may need to copy to destination... | ||
if ( !fs.existsSync(install_plugin_dir) ) { | ||
copyPlugin(plugin_dir, plugins_dir, options.link, pluginInfoProvider); | ||
} | ||
|
||
var projectRoot = cordovaUtil.isCordova(); | ||
|
||
if(projectRoot) { | ||
if(run_hooks) { | ||
// using unified hooksRunner | ||
var hookOptions = { | ||
cordova: { platforms: [ platform ] }, | ||
|
@@ -357,7 +356,7 @@ function runInstall(actions, platform, project_dir, plugin_dir, plugins_dir, opt | |
} | ||
}; | ||
|
||
var hooksRunner = new HooksRunner(projectRoot); | ||
var hooksRunner = new HooksRunner(cordovaUtil.isCordova() || project_dir); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
return hooksRunner.fire('before_plugin_install', hookOptions).then(function() { | ||
return handleInstall(actions, pluginInfo, platform, project_dir, plugins_dir, install_plugin_dir, filtered_variables, options); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,7 +220,9 @@ module.exports.uninstallPlugin = function(id, plugins_dir, options) { | |
// possible options: cli_variables, www_dir, is_top_level | ||
// Returns a promise | ||
function runUninstallPlatform(actions, platform, project_dir, plugin_dir, plugins_dir, options) { | ||
var pluginInfoProvider = options.pluginInfoProvider; | ||
var pluginInfoProvider = options.pluginInfoProvider, | ||
run_hooks = options.hasOwnProperty('run_hooks') ? options.run_hooks : true; | ||
|
||
// If this plugin is not really installed, return (CB-7004). | ||
if (!fs.existsSync(plugin_dir)) { | ||
return Q(); | ||
|
@@ -268,9 +270,7 @@ function runUninstallPlatform(actions, platform, project_dir, plugin_dir, plugin | |
promise = Q(); | ||
} | ||
|
||
var projectRoot = cordovaUtil.isCordova(); | ||
|
||
if(projectRoot) { | ||
if(run_hooks) { | ||
|
||
// using unified hooksRunner | ||
var hooksRunnerOptions = { | ||
|
@@ -283,7 +283,7 @@ function runUninstallPlatform(actions, platform, project_dir, plugin_dir, plugin | |
} | ||
}; | ||
|
||
var hooksRunner = new HooksRunner(projectRoot); | ||
var hooksRunner = new HooksRunner(cordovaUtil.isCordova() || project_dir); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why cordovaUtil.isCordova() || project_dir ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sending project_dir should be enough |
||
return promise.then(function() { | ||
return hooksRunner.fire('before_plugin_uninstall', hooksRunnerOptions); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case that deals with a NON-cordova project.