You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2014/01/07 17:38:33 UTC
git commit: CB-4976 Don't fire download hooks for local paths in
lazy_load
Updated Branches:
refs/heads/master 4b9269abb -> 190e72c2e
CB-4976 Don't fire download hooks for local paths in lazy_load
Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/190e72c2
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/190e72c2
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/190e72c2
Branch: refs/heads/master
Commit: 190e72c2eaeaea74062af0b7027b032c79ca8624
Parents: 4b9269a
Author: Andrew Grieve <ag...@chromium.org>
Authored: Tue Jan 7 11:37:32 2014 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue Jan 7 11:38:13 2014 -0500
----------------------------------------------------------------------
spec/lazy_load.spec.js | 24 +++------
src/lazy_load.js | 128 +++++++++++++++++++-------------------------
2 files changed, 62 insertions(+), 90 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/190e72c2/spec/lazy_load.spec.js
----------------------------------------------------------------------
diff --git a/spec/lazy_load.spec.js b/spec/lazy_load.spec.js
index b4d97f3..c144e6d 100644
--- a/spec/lazy_load.spec.js
+++ b/spec/lazy_load.spec.js
@@ -55,12 +55,11 @@ describe('lazy_load module', function() {
});
describe('custom method (loads custom cordova libs)', function() {
- var mkdir, exists, fire, rm, sym;
+ var exists, fire, rm;
beforeEach(function() {
- mkdir = spyOn(shell, 'mkdir');
+ spyOn(shell, 'mkdir');
rm = spyOn(shell, 'rm');
mv = spyOn(shell, 'mv');
- sym = spyOn(fs, 'symlinkSync');
exists = spyOn(fs, 'existsSync').andReturn(false);
readdir = spyOn(fs, 'readdirSync').andReturn(['somefile.txt']);
fire = spyOn(hooker, 'fire').andReturn(Q());
@@ -76,15 +75,8 @@ describe('lazy_load module', function() {
});
it('should callback with no errors and fire event hooks even if library already exists if the lib url is a local dir', function(done) {
exists.andReturn(true);
- lazy_load.custom('some local dir', 'some id', 'platform X', 'three point five').then(function() {
- expect(fire).toHaveBeenCalled()
- }, function(err) {
- expect(err).not.toBeDefined();
- }).fin(done);
- });
- it('should fire a before_library_download event before it starts downloading a library', function(done) {
- lazy_load.custom('some url', 'some id', 'platform X', 'three point five').then(function() {
- expect(fire).toHaveBeenCalledWith('before_library_download', {platform:'platform X', url:'some url', id:'some id', version:'three point five'});
+ lazy_load.custom('some local dir', 'some id', 'platform X', 'three point six').then(function() {
+ expect(fire).not.toHaveBeenCalled()
}, function(err) {
expect(err).not.toBeDefined();
}).fin(done);
@@ -156,15 +148,15 @@ describe('lazy_load module', function() {
describe('local paths for libraries', function() {
it('should return the local path, no symlink', function(done) {
- lazy_load.custom('/some/random/lib', 'id', 'X', 'three point five').then(function(dir) {
+ lazy_load.custom('/some/random/lib', 'id', 'X', 'three point eight').then(function(dir) {
expect(dir).toEqual('/some/random/lib');
}, function(err) {
expect(err).toBeUndefined();
}).fin(done);
});
- it('should fire after hook once done', function(done) {
- lazy_load.custom('/some/random/lib', 'id', 'X', 'three point five').then(function() {
- expect(fire).toHaveBeenCalledWith('after_library_download', {platform:'X',url:'/some/random/lib',id:'id',version:'three point five',path:'/some/random/lib', symlink:false});
+ it('should not file download hook', function(done) {
+ lazy_load.custom('/some/random/lib', 'id', 'X', 'three point nine').then(function() {
+ expect(fire).not.toHaveBeenCalledWith('after_library_download', {platform:'X',url:'/some/random/lib',id:'id',version:'three point nine',path:'/some/random/lib', symlink:false});
}, function(err) {
expect(err).toBeUndefined();
}).fin(done);
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/190e72c2/src/lazy_load.js
----------------------------------------------------------------------
diff --git a/src/lazy_load.js b/src/lazy_load.js
index cb0ca41..825a512 100644
--- a/src/lazy_load.js
+++ b/src/lazy_load.js
@@ -49,21 +49,19 @@ module.exports = {
var lib_dir = platforms[platform] && platforms[platform].subdirectory && platform !== "blackberry10" ? path.join(download_dir, platforms[platform].subdirectory) : download_dir;
- // For remote resources and archives this will download and/or unpack and cache them in the .cordova dir.
- // In the event that the custom url is a local dir the cache is not used and we must not return here. The later logic will set things appropriately.
+ // Return early for already-cached remote URL, or for local URLs.
var uri = URL.parse(url);
if (uri.protocol && uri.protocol[1] != ':') { // second part of conditional is for awesome windows support. fuuu windows
if (fs.existsSync(download_dir)) {
events.emit('verbose', id + ' library for "' + platform + '" already exists. No need to download. Continuing.');
return Q(lib_dir);
}
+ } else {
+ // Local path.
+ download_dir = uri.path;
+ lib_dir = platforms[platform] && platforms[platform].subdirectory ? path.join(download_dir, platforms[platform].subdirectory) : download_dir;
+ return Q(lib_dir);
}
- try {
- // readlinkSync throws if download_dir points to a non-existent file.
- var dangling = fs.readlinkSync(download_dir);
- return Q.reject(download_dir + " points to non-existent or unreadable location: " + dangling);
- } catch (e) {}
- events.emit('log', 'Installing ' + id + ' library for ' + platform + '...');
return hooker.fire('before_library_download', {
platform:platform,
url:url,
@@ -72,77 +70,59 @@ module.exports = {
}).then(function() {
var uri = URL.parse(url);
var d = Q.defer();
- if (uri.protocol && uri.protocol[1] != ':') { // second part of conditional is for awesome windows support. fuuu windows
- npm.load(function() {
- // Check if NPM proxy settings are set. If so, include them in the request() call.
- var proxy;
- if (uri.protocol == 'https:') {
- proxy = npm.config.get('https-proxy');
- } else if (uri.protocol == 'http:') {
- proxy = npm.config.get('proxy');
- }
-
- shell.mkdir('-p', download_dir);
- var size = 0;
- var request_options = {uri:url};
- if (proxy) {
- request_options.proxy = proxy;
- }
- events.emit('verbose', 'Requesting ' + JSON.stringify(request_options) + '...');
- events.emit('log', 'Downloading ' + id + ' library for ' + platform + '...');
- var req = request.get(request_options, function(err, res, body) {
- if (err) {
- shell.rm('-rf', download_dir);
- d.reject(err);
- } else if (res.statusCode != 200) {
- shell.rm('-rf', download_dir);
- d.reject(new Error('HTTP error ' + res.statusCode + ' retrieving version ' + version + ' of ' + id + ' for ' + platform));
- } else {
- size = body.length;
- }
- });
+ npm.load(function() {
+ // Check if NPM proxy settings are set. If so, include them in the request() call.
+ var proxy;
+ if (uri.protocol == 'https:') {
+ proxy = npm.config.get('https-proxy');
+ } else if (uri.protocol == 'http:') {
+ proxy = npm.config.get('proxy');
+ }
- req.pipe(zlib.createUnzip())
- .pipe(tar.Extract({path:download_dir}))
- .on('error', function(err) {
+ shell.mkdir('-p', download_dir);
+ var size = 0;
+ var request_options = {uri:url};
+ if (proxy) {
+ request_options.proxy = proxy;
+ }
+ events.emit('verbose', 'Requesting ' + JSON.stringify(request_options) + '...');
+ events.emit('log', 'Downloading ' + id + ' library for ' + platform + '...');
+ var req = request.get(request_options, function(err, res, body) {
+ if (err) {
shell.rm('-rf', download_dir);
d.reject(err);
- })
- .on('end', function() {
- events.emit('verbose', 'Downloaded, unzipped and extracted ' + size + ' byte response.');
- events.emit('log', 'Download complete');
- var entries = fs.readdirSync(download_dir);
- var entry = path.join(download_dir, entries[0]);
- shell.mv('-f', path.join(entry, (platform=='blackberry10'?'blackberry10':''), '*'), download_dir);
- shell.rm('-rf', entry);
- events.emit('log', 'Installing ' + id + ' library for ' + platform + '...');
- d.resolve(hooker.fire('after_library_download', {
- platform:platform,
- url:url,
- id:id,
- version:version,
- path: lib_dir,
- size:size,
- symlink:false
- }));
- });
+ } else if (res.statusCode != 200) {
+ shell.rm('-rf', download_dir);
+ d.reject(new Error('HTTP error ' + res.statusCode + ' retrieving version ' + version + ' of ' + id + ' for ' + platform));
+ } else {
+ size = body.length;
+ }
});
- } else {
- // Local path.
- // Do nothing here; users of this code should be using the returned path.
- download_dir = uri.protocol && uri.protocol[1] == ':' ? uri.href : uri.path;
- lib_dir = platforms[platform] && platforms[platform].subdirectory ? path.join(download_dir, platforms[platform].subdirectory) : download_dir;
- events.emit('log', 'Installing ' + id + ' library for ' + platform + '...');
- d.resolve(hooker.fire('after_library_download', {
- platform:platform,
- url:url,
- id:id,
- version:version,
- path: lib_dir,
- symlink:false
- }));
- }
+ req.pipe(zlib.createUnzip())
+ .pipe(tar.Extract({path:download_dir}))
+ .on('error', function(err) {
+ shell.rm('-rf', download_dir);
+ d.reject(err);
+ })
+ .on('end', function() {
+ events.emit('verbose', 'Downloaded, unzipped and extracted ' + size + ' byte response.');
+ events.emit('log', 'Download complete');
+ var entries = fs.readdirSync(download_dir);
+ var entry = path.join(download_dir, entries[0]);
+ shell.mv('-f', path.join(entry, (platform=='blackberry10'?'blackberry10':''), '*'), download_dir);
+ shell.rm('-rf', entry);
+ d.resolve(hooker.fire('after_library_download', {
+ platform:platform,
+ url:url,
+ id:id,
+ version:version,
+ path: lib_dir,
+ size:size,
+ symlink:false
+ }));
+ });
+ });
return d.promise.then(function () { return lib_dir; });
});
},