cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [1/2] git commit: CB-6329 improve 'cordova info' command
Date Wed, 23 Apr 2014 16:35:51 GMT
Repository: cordova-cli
Updated Branches:
  refs/heads/master c7ac042fc -> cdb5deff9


CB-6329 improve 'cordova info' command

info.js file has been modified to work mainly asynchronous.
Added info-utils, which contains several useful functions required by
info.js
-It doesn't require a template to set an output.
-getNode, getCordova, doPlatforms works asynchronous, works with
callbacks and deals with errors.
-it writes a report (info.txt) in rootProject.

This changes are required for futures improvements.


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

Branch: refs/heads/master
Commit: 9372f0dd3ca9d9bc8982d0aaae7ee11ff1e58b9e
Parents: c7ac042
Author: Martin Gonzalez <martin.c.glez.glez@gmail.com>
Authored: Wed Mar 26 12:48:08 2014 -0600
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Wed Apr 23 12:34:36 2014 -0400

----------------------------------------------------------------------
 doc/info.txt      |  21 ------
 doc/platforms.txt |   7 --
 src/info-utils.js |  50 ++++++++++++++
 src/info.js       | 172 +++++++++++++++++++++----------------------------
 4 files changed, 123 insertions(+), 127 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9372f0dd/doc/info.txt
