cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [1/2] git commit: CB-5006: Add --searchpath option for local plugin search path
Date Tue, 07 Jan 2014 22:59:12 GMT
Updated Branches:
  refs/heads/master 6d8c1c777 -> c8e1cb998


CB-5006: Add --searchpath option for local plugin search path


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

Branch: refs/heads/master
Commit: c8e1cb9982c243e6c0674e8c7bb799062c109d05
Parents: 9abd3d8
Author: Mark Koudritsky <kamrik@chromium.org>
Authored: Tue Jan 7 16:06:58 2014 -0500
Committer: Braden Shepherdson <braden@chromium.org>
Committed: Tue Jan 7 17:58:30 2014 -0500

----------------------------------------------------------------------
 doc/help.txt   |   1 +
 main.js        |   1 +
 plugman.js     |   3 +-
 src/fetch.js   | 153 +++++++++++++++++++++++++++++++---------------------
 src/install.js |   2 +-
 5 files changed, 98 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/c8e1cb99/doc/help.txt
----------------------------------------------------------------------
diff --git a/doc/help.txt b/doc/help.txt
index 0c6ad7a..1ab3467 100644
--- a/doc/help.txt
+++ b/doc/help.txt
@@ -28,6 +28,7 @@ Optional parameters
 
  - www <directory>: www assets for the plugin will be installed into this directory.
Default is to install into the standard www directory for the platform specified
  - plugins_dir <directory>: a copy of the plugin will be stored in this directory.
