cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kamrik <...@git.apache.org>
Subject [GitHub] cordova-lib pull request: CB-6481 Add unified hooks support for co...
Date Thu, 31 Jul 2014 17:16:44 GMT
Github user kamrik commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/55#discussion_r15655705
  
    --- Diff: cordova-lib/src/hooks/scriptsFinder.js ---
    @@ -0,0 +1,164 @@
    +/**
    + Licensed to the Apache Software Foundation (ASF) under one
    + or more contributor license agreements.  See the NOTICE file
    + distributed with this work for additional information
    + regarding copyright ownership.  The ASF licenses this file
    + to you 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'),
    +    cordovaUtil = require('../cordova/util'),
    +    events = require('../events'),
    +    Q = require('q'),
    +    plugin  = require('../cordova/plugin'),
    +    PluginInfo = require('../PluginInfo'),
    +    ConfigParser = require('../configparser/ConfigParser');
    +
    +/**
    + * Implements logic to retrieve hook script files defined in special folders and configuration
    + * files: config.xml, hooks/hook_type, plugins/../plugin.xml, etc
    + */
    +module.exports  = {
    +    /**
    +     * Returns all script files for the hook type specified.
    +     */
    +    getHookScripts: function(hook, opts) {
    +        // args check
    +        if (!hook) {
    +            throw new Error('hook type is not specified');
    +        }
    +        return getApplicationHookScripts(hook, opts)
    +            .concat(getPluginsHookScripts(hook, opts));
    +    }
    +};
    +
    +/**
    + * Returns script files defined on application level.
    + * They are stored in .cordova/hooks folders and in config.xml.
    + */
    +function getApplicationHookScripts(hook, opts) {
    +    // args check
    +    if (!hook) {
    +        throw new Error('hook type is not specified');
    +    }
    +    return getApplicationHookScriptsFromDir(path.join(opts.projectRoot, '.cordova', 'hooks',
hook))
    +        .concat(getApplicationHookScriptsFromDir(path.join(opts.projectRoot, 'hooks',
hook)))
    +        .concat(getScriptsFromConfigXml(hook, opts));
    +}
    +
    +/**
    + * Returns script files defined by plugin developers as part of plugin.xml.
    + */
    +function getPluginsHookScripts(hook, opts) {
    +    // args check
    +    if (!hook) {
    +        throw new Error('hook type is not specified');
    +    }
    +
    +    // In case before_plugin_install, after_plugin_install, before_plugin_uninstall hooks
we receive opts.plugin and
    +    // retrieve scripts exclusive for this plugin.
    +    if(opts.plugin) {
    +        events.emit('debug', 'Executing "' + hook + '"  hook for "' + opts.plugin.id
+ '" on ' + opts.plugin.platform + '.');
    +
    +        return getPluginScriptFiles(opts.plugin, hook, [ opts.plugin.platform ]);
    +    }
    +
    +    events.emit('debug', 'Executing "' + hook + '"  hook for all plugins.');
    +    return getAllPluginsHookScriptFiles(hook, opts);
    +}
    +
    +/**
    + * Gets application level hooks from the directrory specified.
    + */
    +function getApplicationHookScriptsFromDir(dir) {
    +    if (!(fs.existsSync(dir))) {
    +        return [];
    +    }
    +
    +    var compareNumbers = function(a, b) {
    +        // TODO SG looks very complex, do we really need this?
    +        return isNaN (parseInt(a, 10)) ? a.toLowerCase().localeCompare(b.toLowerCase
? b.toLowerCase(): b)
    +            : parseInt(a, 10) > parseInt(b, 10) ? 1 : parseInt(a, 10) < parseInt(b,
10) ? -1 : 0;
    +    };
    +
    +    var scripts = fs.readdirSync(dir).sort(compareNumbers).filter(function(s) {
    +        return s[0] != '.';
    +    });
    +    return scripts.map(function (scriptPath) {
    +        // for old style hook files we don't use module loader for backward compatibility
    +        return { path: scriptPath, fullPath: path.join(dir, scriptPath), useModuleLoader:
false };
    +    });
    +}
    +
    +/**
    + * Gets all scripts defined in config.xml with the specified type and platforms.
    + */
    +function getScriptsFromConfigXml(hook, opts) {
    +    var configPath = cordovaUtil.projectConfig(opts.projectRoot);
    +    var configXml;
    +
    +    try {
    +        configXml = new ConfigParser(configPath);
    +    } catch(ex) {
    +        events.emit('err', 'scriptsFinder could not load config.xml: ' + ex.message);
    +        console.log('scriptsFinder could not load config.xml: ' + ex.message);
    +        return [];
    --- End diff --
    
    Why do we want a missing or bad config.xml go by silently? Are there any legit cases where
this would run in a project with no config.xml file?
    
    By the way, what's the state of this PR? The code looks pretty good (just skimmed through
it, didn't read thoroughly).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message