----------------------------------------------------------------------
diff --git a/doc/info.txt b/doc/info.txt
deleted file mode 100644
index ad3b665..0000000
--- a/doc/info.txt
+++ /dev/null
@@ -1,21 +0,0 @@
-Useful Information For Submitting Bug Reports and Getting Help
-
-Current Node Version
-
-    %Node
-
-Current Cordova CLI Version
-
-    %Cordova
-
-Current Config.xml
-
-    %Config
-
-Currently Installed Platforms
-
-    %Platforms
-
-Currently Installed Plugins
-
-    %Plugins

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9372f0dd/doc/platforms.txt
----------------------------------------------------------------------
diff --git a/doc/platforms.txt b/doc/platforms.txt
deleted file mode 100644
index 58689f7..0000000
--- a/doc/platforms.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-----------------------------------------
-
-    %Platform
-
-    %OtherGoodies
-
-----------------------------------------

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9372f0dd/src/info-utils.js
----------------------------------------------------------------------
diff --git a/src/info-utils.js b/src/info-utils.js
new file mode 100644
index 0000000..607e922
--- /dev/null
+++ b/src/info-utils.js
@@ -0,0 +1,50 @@
+    /**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+     */
+var superspawn    = require('./superspawn'),
+    Q             = require('q'),
+    _self;
+
+_self = {
+    getNodeInfo : function () {
+        return _self.execSpawn("node", "--version", "Node version: ", "Error retrieving Node
version: ");
+    },
+    getCordovaInfo : function () {
+        return _self.execSpawn("Cordova", "--version", "Cordova version: ", "Error retrieving
Cordova version: ");
+    },
+    getPlatformInfo : function (platform, projectRoot) {
+        switch (platform) {
+        case "ios":
+            return _self.execSpawn("xcodebuild", "-version", "iOS platform:\n\n", "Error
retrieving iOS platform information: ");
+            break;
+        case "android":
+            return _self.execSpawn("android list target", "", "Android platform:\n\n", "Error
retrieving Android platform information: ");
+            break;
+        }
+    },
+    // Execute using a child_process exec, for any async command
+    execSpawn : function (command, args, resultMsg, errorMsg) {
+        return Q.when(superspawn.spawn(command, args), function (result) {
+            return resultMsg + result;
+        }, function (error) {
+            return errorMsg + error;
+        });
+    }
+};
+
+module.exports = _self;

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9372f0dd/src/info.js
----------------------------------------------------------------------
diff --git a/src/info.js b/src/info.js
index 0668cd5..a61d88e 100644
--- a/src/info.js
+++ b/src/info.js
@@ -1,4 +1,4 @@
-/**
+    /**
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
     distributed with this work for additional information
@@ -15,124 +15,98 @@
     KIND, either express or implied.  See the License for the
     specific language governing permissions and limitations
     under the License.
-*/
-var cordova_util  = require('./util'),
-    shell         = require('shelljs'),
-    path          = require('path'),
-    fs            = require('fs'),
-    Q             = require('q'),
-    events        = require('./events');
-
-/*
+     */
+     
+    /*
     A utility funciton to help output the information needed
     when submitting a help request.
-
     Outputs to a file
-*/
-module.exports = function info() {
-
-    //Get the template
-    var projectRoot = cordova_util.cdProjectRoot();
+     */
+var cordova_util = require('./util'),
+    path         = require('path'),
+    fs           = require('fs'),
+    Q            = require('q'),
+    info_utils   = require('./info-utils');
 
-    var raw = fs.readFileSync(path.join(__dirname, '..', 'doc', 'info.txt'), 'utf-8').split("\n"),
-        output;
 
-    output = raw.map(function(line) {
-        if(line.match('    %') ) {
-            var type = (line.substr(5)).replace("\r",""),
-                out = "";
+module.exports = function info() {
+    //Get projectRoot
+    var projectRoot = cordova_util.cdProjectRoot(),output = "";
+    if (!projectRoot) {
+        return Q.reject(new Error('Current working directory is not a Cordova-based project.'));
+    }
 
-            switch(type) {
-            case "Node":
-                out = shell.exec('node --version',{silent:true}).output;
-                break;
-            case "Cordova":
-                out = require('../package').version;
-                break;
-            case "Config":
-                out = fs.readFileSync( cordova_util.projectConfig(projectRoot) );
-                break;
-            case "Platforms":
-                out = doPlatforms( projectRoot );
-                break;
-            case "Plugins":
-                out = doPlugins( projectRoot );
-                break;
-            default:
-                break;
+    //Array of functions, Q.allSettled
+    console.log("Collecting Data...\n\n");
+    return Q.allSettled([
+            //Get Node version
+            info_utils.getNodeInfo(),
+            //Get Cordova version
+            info_utils.getCordovaInfo(),
+            //Get project config.xml file using ano
+             getProjectConfig(projectRoot),
+            //Get list of plugins
+            listPlugins(projectRoot), 
+            //Get Platforms information
+            getPlatforms(projectRoot)
+        ]).then(function (promises) {
+        promises.forEach(function (p) {
+            output += (function () {
+                return p.state === "fulfilled" ? p.value + "\n\n" : p.state === "rejected"
? p.reason + "\n\n" : "Still working"
             }
-            return line.replace( "%"+type, out );
-        } else {
-            return line;
-        }
-    }).join("\n");
-
-    // /*
-    //     Write to File;
-    // */
-    events.emit('results', output);
-    fs.writeFileSync('info.txt', output );
-    return Q();
+                ());
+        });
+        print_SaveMsg(projectRoot, output);
+    });
 };
 
-function doPlatform( currentPlatform ) {
-    var output = "";
-    switch( currentPlatform ){
-    case "ios":
-        output = shell.exec('xcodebuild -version',{silent:true} ).output;
-        break;
-    case "android":
-        output = shell.exec('android list target',{silent:true} ).output;
-    }
-
-    return output;
+function print_SaveMsg(projectRoot, data) {
+    console.info(data);
+    fs.writeFile(path.join(projectRoot, "info.txt"), data, "utf-8", function (err) {
+        if (err)
+            throw err;
+    });
 }
 
-function doPlatforms( projectRoot ){
-    var platforms = cordova_util.listPlatforms(projectRoot);
-
-    if( platforms.length ) {
+function getPlatforms(projectRoot) {
+    var platforms = cordova_util.listPlatforms(projectRoot),
+        promises  = [];
 
-        var raw = fs.readFileSync(path.join(__dirname, '..', 'doc', 'platforms.txt')).toString('utf8').split("\n"),
-            output = "",
-            i;
+    if (platforms.length) {
+        platforms.forEach(function (platform) {
+            var deferred = Q.defer();
+            deferred.resolve(info_utils.getPlatformInfo(platform, projectRoot));
+            promises.push(deferred.promise);
+        });
+    } else {
+        var deferred = Q.defer()
+            deferred.reject("No Platforms Currently Installed");
+        promises.push(deferred.promise);
+    }
 
-        for(i=0; i<platforms.length; i++){
-            output += raw.map(function(line) {
-                if(line.match('    %') ) {
-                    var type = (line.substr(5)).replace("\r",""),
-                        out = "";
+    return Q.all(promises)
+}
 
-                    switch(type) {
-                    case "OtherGoodies":
-                        out = doPlatform( platforms[ i ] );
-                        break;
-                    case "Platform":
-                        out = platforms[ i ];
-                        break;
-                    default:
-                        break;
-                    }
-                        return line.replace( "%"+type, out );
-                    } else {
-                        return line.magenta;
-                }
-            }).join("\n");
-        }
+function listPlugins(projectRoot) {
+    var pluginPath = path.join(projectRoot, "plugins"),
+        plugins    = cordova_util.findPlugins(pluginPath),
+        deferred   = Q.defer();
 
-        return output;
+    if (!plugins.length) {
+        deferred.reject("No Plugins Currently Installed");
     } else {
-        return "No Platforms Currently Installed";
+        deferred.resolve("Plugins: \n\n" + plugins);
     }
+    return deferred.promise
 }
 
-function doPlugins( projectRoot ){
-    var pluginPath = path.join(projectRoot, 'plugins'),
-        plugins = cordova_util.findPlugins(pluginPath);
+function getProjectConfig(projectRoot) {
+    var deferred = Q.defer();
 
-    if( !plugins.length ) {
-        return "No Plugins Currently Installed";
+    if (!fs.existsSync(projectRoot)  ) {
+        deferred.reject("Config.xml file not found");
     } else {
-        return plugins;
+        deferred.resolve("Config.xml file: \n\n" + (fs.readFileSync(cordova_util.projectConfig(projectRoot),
"utf-8")));
     }
+    return deferred.promise
 }


Mime
View raw message