Default is to install into the <project directory>/plugins folder
+ - searchpath <directory>: a colon separated list of directories to scan for a plugin
with the provided ID before looking on http://plugins.cordova.io
 
 Optional flags
 --------------

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/c8e1cb99/main.js
----------------------------------------------------------------------
diff --git a/main.js b/main.js
index 5fa6422..877c563 100755
--- a/main.js
+++ b/main.js
@@ -38,6 +38,7 @@ var known_opts = { 'platform' : [ 'ios', 'android', 'amazon-fireos', 'blackberry
         , 'link': Boolean
         , 'variable' : Array
         , 'www': path
+        , 'searchpath' : String
 }, shortHands = { 'var' : ['--variable'], 'v': ['--version'], 'h': ['--help'] };
 
 var cli_opts = nopt(known_opts, shortHands);

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/c8e1cb99/plugman.js
----------------------------------------------------------------------
diff --git a/plugman.js b/plugman.js
index 7f7ac5c..4c0f61b 100755
--- a/plugman.js
+++ b/plugman.js
@@ -97,7 +97,8 @@ plugman.commands =  {
         var opts = {
             subdir: '.',
             cli_variables: cli_variables,
-            www_dir: cli_opts.www
+            www_dir: cli_opts.www,
+            searchpath: cli_opts.searchpath ? cli_opts.searchpath.split(':') : []
         };
         return plugman.install(cli_opts.platform, cli_opts.project, cli_opts.plugin, cli_opts.plugins_dir,
opts);
     },

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/c8e1cb99/src/fetch.js
----------------------------------------------------------------------
diff --git a/src/fetch.js b/src/fetch.js
index 0d50187..2f45f5f 100644
--- a/src/fetch.js
+++ b/src/fetch.js
@@ -10,16 +10,17 @@ var shell   = require('shelljs'),
 // XXX: leave the require('../plugman') because jasmine shits itself if you declare it up
top
 // possible options: link, subdir, git_ref, client, expected_id
 // Returns a promise.
-module.exports = function fetchPlugin(plugin_dir, plugins_dir, options) {
-    require('../plugman').emit('log', 'Fetching plugin from "' + plugin_dir + '"...');
+module.exports = function fetchPlugin(plugin_src, plugins_dir, options) {
+    require('../plugman').emit('log', 'Fetching plugin from "' + plugin_src + '"...');
     // Ensure the containing directory exists.
     shell.mkdir('-p', plugins_dir);
 
     options = options || {};
     options.subdir = options.subdir || '.';
+    options.searchpath = options.searchpath || [];
 
     // clone from git repository
-    var uri = url.parse(plugin_dir);
+    var uri = url.parse(plugin_src);
 
     // If the hash exists, it has the form from npm: http://foo.com/bar#git-ref[:subdir]
     // NB: No leading or trailing slash on the subdir.
@@ -32,27 +33,28 @@ module.exports = function fetchPlugin(plugin_dir, plugins_dir, options)
{
                 options.subdir = result[2];
 
             // Recurse and exit with the new options and truncated URL.
-            var new_dir = plugin_dir.substring(0, plugin_dir.indexOf('#'));
+            var new_dir = plugin_src.substring(0, plugin_src.indexOf('#'));
             return fetchPlugin(new_dir, plugins_dir, options);
         }
     }
 
-    if ( uri.protocol && uri.protocol != 'file:' && !plugin_dir.match(/^\w+:\\/))
{
+    // If it looks like a network URL, git clone it.
+    if ( uri.protocol && uri.protocol != 'file:' && !plugin_src.match(/^\w+:\\/))
{
         if (options.link) {
             return Q.reject(new Error('--link is not supported for git URLs'));
         } else {
             var data = {
                 source: {
                     type: 'git',
-                    url:  plugin_dir,
+                    url:  plugin_src,
                     subdir: options.subdir,
                     ref: options.git_ref
                 }
             };
 
-            return plugins.clonePluginGitRepo(plugin_dir, plugins_dir, options.subdir, options.git_ref)
+            return plugins.clonePluginGitRepo(plugin_src, plugins_dir, options.subdir, options.git_ref)
             .then(function(dir) {
-                return checkID(options, dir);
+                return checkID(options.expected_id, dir);
             })
             .then(function(dir) {
                 metadata.save_fetch_metadata(dir, data);
@@ -60,70 +62,101 @@ module.exports = function fetchPlugin(plugin_dir, plugins_dir, options)
{
             });
         }
     } else {
+        // If it's not a network URL, it's either a local path or a plugin ID.
 
-        // Copy from the local filesystem.
-        // First, read the plugin.xml and grab the ID.
         // NOTE: Can't use uri.href here as it will convert spaces to %20 and make path invalid.
-        // Use original plugin_dir value instead.
-        plugin_dir = path.join(plugin_dir, options.subdir);
-
-        var linkable = true;
-        var movePlugin = function(plugin_dir) {
-            var plugin_xml_path = path.join(plugin_dir, 'plugin.xml');
-            require('../plugman').emit('verbose', 'Fetch is reading plugin.xml from location
"' + plugin_xml_path + '"...');
-            var xml = xml_helpers.parseElementtreeSync(plugin_xml_path);
-            var plugin_id = xml.getroot().attrib.id;
-
-            var dest = path.join(plugins_dir, plugin_id);
-
-            shell.rm('-rf', dest);
-            if (options.link && linkable) {
-                require('../plugman').emit('verbose', 'Symlinking from location "' + plugin_dir
+ '" to location "' + dest + '"');
-                fs.symlinkSync(plugin_dir, dest, 'dir');
-            } else {
-                shell.mkdir('-p', dest);
-                require('../plugman').emit('verbose', 'Copying from location "' + plugin_dir
+ '" to location "' + dest + '"');
-                shell.cp('-R', path.join(plugin_dir, '*') , dest);
-            }
+        // Use original plugin_src value instead.
 
-            var data = {
-                source: {
-                type: 'local',
-                      path: plugin_dir
-                }
-            };
-            metadata.save_fetch_metadata(dest, data);
-            return dest;
-        };
+        var p,  // The Q promise to be returned.
+            linkable = true,
+            plugin_dir = path.join(plugin_src, options.subdir);
 
-        if(!fs.existsSync(plugin_dir)) {
-            return registry.fetch([plugin_dir], options.client)
-            .then(function(dir) {
+        if (fs.existsSync(plugin_dir)) {
+            p = Q(plugin_dir);
+        } else {
+            // If there is no such local path, it's a plugin id.
+            // First look for it in the local search path (if provided).
+            var local_dir = findLocalPlugin(plugin_src, options.searchpath);
+            if(local_dir) {
+                p = Q(local_dir);
+            } else {
+                // If not found in local search path, fetch from the registry.
                 linkable = false;
-                return movePlugin(dir);
+                p = registry.fetch([plugin_src], options.client);
+            }
+        }
+
+        return p
+        .then(function(dir) {
+                return copyPlugin(dir, plugins_dir, options.link && linkable);
             })
-            .then(function(dir) {
-                return checkID(options, dir);
+        .then(function(dir) {
+                return checkID(options.expected_id, dir);
             });
-        } else {
-            return Q(movePlugin(plugin_dir))
-            .then(function(dir) {
-                return checkID(options, dir);
-            });
-        }
     }
 };
 
+
+function readId(dir) {
+    var xml_path = path.join(dir, 'plugin.xml');
+    require('../plugman').emit('verbose', 'Fetch is reading plugin.xml from location "' +
xml_path + '"...');
+    var et = xml_helpers.parseElementtreeSync(path.join(dir, 'plugin.xml'));
+    var plugin_id = et.getroot().attrib.id;
+    return plugin_id;
+}
+
 // Helper function for checking expected plugin IDs against reality.
-function checkID(options, dir) {
-    // Read the plugin.xml file and check the ID matches options.expected_id if set.
-    if (options.expected_id) {
-        var et = xml_helpers.parseElementtreeSync(path.join(dir, 'plugin.xml'));
-        if (et.getroot().attrib.id == options.expected_id) {
-            return dir;
-        } else {
-            return Q.reject(new Error('Expected Fetched plugin to have ID "' + options.expected_id
+ '" but got "' + et.getroot().attrib.id + '".'));
+function checkID(expected_id, dir) {
+    if ( expected_id ) {
+        var id = readId(dir);
+        if (expected_id != id) {
+            throw new Error('Expected fetched plugin to have ID "' + expected_id + '" but
got "' + id + '".');
         }
     }
     return dir;
 }
+
+// Look for plugin in local search path.
+function findLocalPlugin(plugin_id, searchpath) {
+    for(var i = 0; i < searchpath.length; i++){
+        var files = fs.readdirSync(searchpath[i]);
+        for (var j = 0; j < files.length; j++){
+            var plugin_path = path.join(searchpath[i], files[j]);
+            try {
+                var id = readId(plugin_path);
+                if (plugin_id === id) {
+                    return plugin_path;
+                }
+            }
+            catch(err) {
+                require('../plugman').emit('verbose', 'Error while trying to read plugin.xml
from ' + plugin_path);
+            }
+        }
+    }
+    return null;
+}
+
+
+// Copy or link a plugin from plugin_dir to plugins_dir/plugin_id.
+function copyPlugin(plugin_dir, plugins_dir, link) {
+    var plugin_id = readId(plugin_dir);
+    var dest = path.join(plugins_dir, plugin_id);
+    shell.rm('-rf', dest);
+    if (link) {
+        require('../plugman').emit('verbose', 'Symlinking from location "' + plugin_dir +
'" to location "' + dest + '"');
+        fs.symlinkSync(plugin_dir, dest, 'dir');
+    } else {
+        shell.mkdir('-p', dest);
+        require('../plugman').emit('verbose', 'Copying from location "' + plugin_dir + '"
to location "' + dest + '"');
+        shell.cp('-R', path.join(plugin_dir, '*') , dest);
+    }
+
+    var data = {
+        source: {
+        type: 'local',
+              path: plugin_dir
+        }
+    };
+    metadata.save_fetch_metadata(dest, data);
+    return dest;
+}

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/c8e1cb99/src/install.js
----------------------------------------------------------------------
diff --git a/src/install.js b/src/install.js
index 37b78e1..28c030f 100644
--- a/src/install.js
+++ b/src/install.js
@@ -50,7 +50,7 @@ function possiblyFetch(actions, platform, project_dir, id, plugins_dir,
options)
     // Check that the plugin has already been fetched.
     if (!fs.existsSync(plugin_dir)) {
         // if plugin doesnt exist, use fetch to get it.
-        return require('../plugman').raw.fetch(id, plugins_dir, { link: false, subdir: options.subdir,
git_ref: options.git_ref, client: 'plugman', expected_id: options.expected_id })
+        return require('../plugman').raw.fetch(id, plugins_dir, { link: false, subdir: options.subdir,
git_ref: options.git_ref, client: 'plugman', expected_id: options.expected_id, searchpath:
options.searchpath })
         .then(function(plugin_dir) {
             return runInstall(actions, platform, project_dir, plugin_dir, plugins_dir, options);
         });


Mime
View raw message