cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject git commit: [CB-4793] Lazily require modules in some places.
Date Wed, 11 Sep 2013 18:59:26 GMT
Updated Branches:
  refs/heads/master cc30eaca3 -> aba4599f6


[CB-4793] Lazily require modules in some places.

With the related change to plugman:
cordova plugin ls: 700ms -> 80ms
cordova create: 800ms -> 450ms


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

Branch: refs/heads/master
Commit: aba4599f61449f3c506bfbd7e80f326029314dc4
Parents: cc30eac
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Wed Sep 11 14:56:31 2013 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Wed Sep 11 14:56:31 2013 -0400

----------------------------------------------------------------------
 bin/cordova   | 22 +++++++++++++++++++++-
 cordova.js    | 44 ++++++++++++++++++++------------------------
 platforms.js  | 18 +++++++++++++-----
 src/cli.js    | 10 +++++-----
 src/plugin.js | 21 +++++++++++----------
 src/util.js   | 16 +++++++++++-----
 6 files changed, 81 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/aba4599f/bin/cordova
----------------------------------------------------------------------
diff --git a/bin/cordova b/bin/cordova
index 4b0c4de..3a54c19 100755
--- a/bin/cordova
+++ b/bin/cordova
@@ -1,4 +1,24 @@
 #!/usr/bin/env node
-var CLI = require('../src/cli');
 
+// Set this to 1 to enable timestamp collection via addTs().
+if (0) {
+    var ts = [];
+    addTs = function(name) {
+        ts.push([name, new Date]);
+    }
+    process.on('exit', function() {
+        for (var i = 0; i < ts.length - 1; ++i) {
+          var e1 = ts[i];
+          var e2 = ts[i+1];
+          console.log(e1[0] + ' -> ' + e2[0] + ' = ' + (e2[1] - e1[1]));
+        }
+        console.log('total: ' + (ts[ts.length-1][1] - ts[0][1]));
+    });
+} else {
+    addTs = function() {};
+}
+
+addTs('start');
+var CLI = require('../src/cli');
 new CLI(process.argv);
+addTs('end');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/aba4599f/cordova.js
----------------------------------------------------------------------
diff --git a/cordova.js b/cordova.js
index 5e5eb87..277c12a 100644
--- a/cordova.js
+++ b/cordova.js
@@ -16,15 +16,8 @@
     specific language governing permissions and limitations
     under the License.
 */
