[ 
https://issues.apache.org/jira/browse/CB-6481?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13986708#comment-13986708
 ] 

ASF GitHub Bot commented on CB-6481:
------------------------------------

Github user agrieve commented on a diff in the pull request:

    https://github.com/apache/cordova-plugman/pull/74#discussion_r12193181
  
    --- Diff: src/util/hooks.js ---
    @@ -0,0 +1,124 @@
    +/*
    + * Copyright (c) Microsoft Open Technologies, Inc.  
    + * 
    + * Licensed under the Apache License, Version 2.0 (the "License"); 
    + * you may not use this file except in compliance with the License. 
    + * You may obtain a copy of the License at 
    + * 
    + *     http://www.apache.org/licenses/LICENSE-2.0 
    + * 
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +var path = require('path'),
    +    fs   = require('fs'),
    +    child_process = require('child_process'),
    +    Q = require('q'),
    +    events = require('../events'),
    +    os = require('os'),
    +    isWindows = (os.platform().substr(0,3) === 'win');
    +
    +/**
    + * Implements functionality to run plugin level hooks. 
    + * Hooks are defined in plugin config file as <script> elements.
    + */
    +module.exports = {
    +    /**
    +     * Fires specific plugin hook: 'preinstall', 'afterinstall', 
'uninstall', etc.
    +     * Async. Returns promise.
    +     */
    +    fire : function(type, plugin_id, pluginElement, platform, project_dir, 
plugin_dir) {
    +        // args check
    +        if (!type) {
    +            throw Error('hook type is not specified');
    +        }
    +
    +        events.emit('debug', 'Executing "' + type + '"  hook for "' + 
plugin_id + '" on ' + platform + '.');
    +        
    +        var scriptTypes = getScriptTypesForHook(type);
    +        if (scriptTypes == null) {
    +            throw Error('unknown plugin hook type: "' + type + '"' );
    +        }
    +
    +        var scriptFilesToRun = getScriptFiles(pluginElement, scriptTypes, 
platform);
    +            context = {
    +                platform: platform,
    +                projectDir: project_dir,
    +                pluginDir: plugin_dir,
    +                cmdLine: process.argv.join(' '),
    +                pluginId: plugin_id
    +            };
    +
    +        return runScripts(scriptFilesToRun, context);
    +    }
    +};
    +
    +/**
    + * Returns all script types represented corresponding hook event.
    + * Allows to use multiple script types for the same hook event (usage 
simplicity),
    + * For example: 
    + * <script type='install' .. /> or <script type='postinstall' .. /> could 
be used 
    + * to define 'afterinstall' hook.
    + */
    +function getScriptTypesForHook(hookType) {
    +    var knownTypes = {
    +        beforeinstall: ['beforeinstall', 'preinstall'],
    +        afterinstall: ['install', 'afterinstall', 'postinstall'],
    +        uninstall: ['uninstall']
    +    }
    +
    +     return knownTypes[hookType.toLowerCase()];
    +}
    +
    +/**
    + * Gets all scripts from the plugin xml with the specified types.
    + */
    +function getScriptFiles(pluginElement, scriptTypes, platform) {
    +    var scriptFiles= [],
    +        scriptElements =  pluginElement.findall('./script').concat(
    +            pluginElement.findall('./platform[@name="' + platform + 
'"]/script'));
    +
    +    var pendingScripts = [];
    +
    +    scriptElements.forEach(function(script){
    +        if (script.attrib.type && 
scriptTypes.indexOf(script.attrib.type.toLowerCase()) > -1 && 
script.attrib.src) {
    +            scriptFiles.push(script.attrib.src);
    +        }
    +    });
    +    return scriptFiles;
    +}
    +
    +/**
    + * Async runs the script files.
    + */
    +function runScripts(scripts, context) {
    +    var pendingScripts = [];
    +
    +    scripts.forEach(function(script){
    +        pendingScripts.push(runScriptFile(script, context));
    --- End diff --
    
    This runs all scripts at the same time. Probably want to run them serially.


> adds plugin level hooks support
> -------------------------------
>
>                 Key: CB-6481
>                 URL: https://issues.apache.org/jira/browse/CB-6481
>             Project: Apache Cordova
>          Issue Type: New Feature
>          Components: CLI, Plugman
>            Reporter: Sergey Grebnov
>            Assignee: Sergey Grebnov
>
> As per "Proposal: hooks support for plugins" dev mail thread discussion
> Hi, I have an idea how we can add more flexibility to plugin developers.
> Note, right now we have Application Developers – someone who use Cordova for 
> developing applications and Plugin Developers – someone who creates plugins 
> so that Application Developers can use them. For Application Developers we 
> expose  hooks so that they can customize their build/package/etc process. I 
> want us to provide similar sort of flexibility to Plugin Developers so that 
> they can go beyond of <source/>, <framework/>  tags and get mechanism to add 
> custom installation,  build logic required by a plugin. Example usage will 
> include: downloading/compiling additional binaries, marking source file to be 
> copied to output dir, changing target build platform,  etc. At present time 
> the steps described could be only achieved by hooks manually added by 
> Application Developer, but the right way is to allow Plugin Developer to 
> expose this as part of plugin definition.
> Example configuration could look like
> ```
> <script type="postinstall" src="scripts/postinstall.js" />
> <script type="preinstall" src="scripts/preinstall.js" />
> <script type="install" src="scripts/install.js" />
> ```
> beforeinstall/preinstall – run before plugin is installed
> install/postinstall/afterinstall – run after plugin is installed
> uninstall – run after plugin is uninstalled



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to