cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject cordova-lib git commit: CB-8285 Don't create .fetch.json files within plugin directories
Date Mon, 12 Jan 2015 19:06:54 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master 5094c8095 -> c49eaa86c


CB-8285 Don't create .fetch.json files within plugin directories

Also cleans up old .fetch.json when found.

This info is now stored in a single `fetch.json` file within `plugins_dir`


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

Branch: refs/heads/master
Commit: c49eaa86c92ba6a799ac8b91bfa092dbf3f5494d
Parents: 5094c80
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Mon Jan 12 14:05:46 2015 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Mon Jan 12 14:05:46 2015 -0500

----------------------------------------------------------------------
 cordova-lib/spec-plugman/fetch.spec.js   |  12 +--
 cordova-lib/src/plugman/fetch.js         | 137 ++++++++++++--------------
 cordova-lib/src/plugman/util/metadata.js |  45 ++++++---
 3 files changed, 104 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/c49eaa86/cordova-lib/spec-plugman/fetch.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-plugman/fetch.spec.js b/cordova-lib/spec-plugman/fetch.spec.js
index 78a7c54..cd11f84 100644
--- a/cordova-lib/spec-plugman/fetch.spec.js
+++ b/cordova-lib/spec-plugman/fetch.spec.js
@@ -128,7 +128,7 @@ describe('fetch', function() {
             runs(function() {
                 expect(done).toBe(true);
                 expect(clone).toHaveBeenCalledWith(url, temp, '.', undefined);
-                expect(save_metadata).toHaveBeenCalledWith(test_plugin, jasmine.any(Object));
+                expect(save_metadata).toHaveBeenCalled();
             });
         });
         it('should call clonePluginGitRepo with subdir if applicable', function() {
@@ -140,7 +140,7 @@ describe('fetch', function() {
             waitsFor(function() { return done; }, 'fetch promise never resolved', 250);
             runs(function() {
                 expect(clone).toHaveBeenCalledWith(url, temp, dir, undefined);
-                expect(save_metadata).toHaveBeenCalledWith(test_plugin, jasmine.any(Object));
+                expect(save_metadata).toHaveBeenCalled();
             });
         });
         it('should call clonePluginGitRepo with subdir and git ref if applicable', function()
{
@@ -153,7 +153,7 @@ describe('fetch', function() {
             waitsFor(function() { return done; }, 'fetch promise never resolved', 250);
             runs(function() {
                 expect(clone).toHaveBeenCalledWith(url, temp, dir, ref);
-                expect(save_metadata).toHaveBeenCalledWith(test_plugin, jasmine.any(Object));
+                expect(save_metadata).toHaveBeenCalled();
             });
         });
         it('should extract the git ref from the URL hash, if provided', function() {
@@ -165,7 +165,7 @@ describe('fetch', function() {
             waitsFor(function() { return done; }, 'fetch promise never resolved', 250);
             runs(function() {
                 expect(clone).toHaveBeenCalledWith(baseURL, temp, '.', 'fakeGitRef');
-                expect(save_metadata).toHaveBeenCalledWith(test_plugin, jasmine.any(Object));
+                expect(save_metadata).toHaveBeenCalled();
             });
         });
         it('should extract the subdir from the URL hash, if provided', function() {
@@ -177,7 +177,7 @@ describe('fetch', function() {
             waitsFor(function() { return done; }, 'fetch promise never resolved', 250);
             runs(function() {
                 expect(clone).toHaveBeenCalledWith(baseURL, temp, 'fakeSubDir', undefined);
-                expect(save_metadata).toHaveBeenCalledWith(test_plugin, jasmine.any(Object));
+                expect(save_metadata).toHaveBeenCalled();
             });
         });
         it('should extract the git ref and subdir from the URL hash, if provided', function()
{
@@ -189,7 +189,7 @@ describe('fetch', function() {
             waitsFor(function() { return done; }, 'fetch promise never resolved', 250);
             runs(function() {
                 expect(clone).toHaveBeenCalledWith(baseURL, temp, 'fake/Sub/Dir', 'fakeGitRef');
-                expect(save_metadata).toHaveBeenCalledWith(test_plugin, jasmine.any(Object));
+                expect(save_metadata).toHaveBeenCalled();
             });
         });
         it('should fail when the expected ID doesn\'t match', function(done) {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/c49eaa86/cordova-lib/src/plugman/fetch.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/plugman/fetch.js b/cordova-lib/src/plugman/fetch.js
index edc0d43..f800b2d 100644
--- a/cordova-lib/src/plugman/fetch.js
+++ b/cordova-lib/src/plugman/fetch.js
@@ -43,7 +43,6 @@ var localPlugins = null;
 // Returns a promise.
 module.exports = fetchPlugin;
 function fetchPlugin(plugin_src, plugins_dir, options) {
-     var data = {};//data to be saved to .fetch.json
     // Ensure the containing directory exists.
     shell.mkdir('-p', plugins_dir);
 
@@ -73,97 +72,89 @@ function fetchPlugin(plugin_src, plugins_dir, options) {
         }
     }
 
-    // If it looks like a network URL, git clone it.
-    if ( uri.protocol && uri.protocol != 'file:' && uri.protocol[1] != ':'
&& !plugin_src.match(/^\w+:\\/)) {
-        events.emit('log', 'Fetching plugin "' + plugin_src + '" via git clone');
-        if (options.link) {
-            events.emit('log', '--link is not supported for git URLs and will be ignored');
-        }
-
-            data = {
-                source: {
-                    type: 'git',
-                    url:  plugin_src,
-                    subdir: options.subdir,
-                    ref: options.git_ref
-                }
-            };
+    return Q.when().then(function() {
+        // If it looks like a network URL, git clone it.
+        if ( uri.protocol && uri.protocol != 'file:' && uri.protocol[1] !=
':' && !plugin_src.match(/^\w+:\\/)) {
+            events.emit('log', 'Fetching plugin "' + plugin_src + '" via git clone');
+            if (options.link) {
+                events.emit('log', '--link is not supported for git URLs and will be ignored');
+            }
 
             return plugins.clonePluginGit(plugin_src, plugins_dir, options)
             .then(function(dir) {
-                checkID(options.expected_id, dir);
-                return dir;
-            })
-            .then(function(dir) {
-                metadata.save_fetch_metadata(dir, data);
-                return dir;
+                return {
+                    pinfo: new PluginInfo.PluginInfo(dir),
+                    fetchJsonSource: {
+                        type: 'git',
+                        url:  plugin_src,
+                        subdir: options.subdir,
+                        ref: options.git_ref
+                    }
+                };
             });
-    } else {
+        }
         // If it's not a network URL, it's either a local path or a plugin ID.
-       
-        var p,  // The Q promise to be returned.
-            linkable = true,
-            plugin_dir = cordovaUtil.fixRelativePath(path.join(plugin_src, options.subdir));
-
-        if (fs.existsSync(plugin_dir)) {
-            p = Q(plugin_dir);
-            data = {
-                  source: {
-                    type: 'local',
-                    path: plugin_dir
+        var plugin_dir = cordovaUtil.fixRelativePath(path.join(plugin_src, options.subdir));
+
+        return Q.when().then(function() {
+            if (fs.existsSync(plugin_dir)) {
+                return {
+                    pinfo: new PluginInfo.PluginInfo(plugin_dir),
+                    fetchJsonSource: {
+                        type: 'local',
+                        path: plugin_dir
                     }
                 };
-        } else {
+            }
             // If there is no such local path, it's a plugin id or id@versionspec.
             // First look for it in the local search path (if provided).
             var pinfo = findLocalPlugin(plugin_src, options.searchpath);
             if (pinfo) {
-                p = Q(pinfo.dir);
-                data = {
-                  source: {
-                    type: 'local',
-                    path: pinfo.dir
+                events.emit('verbose', 'Found ' + plugin_src + ' at ' + pinfo.dir);
+                return {
+                    pinfo: pinfo,
+                    fetchJsonSource: {
+                        type: 'local',
+                        path: pinfo.dir
                     }
                 };
-                events.emit('verbose', 'Found ' + plugin_src + ' at ' + pinfo.dir);
             } else if ( options.noregistry ) {
-                p = Q.reject(new CordovaError(
-                        'Plugin ' + plugin_src + ' not found locally. ' +
-                        'Note, plugin registry was disabled by --noregistry flag.'
-                    ));
-            } else {
-                // If not found in local search path, fetch from the registry.
-                linkable = false;
-                events.emit('log', 'Fetching plugin "' + plugin_src + '" via plugin registry');
-                p = registry.fetch([plugin_src], options.client);
-                data = {
-                  source: {
-                    type: 'registry',
-                    id: plugin_src
+                return Q.reject(new CordovaError(
+                    'Plugin ' + plugin_src + ' not found locally. ' +
+                    'Note, plugin registry was disabled by --noregistry flag.'
+                ));
+            }
+            // If not found in local search path, fetch from the registry.
+            events.emit('log', 'Fetching plugin "' + plugin_src + '" via plugin registry');
+            return registry.fetch([plugin_src], options.client)
+            .then(function(dir) {
+                return {
+                    pinfo: new PluginInfo.PluginInfo(dir),
+                    fetchJsonSource: {
+                        type: 'registry',
+                        id: plugin_src
                     }
                 };
-            }
-        }
-
-        return p
-        .then(function(dir) {
-            options.plugin_src_dir = dir;
-            return copyPlugin(dir, plugins_dir, options.link && linkable);
-        }).then(function(dest){
-             metadata.save_fetch_metadata(dest, data);
-             return dest;
-        })
-        .then(function(dir) {
-            checkID(options.expected_id, dir);
-            return dir;
+            });
+        }).then(function(result) {
+            options.plugin_src_dir = result.pinfo.dir;
+            return Q.when(copyPlugin(result.pinfo, plugins_dir, options.link && result.fetchJsonSource.type
== 'local'))
+            .then(function(dir) {
+                result.pinfo.dir = dir;
+                return result;
+            });
         });
-    }
+    }).then(function(result){
+        checkID(options.expected_id, result.pinfo);
+        metadata.save_fetch_metadata(plugins_dir, result.pinfo.id, { source: result.fetchJsonSource
});
+        return result.pinfo.dir;
+    });
 }
 
+
 // Helper function for checking expected plugin IDs against reality.
-function checkID(expected_id, dir) {
+function checkID(expected_id, pinfo) {
     if (!expected_id) return;
-    var pinfo = new PluginInfo.PluginInfo(dir);
     var id = pinfo.id;
     // if id with specific version provided, append version to id
     if (expected_id.split('@').length > 1) {
@@ -246,8 +237,8 @@ function findLocalPlugin(plugin_src, searchpath) {
 
 
 // Copy or link a plugin from plugin_dir to plugins_dir/plugin_id.
-function copyPlugin(plugin_dir, plugins_dir, link) {
-    var pinfo = new PluginInfo.PluginInfo(plugin_dir);
+function copyPlugin(pinfo, plugins_dir, link) {
+    var plugin_dir = pinfo.dir;
     var dest = path.join(plugins_dir, pinfo.id);
     shell.rm('-rf', dest);
     if (link) {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/c49eaa86/cordova-lib/src/plugman/util/metadata.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/plugman/util/metadata.js b/cordova-lib/src/plugman/util/metadata.js
index bf88f41..ba70636 100644
--- a/cordova-lib/src/plugman/util/metadata.js
+++ b/cordova-lib/src/plugman/util/metadata.js
@@ -21,22 +21,45 @@
           indent:4, unused:vars, latedef:nofunc
 */
 
-var fs = require('fs'),
-    path = require('path');
+var fs = require('fs');
+var path = require('path');
 
-var filename = '.fetch.json';
+var cachedJson = null;
+
+function getJson(pluginsDir) {
+    if (!cachedJson) {
+        var fetchJsonPath = path.join(pluginsDir, 'fetch.json');
+        if (fs.existsSync(fetchJsonPath)) {
+            cachedJson = JSON.parse(fs.readFileSync(fetchJsonPath, 'utf-8'));
+        } else {
+            cachedJson = {};
+        }
+    }
+    return cachedJson;
+}
 
 exports.get_fetch_metadata = function(plugin_dir) {
-    var filepath = path.join(plugin_dir, filename);
-    if (fs.existsSync(filepath)) {
-        return JSON.parse(fs.readFileSync(filepath, 'utf-8'));
-    } else {
-        return {};
+    var pluginsDir = path.dirname(plugin_dir);
+    var pluginId = path.basename(plugin_dir);
+
+    var metadataJson = getJson(pluginsDir);
+    if (metadataJson[pluginId]) {
+        return metadataJson[pluginId];
+    }
+    var legacyPath = path.join(plugin_dir, '.fetch.json');
+    if (fs.existsSync(legacyPath)) {
+        var ret = JSON.parse(fs.readFileSync(legacyPath, 'utf-8'));
+        exports.save_fetch_metadata(pluginsDir, pluginId, ret);
+        fs.unlinkSync(legacyPath);
+        return ret;
     }
+    return {};
 };
 
-exports.save_fetch_metadata = function(plugin_dir, data) {
-    var filepath = path.join(plugin_dir, '.fetch.json');
-    fs.writeFileSync(filepath, JSON.stringify(data), 'utf-8');
+exports.save_fetch_metadata = function(pluginsDir, pluginId, data) {
+    var metadataJson = getJson(pluginsDir);
+    metadataJson[pluginId] = data;
+    var fetchJsonPath = path.join(pluginsDir, 'fetch.json');
+    fs.writeFileSync(fetchJsonPath, JSON.stringify(metadataJson, null, 4), 'utf-8');
 };
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message