-var cordova_events = require('./src/events'),
-    prepare        = require('./src/prepare'),
-    platform       = require('./src/platform'),
-    run            = require('./src/run'),
-    hooker         = require('./src/hooker'),
-    util           = require('./src/util'),
-    path           = require('path'),
-    fs             = require('fs'),
-    compile        = require('./src/compile');
+var cordova_events = require('./src/events');
+var addModuleProperty = require('./src/util').addModuleProperty;
 
 var off = function() {
     cordova_events.removeListener.apply(cordova_events, arguments);
@@ -34,20 +27,7 @@ var emit = function() {
     cordova_events.emit.apply(cordova_events, arguments);
 };
 
-module.exports = {
-    help:      require('./src/help'),
-    config:    require('./src/config'),
-    create:    require('./src/create'),
-    platform:  platform,
-    platforms: platform,
-    prepare:   prepare,
-    compile:   compile,
-    run:       run,
-    ripple:    require('./src/ripple'),
-    emulate:   require('./src/emulate'),
-    plugin:    require('./src/plugin'),
-    plugins:   require('./src/plugin'),
-    serve:     require('./src/serve'),
+exports = module.exports = {
     on:        function() {
         cordova_events.on.apply(cordova_events, arguments);
     },
@@ -58,5 +38,21 @@ module.exports = {
     },
     emit:      emit,
     trigger:   emit,
-    build:     require('./src/build')
 };
+
+addModuleProperty(module, 'prepare', './src/prepare');
+addModuleProperty(module, 'build', './src/build');
+addModuleProperty(module, 'help', './src/help');
+addModuleProperty(module, 'config', './src/config');
+addModuleProperty(module, 'create', './src/create');
+addModuleProperty(module, 'ripple', './src/ripple');
+addModuleProperty(module, 'emulate', './src/emulate');
+addModuleProperty(module, 'plugin', './src/plugin');
+addModuleProperty(module, 'plugins', './src/plugin');
+addModuleProperty(module, 'serve', './src/serve');
+addModuleProperty(module, 'platform', './src/platform');
+addModuleProperty(module, 'platforms', './src/platform');
+addModuleProperty(module, 'compile', './src/compile');
+addModuleProperty(module, 'run', './src/run');
+
+

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/aba4599f/platforms.js
----------------------------------------------------------------------
diff --git a/platforms.js b/platforms.js
index 74d72ec..40d0a6b 100644
--- a/platforms.js
+++ b/platforms.js
@@ -19,27 +19,27 @@
 
 module.exports = {
     'ios' : {
-        parser : require('./src/metadata/ios_parser'),
+        parser : './src/metadata/ios_parser',
         url    : 'https://git-wip-us.apache.org/repos/asf?p=cordova-ios.git',
         version: '3.0.0'
     }, 
     'android' : {
-        parser : require('./src/metadata/android_parser'),
+        parser : './src/metadata/android_parser',
         url    : 'https://git-wip-us.apache.org/repos/asf?p=cordova-android.git',
         version: '3.0.0'
     }, 
     'wp7' : {
-        parser : require('./src/metadata/wp7_parser'),
+        parser : './src/metadata/wp7_parser',
         url    : 'https://git-wip-us.apache.org/repos/asf?p=cordova-wp8.git',
         version: '3.0.0'
     },
     'wp8' : {
-        parser : require('./src/metadata/wp8_parser'),
+        parser : './src/metadata/wp8_parser',
         url    : 'https://git-wip-us.apache.org/repos/asf?p=cordova-wp8.git',
         version: '3.0.0'
     },
     blackberry10 : {
-        parser : require('./src/metadata/blackberry10_parser'),
+        parser : './src/metadata/blackberry10_parser',
         url    : 'https://git-wip-us.apache.org/repos/asf?p=cordova-blackberry.git',
         version: '3.0.0'
     },
@@ -48,3 +48,11 @@ module.exports = {
         version: '3.0.0'
     }
 };
+
+var addModuleProperty = require('./src/util').addModuleProperty;
+Object.keys(module.exports).forEach(function(key) {
+    var obj = module.exports[key];
+    if (obj.parser) {
+        addModuleProperty(module, 'parser', obj.parser, obj);
+    }
+});

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/aba4599f/src/cli.js
----------------------------------------------------------------------
diff --git a/src/cli.js b/src/cli.js
index 24c9d7d..0e7ea76 100755
--- a/src/cli.js
+++ b/src/cli.js
@@ -17,12 +17,10 @@
     under the License.
 */
 
-var optimist  = require('optimist'),
-    cordova   = require('../cordova'),
-    plugman   = require('plugman'),
-    platforms = require("../platforms");
-
 module.exports = function CLI(inputArgs) {
+    var optimist  = require('optimist'),
+        cordova   = require('../cordova');
+
     args = optimist(inputArgs)
         .boolean('d')
         .boolean('verbose')
@@ -56,6 +54,7 @@ module.exports = function CLI(inputArgs) {
     if (opts.verbose) {
         cordova.on('log', console.log);
         cordova.on('warn', console.warn);
+        var plugman = require('plugman');
         plugman.on('log', console.log);
         plugman.on('warn', console.warn);
         //Remove the corresponding token
@@ -77,6 +76,7 @@ module.exports = function CLI(inputArgs) {
     if (cordova.hasOwnProperty(cmd)) {
         if (cmd == 'emulate' || cmd == 'build' || cmd == 'prepare' || cmd == 'compile' ||
cmd == 'run') {
             // Filter all non-platforms into options
+            var platforms = require("../platforms");
             tokens.forEach(function(option, index) {
                 if (platforms.hasOwnProperty(option)) {
                     opts.platforms.push(option);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/aba4599f/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index 8ea1b3e..2c5cd33 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -16,18 +16,14 @@
     specific language governing permissions and limitations
     under the License.
 */
-var cordova_util  = require('./util'),
-    util          = require('util'),
-    fs            = require('fs'),
-    path          = require('path'),
-    shell         = require('shelljs'),
-    platforms     = require('../platforms'),
-    n             = require('ncallbacks'),
-    hooker        = require('./hooker'),
-    plugman       = require('plugman'),
-    events        = require('./events');
 
 module.exports = function plugin(command, targets, callback) {
+    var cordova_util  = require('./util'),
+        path          = require('path'),
+        n             = require('ncallbacks'),
+        hooker        = require('./hooker'),
+        events        = require('./events');
+
     var projectRoot = cordova_util.isCordova(process.cwd()),
         err;
 
@@ -115,6 +111,7 @@ module.exports = function plugin(command, targets, callback) {
 
                         // Fetch the plugin first.
                         events.emit('log', 'Calling plugman.fetch on plugin "' + target +
'"');
+                        var plugman = require('plugman');
                         plugman.fetch(target, pluginsDir, {}, function(err, dir) {
                             if (err) {
                                 err = new Error('Error fetching plugin: ' + err);
@@ -128,6 +125,7 @@ module.exports = function plugin(command, targets, callback) {
                                         return;
                                     }
 
+                                    var platforms = require('../platforms');
                                     var platform = platformList[platformIndex],
                                         platformRoot = path.join(projectRoot, 'platforms',
platform),
                                         parser = new platforms[platform].parser(platformRoot),
@@ -194,8 +192,10 @@ module.exports = function plugin(command, targets, callback) {
                             // If this is a web-only or dependency-only plugin, then
                             // there may be nothing to do here except remove the
                             // reference from the platform's plugin config JSON.
+                            var plugman = require('plugman');
                             platformList.forEach(function(platform) {
                                 var platformRoot = path.join(projectRoot, 'platforms', platform);
+                                var platforms = require('../platforms');
                                 var parser = new platforms[platform].parser(platformRoot);
                                 events.emit('log', 'Calling plugman.uninstall on plugin "'
+ target + '" for platform "' + platform + '"');
                                 plugman.uninstall.uninstallPlatform(platform, platformRoot,
target, path.join(projectRoot, 'plugins'), { www_dir: parser.staging_dir() });
@@ -217,6 +217,7 @@ module.exports = function plugin(command, targets, callback) {
                     if(callback) callback(err);
                     else throw err;
                 } else {
+                    var plugman = require('plugman');
                     plugman.search(opts.plugins, function(err, plugins) {
                         if(err) return console.log(err);
                         for(var plugin in plugins) {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/aba4599f/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index 858d20b..53eb8dd 100644
--- a/src/util.js
+++ b/src/util.js
@@ -18,8 +18,6 @@
 */
 var fs            = require('fs'),
     path          = require('path'),
-    config_parser = require('./config_parser'),
-    plugin_parser = require('./plugin_parser'),
     shell         = require('shelljs');
 
 // Global configuration paths
@@ -28,7 +26,7 @@ var global_config_path = path.join(HOME, '.cordova');
 var lib_path = path.join(global_config_path, 'lib');
 shell.mkdir('-p', lib_path);
 
-module.exports = {
+exports = module.exports = {
     globalConfig:global_config_path,
     libDirectory:lib_path,
     // Runs up the directory chain looking for a .cordova directory.
@@ -52,8 +50,6 @@ module.exports = {
             }
         } else return false;
     },
-    config_parser:config_parser,
-    plugin_parser:plugin_parser,
     // Recursively deletes .svn folders from a target path
     deleteSvnFolders:function(dir) {
         var contents = fs.readdirSync(dir);
@@ -137,3 +133,13 @@ module.exports = {
         return result;
     }
 };
+
+function addModuleProperty(module, symbol, modulePath, opt_obj) {
+    Object.defineProperty(opt_obj || module.exports, symbol, {
+        get : function() { return module.require(modulePath); }});
+}
+
+addModuleProperty(module, 'config_parser', './config_parser');
+addModuleProperty(module, 'plugin_parser', './plugin_parser');
+
+exports.addModuleProperty = addModuleProperty;


Mime
View raw message