You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by da...@apache.org on 2016/03/09 20:12:47 UTC

android commit: CB-10749 Use cordova-common.CordovaLogger in cordova-android

Repository: cordova-android
Updated Branches:
  refs/heads/master 18e81c4b9 -> 82582e5a5


CB-10749 Use cordova-common.CordovaLogger in cordova-android

Added -d shorthand to all platform scripts


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

Branch: refs/heads/master
Commit: 82582e5a5bb3b7472d51812d902b5074227aa41b
Parents: 18e81c4
Author: daserge <v-...@microsoft.com>
Authored: Wed Mar 9 16:56:47 2016 +0300
Committer: daserge <v-...@microsoft.com>
Committed: Wed Mar 9 18:54:05 2016 +0300

----------------------------------------------------------------------
 bin/create                                 |  4 +-
 bin/templates/cordova/Api.js               | 33 ++++++++---
 bin/templates/cordova/build                |  2 +
 bin/templates/cordova/clean                | 14 ++++-
 bin/templates/cordova/lib/ConsoleLogger.js | 75 -------------------------
 bin/templates/cordova/lib/build.js         |  5 +-
 bin/templates/cordova/lib/prepare.js       |  2 +-
 bin/templates/cordova/lib/run.js           |  7 ++-
 bin/templates/cordova/loggingHelper.js     | 18 ++++++
 bin/templates/cordova/run                  |  2 +
 bin/update                                 |  4 +-
 11 files changed, 73 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/create
