You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2013/03/04 20:32:50 UTC

[83/91] [abbrv] git commit: small refactor and added tests

small refactor and added tests


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

Branch: refs/heads/master
Commit: 7061e481d854955d2323c78ca0754fe11d4afa0d
Parents: 9459086
Author: Anis Kadri <an...@gmail.com>
Authored: Wed Feb 13 18:02:37 2013 -0800
Committer: Anis Kadri <an...@gmail.com>
Committed: Wed Feb 13 18:02:37 2013 -0800

----------------------------------------------------------------------
 config/remote.js    |    5 +++++
 plugman.js          |   10 +++++++---
 test/remote-test.js |   26 ++++++++++++++++++++++++++
 util/plugins.js     |   40 +++++++++++++++++++++++++++-------------
 4 files changed, 65 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/7061e481/config/remote.js
----------------------------------------------------------------------
diff --git a/config/remote.js b/config/remote.js
new file mode 100644
index 0000000..c4b9485
--- /dev/null
+++ b/config/remote.js
@@ -0,0 +1,5 @@
+module.exports = {
+ url: 'http://plugins.cordova.io',
+ query_path: '/cordova_plugins/_design/cordova_plugins/_view/by_name?key=\"%s\"',
+ list_path: '/cordova_plugins/_design/cordova_plugins/_view/by_name'
+}

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/7061e481/plugman.js
----------------------------------------------------------------------
diff --git a/plugman.js b/plugman.js
index 980f988..02e4879 100755
--- a/plugman.js
+++ b/plugman.js
@@ -39,7 +39,11 @@ if (cli_opts.v) {
     console.log(package.name + ' version ' + package.version);
 }
 else if (cli_opts.list) {
-    plugins.listAllPlugins();
+    plugins.listAllPlugins(function(plugins) {
+      for(var i = 0, j = plugins.length ; i < j ; i++) {
+        console.log(plugins[i].value.name, '-', plugins[i].value.description);
+      }
+    });
 }
 else if (!cli_opts.platform || !cli_opts.project || !cli_opts.plugin) {
     printUsage();
@@ -83,8 +87,8 @@ function handlePlugin(action, platform, project_dir, plugin_dir) {
         // try querying the plugin database
         async = true;
         plugins.getPluginInfo(plugin_dir,
-            function(plugin_dir) {
-                execAction(action, platform, project_dir, plugin_dir);
+            function(plugin_info) {
+                execAction(action, platform, project_dir, plugins.clonePluginGitRepo(plugin_info.url));
             },
             function(e) {
                 throw new Error(action + ' failed. "' + plugin_xml_path + '" not found');

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/7061e481/test/remote-test.js
----------------------------------------------------------------------
diff --git a/test/remote-test.js b/test/remote-test.js
new file mode 100644
index 0000000..27b8aa5
--- /dev/null
+++ b/test/remote-test.js
@@ -0,0 +1,26 @@
+var path = require('path'),
+    plugins = require(path.join(__dirname, '..', 'util', 'plugins'));
+
+exports['should get plugin information from a remote source'] = function(test) {
+    plugins.getPluginInfo('ChildBrowser', function(plugin) {
+        test.equal('https://github.com/imhotep/ChildBrowser', plugin.url);
+        test.equal('ChildBrowser', plugin.name);
+        test.equal('ChildBrowser plugin', plugin.description);
+        test.done();
+    }, function() { test.ok(false); });
+}
+
+exports['should list all plugins from a remote source'] = function(test) {
+    plugins.listAllPlugins('ChildBrowser', function(plugins) {
+        test.ok(plugins != undefined);
+        test.ok(plugins.length > 0);
+        test.done();
+    }, function() { test.ok(false); });
+}
+
+exports['should clone plugin git repository'] = function(test) {
+    plugins.getPluginInfo('ChildBrowser', function(plugin) {
+        test.ok(plugins.clonePluginGitRepo(plugin.url) != undefined);
+        test.done();
+    }, function() { test.ok(false); });
+}

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/7061e481/util/plugins.js
----------------------------------------------------------------------
diff --git a/util/plugins.js b/util/plugins.js
index 62548fd..51ef9f1 100644
--- a/util/plugins.js
+++ b/util/plugins.js
@@ -1,15 +1,15 @@
 #!/usr/bin/env node
-// FIXME: change this quickly
-var url = "http://plugins.cordova.io";
 var http = require('http'),
     osenv = require('osenv'),
     path = require('path'),
     fs = require('fs'),
-    shell = require('shelljs');
+    util = require('util'),
+    shell = require('shelljs'),
+    remote = require(path.join(__dirname, '..', 'config', 'remote'));
 
 // Fetches plugin information from remote server
 exports.getPluginInfo = function(plugin_name, success, error) {
-    http.get(url + "/cordova_plugins/_design/cordova_plugins/_view/by_name?key=\""+plugin_name+"\"", function(res) {
+    http.get(remote.url + util.format(remote.query_path, plugin_name), function(res) {
       var str = '';
       res.on('data', function (chunk) {
         str += chunk;
@@ -18,7 +18,7 @@ exports.getPluginInfo = function(plugin_name, success, error) {
           var response, plugin_info;
           if((response = JSON.parse(str)).rows.length == 1) {
             plugin_info = response.rows[0].value;
-            success(exports.clonePluginGitRepo(plugin_info.url));
+            success(plugin_info);
           } else {
             error("Could not find information on "+plugin_dir+" plugin");
           }
@@ -31,16 +31,14 @@ exports.getPluginInfo = function(plugin_name, success, error) {
 }
 
 exports.listAllPlugins = function(plugin_name, success, error) {
-    http.get(url + "/cordova_plugins/_design/cordova_plugins/_view/by_name", function(res) {
+    http.get(remote.url + remote.list_path, function(res) {
       var str = '';
       res.on('data', function (chunk) {
         str += chunk;
       });
       res.on('end', function () {
-          var plugins = (JSON.parse(str)).rows, i, j;
-          for(i = 0, j = plugins.length ; i < j ; i++) {
-            console.log(plugins[i].value.name, '-', plugins[i].value.description);
-          }
+          var plugins = (JSON.parse(str)).rows;
+          success(plugins);
       });
       
     }).on('error', function(e) {
@@ -49,16 +47,20 @@ exports.listAllPlugins = function(plugin_name, success, error) {
     });
 }
 
-exports.clonePluginGitRepo = function(plugin_dir) {
+exports.clonePluginGitRepo = function(plugin_git_url) {
     if(!shell.which('git')) {
         throw new Error('git command line is not installed');
     }
     // use osenv to get a temp directory in a portable way
-    plugin_git_url = plugin_dir; 
     plugin_dir = path.join(osenv.tmpdir(), 'plugin');
+    
+    // trash it if it already exists (something went wrong before probably)
+    if(fs.existsSync(plugin_dir)) {
+        shell.rm('-rf', plugin_dir);
+    }
 
     if(shell.exec('git clone ' + plugin_git_url + ' ' + plugin_dir + ' 2>&1 1>/dev/null', {silent: true}).code != 0) {
-        throw new Error('failed to get the plugin via git URL', plugin_git_url);
+        throw new Error('failed to get the plugin via git URL '+ plugin_git_url);
     }
     
     process.on('exit', function() {
@@ -71,3 +73,15 @@ exports.clonePluginGitRepo = function(plugin_dir) {
 
     return plugin_dir;
 }
+
+// TODO add method for archives and other formats
+// exports.extractArchive = function(plugin_dir) {
+// }
+
+// TODO add method to publish plugin from cli 
+// exports.publishPlugin = function(plugin_dir) {
+// }
+
+// TODO add method to unpublish plugin from cli 
+// exports.unpublishPlugin = function(plugin_dir) {
+// }