----------------------------------------------------------------------
diff --git a/bin/create b/bin/create
index 2052a27..b1e4d5a 100755
--- a/bin/create
+++ b/bin/create
@@ -28,7 +28,7 @@ var argv = require('nopt')({
     'shared' : Boolean,
     'link' : Boolean,
     'activity-name' : [String, undefined]
-});
+}, { 'd' : '--verbose' });
 
 if (argv.help || argv.argv.remain.length === 0) {
     console.log('Usage: ' + path.relative(process.cwd(), path.join(__dirname, 'create')) + ' <path_to_new_project> <package_name> <project_name> [<template_path>] [--activity-name <activity_name>] [--link]');
@@ -53,4 +53,6 @@ var options = {
     activityName: argv['activity-name']
 };
 
+require('./templates/cordova/loggingHelper').adjustLoggerLevel(argv);
+
 Api.createPlatform(argv.argv.remain[0], config, options).done();

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/Api.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js
index ad6f71c..0a207e4 100644
--- a/bin/templates/cordova/Api.js
+++ b/bin/templates/cordova/Api.js
@@ -29,11 +29,25 @@ var AndroidProject = require('./lib/AndroidProject');
 var PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger;
 var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
 
-var ConsoleLogger = require('./lib/ConsoleLogger');
 var pluginHandlers = require('./lib/pluginHandlers');
+var CordovaLogger = require('cordova-common').CordovaLogger;
+var selfEvents = require('cordova-common').events;
 
 var PLATFORM = 'android';
 
+function setupEvents(externalEventEmitter) {
+    if (externalEventEmitter) {
+        // This will make the platform internal events visible outside
+        selfEvents.forwardEventsTo(externalEventEmitter);
+        return externalEventEmitter;
+    }
+
+    // There is no logger if external emitter is not present, 
+    // so attach a console logger
+    CordovaLogger.get().subscribe(selfEvents);
+    return selfEvents;
+}
+
 /**
  * Class, that acts as abstraction over particular platform. Encapsulates the
  *   platform's properties and methods.
@@ -48,9 +62,8 @@ var PLATFORM = 'android';
 function Api(platform, platformRootDir, events) {
     this.platform = PLATFORM;
     this.root = path.resolve(__dirname, '..');
-    this.events = events || ConsoleLogger.get();
-    // NOTE: trick to share one EventEmitter instance across all js code
-    require('cordova-common').events = this.events;
+
+    setupEvents(events);
 
     this._platformJson = PlatformJson.load(this.root, platform);
     this._pluginInfoProvider = new PluginInfoProvider();
@@ -91,8 +104,10 @@ function Api(platform, platformRootDir, events) {
  *   instance or rejected with CordovaError.
  */
 Api.createPlatform = function (destination, config, options, events) {
+    events = setupEvents(events);
+
     return require('../../lib/create')
-    .create(destination, config, options, events || ConsoleLogger.get())
+    .create(destination, config, options, events)
     .then(function (destination) {
         var PlatformApi = require(path.resolve(destination, 'cordova/Api'));
         return new PlatformApi(PLATFORM, destination, events);
@@ -116,8 +131,10 @@ Api.createPlatform = function (destination, config, options, events) {
  *   instance or rejected with CordovaError.
  */
 Api.updatePlatform = function (destination, options, events) {
+    events = setupEvents(events);
+
     return require('../../lib/create')
-    .update(destination, options, events || ConsoleLogger.get())
+    .update(destination, options, events)
     .then(function (destination) {
         var PlatformApi = require(path.resolve(destination, 'cordova/Api'));
         return new PlatformApi('android', destination, events);
@@ -220,7 +237,7 @@ Api.prototype.addPlugin = function (plugin, installOptions) {
             .save_all();
 
         if (plugin.getFrameworks(self.platform).length > 0) {
-            self.events.emit('verbose', 'Updating build files since android plugin contained <framework>');
+            selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
             require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
         }
 
@@ -278,7 +295,7 @@ Api.prototype.removePlugin = function (plugin, uninstallOptions) {
             .save_all();
 
         if (plugin.getFrameworks(self.platform).length > 0) {
-            self.events.emit('verbose', 'Updating build files since android plugin contained <framework>');
+            selfEvents.emit('verbose', 'Updating build files since android plugin contained <framework>');
             require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles();
         }
 

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/build
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/build b/bin/templates/cordova/build
index de86a36..222e84a 100755
--- a/bin/templates/cordova/build
+++ b/bin/templates/cordova/build
@@ -41,6 +41,8 @@ var buildOpts = nopt({
 // Make buildOptions compatible with PlatformApi build method spec
 buildOpts.argv = buildOpts.argv.original;
 
+require('./loggingHelper').adjustLoggerLevel(buildOpts);
+
 new Api().build(buildOpts)
 .catch(function(err) {
     console.error(err.stack);

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/clean
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/clean b/bin/templates/cordova/clean
index 1f4a53d..dd85849 100755
--- a/bin/templates/cordova/clean
+++ b/bin/templates/cordova/clean
@@ -21,6 +21,7 @@
 
 var Api = require('./Api');
 var path  = require('path');
+var nopt = require('nopt');
 
 // Support basic help commands
 if(['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2]) >= 0) {
@@ -29,7 +30,18 @@ if(['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2]) >=
     process.exit(0);
 }
 
-new Api().clean({argv: process.argv.slice(2)})
+// Do some basic argument parsing
+var opts = nopt({
+    'verbose' : Boolean,
+    'silent' : Boolean
+}, { 'd' : '--verbose' });
+
+// Make buildOptions compatible with PlatformApi clean method spec
+opts.argv = opts.argv.original;
+
+require('./loggingHelper').adjustLoggerLevel(opts);
+
+new Api().clean(opts)
 .catch(function(err) {
     console.error(err.stack);
     process.exit(2);

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/lib/ConsoleLogger.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/lib/ConsoleLogger.js b/bin/templates/cordova/lib/ConsoleLogger.js
deleted file mode 100644
index cee2dc1..0000000
--- a/bin/templates/cordova/lib/ConsoleLogger.js
+++ /dev/null
@@ -1,75 +0,0 @@
-/**
-    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 loggerInstance;
-var util = require('util');
-var EventEmitter = require('events').EventEmitter;
-var CordovaError = require('cordova-common').CordovaError;
-
-/**
- * @class ConsoleLogger
- * @extends EventEmitter
- *
- * Implementing basic logging for platform. Inherits regular NodeJS
- *   EventEmitter. All events, emitted on this class instance are immediately
- *   logged to console.
- *
- * Also attaches handler to process' uncaught exceptions, so these exceptions
- *   logged to console similar to regular error events.
- */
-function ConsoleLogger() {
-    EventEmitter.call(this);
-
-    var isVerbose = process.argv.indexOf('-d') >= 0 || process.argv.indexOf('--verbose') >= 0;
-    // For CordovaError print only the message without stack trace unless we
-    // are in a verbose mode.
-    process.on('uncaughtException', function(err){
-        if ((err instanceof CordovaError) && isVerbose) {
-            console.error(err.stack);
-        } else {
-            console.error(err.message);
-        }
-        process.exit(1);
-    });
-
-    this.on('results', console.log);
-    this.on('verbose', function () {
-        if (isVerbose)
-            console.log.apply(console, arguments);
-    });
-    this.on('info', console.log);
-    this.on('log', console.log);
-    this.on('warn', console.warn);
-}
-util.inherits(ConsoleLogger, EventEmitter);
-
-/**
- * Returns already instantiated/newly created instance of ConsoleLogger class.
- *   This method should be used instead of creating ConsoleLogger directly,
- *   otherwise we'll get multiple handlers attached to process'
- *   uncaughtException
- *
- * @return  {ConsoleLogger}  New or already created instance of ConsoleLogger
- */
-ConsoleLogger.get = function () {
-    loggerInstance = loggerInstance || new ConsoleLogger();
-    return loggerInstance;
-};
-
-module.exports = ConsoleLogger;

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/lib/build.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/lib/build.js b/bin/templates/cordova/lib/build.js
index e1263ef..1abaa52 100644
--- a/bin/templates/cordova/lib/build.js
+++ b/bin/templates/cordova/lib/build.js
@@ -149,17 +149,16 @@ module.exports.runClean = function(options) {
 module.exports.run = function(options, optResolvedTarget) {
     var opts = parseOpts(options, optResolvedTarget, this.root);
     var builder = builders.getBuilder(opts.buildMethod);
-    var self = this;
     return builder.prepEnv(opts)
     .then(function() {
         if (opts.prepEnv) {
-            self.events.emit('verbose', 'Build file successfully prepared.');
+            events.emit('verbose', 'Build file successfully prepared.');
             return;
         }
         return builder.build(opts)
         .then(function() {
             var apkPaths = builder.findOutputApks(opts.buildType, opts.arch);
-            self.events.emit('log', 'Built the following apk(s): \n\t' + apkPaths.join('\n\t'));
+            events.emit('log', 'Built the following apk(s): \n\t' + apkPaths.join('\n\t'));
             return {
                 apkPaths: apkPaths,
                 buildType: opts.buildType,

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/lib/prepare.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/lib/prepare.js b/bin/templates/cordova/lib/prepare.js
index 3fba9fd..474ffd6 100644
--- a/bin/templates/cordova/lib/prepare.js
+++ b/bin/templates/cordova/lib/prepare.js
@@ -45,7 +45,7 @@ module.exports.prepare = function (cordovaProject) {
         handleSplashes(cordovaProject.projectConfig, self.root);
     })
     .then(function () {
-        self.events.emit('verbose', 'updated project successfully');
+        events.emit('verbose', 'updated project successfully');
     });
 };
 

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/lib/run.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/lib/run.js b/bin/templates/cordova/lib/run.js
index 3cc5c0d..027a482 100644
--- a/bin/templates/cordova/lib/run.js
+++ b/bin/templates/cordova/lib/run.js
@@ -25,7 +25,8 @@ var path  = require('path'),
     build = require('./build'),
     emulator = require('./emulator'),
     device   = require('./device'),
-    Q = require('q');
+    Q = require('q'),
+    events = require('cordova-common').events;
 
 function getInstallTarget(runOptions) {
     var install_target;
@@ -62,10 +63,10 @@ function getInstallTarget(runOptions) {
             return device.list()
             .then(function(device_list) {
                 if (device_list.length > 0) {
-                    self.events.emit('warn', 'No target specified, deploying to device \'' + device_list[0] + '\'.');
+                    events.emit('warn', 'No target specified, deploying to device \'' + device_list[0] + '\'.');
                     install_target = device_list[0];
                 } else {
-                    self.events.emit('warn', 'No target specified, deploying to emulator');
+                    events.emit('warn', 'No target specified, deploying to emulator');
                     install_target = '--emulator';
                 }
             });

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/loggingHelper.js
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/loggingHelper.js b/bin/templates/cordova/loggingHelper.js
new file mode 100644
index 0000000..32b2ee0
--- /dev/null
+++ b/bin/templates/cordova/loggingHelper.js
@@ -0,0 +1,18 @@
+var CordovaLogger = require('cordova-common').CordovaLogger;
+
+module.exports = {
+    adjustLoggerLevel: function (opts) {
+        if (opts instanceof Array) {
+            opts.silent = opts.indexOf('--silent') !== -1;
+            opts.verbose = opts.indexOf('--verbose') !== -1;
+        }
+
+        if (opts.silent) {
+            CordovaLogger.get().setLevel('error');
+        }
+
+        if (opts.verbose) {
+            CordovaLogger.get().setLevel('verbose');
+        }
+    }
+};

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/templates/cordova/run
----------------------------------------------------------------------
diff --git a/bin/templates/cordova/run b/bin/templates/cordova/run
index 1a7835e..9544c1d 100755
--- a/bin/templates/cordova/run
+++ b/bin/templates/cordova/run
@@ -44,6 +44,8 @@ var runOpts = nopt({
 // Make runOptions compatible with PlatformApi run method spec
 runOpts.argv = runOpts.argv.remain;
 
+require('./loggingHelper').adjustLoggerLevel(runOpts);
+
 new Api().run(runOpts)
 .catch(function(err) {
     console.error(err, err.stack);

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/82582e5a/bin/update
----------------------------------------------------------------------
diff --git a/bin/update b/bin/update
index 861c8d0..c51b7ff 100755
--- a/bin/update
+++ b/bin/update
@@ -24,7 +24,7 @@ var args  = require('nopt')({
     'link': Boolean,
     'shared': Boolean,
     'help': Boolean
-});
+}, { 'd' : '--verbose' });
 
 if (args.help || args.argv.remain.length === 0) {
     console.log('Usage: ' + path.relative(process.cwd(), path.join(__dirname, 'update')) + ' <path_to_project> [--link]');
@@ -32,4 +32,6 @@ if (args.help || args.argv.remain.length === 0) {
     process.exit(1);
 }
 
+require('./templates/cordova/loggingHelper').adjustLoggerLevel(args);
+
 Api.updatePlatform(args.argv.remain[0], {link: (args.link || args.shared)}).done();


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