Skip to content

Commit

Permalink
Merge pull request #6297 from skypanther/TIMOB12169
Browse files Browse the repository at this point in the history
[TIMOB-12169] Create build log with compilation
  • Loading branch information
cb1kenobi committed Nov 18, 2014
2 parents beaaddf + 2e9ce8a commit 8e336c9
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 11 deletions.
85 changes: 85 additions & 0 deletions cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var appc = require('node-appc'),
fs = require('fs'),
jsanalyze = require('titanium-sdk/lib/jsanalyze'),
path = require('path'),
sprintf = require('sprintf'),
ti = require('titanium-sdk'),
tiappxml = require('titanium-sdk/lib/tiappxml'),
__ = appc.i18n(__dirname).__;
Expand Down Expand Up @@ -106,6 +107,9 @@ exports.config = function (logger, config, cli) {
}
}

// start file logging here
patchLogger(logger, cli);

// load the tiapp.xml
try {
var tiapp = cli.tiapp = new tiappxml(path.join(projectDir, 'tiapp.xml'));
Expand Down Expand Up @@ -257,3 +261,84 @@ exports.run = function (logger, config, cli, finished) {
}
});
};

/**
* Monkey-patch the logger object to enable file logging during build
* @param {Object} logger - The logger instance
* @param {Object} cli - The CLI instance
*/
function patchLogger(logger, cli) {
var origLoggerLog = logger.log,
platform = ti.resolvePlatform(cli.argv.platform),
logFileStream;
// create our write stream
logFileStream = fs.createWriteStream(path.join(cli.argv['project-dir'], 'build', 'build_' + platform + '.log'), { 'flags': 'w', 'encoding': 'ascii' });
// write the banner to start out the log
logFileStream.write(logBanner(cli));
// override the existing log function
logger.log = function() {
// most of this copied from the CLI's logger.js logger.log() function
var args = Array.prototype.slice.call(arguments),
padLevels = logger.padLevels,
prefix;

// if there are no args (i.e. a blank line), we need at least one space
args.length || args.unshift(' ');

// if we're not being called from info/warn/error/debug, then set this as a general log entry
args[0] in logger.levels || args.unshift('_');

// turn off padding
logger.padLevels = args[0] != '_';

// get rid of any null args
while (args.length && args[args.length-1] == null) args.pop();

// if we're logging an error, we need to cast to a string so that sprintf doesn't complain
if (args[1] instanceof Error || Object.prototype.toString.call(args[1]) == '[object Error]') {
args[1] = (args[1].stack || args[1].toString()) + '\n';
} else if (args[1] == null || args[1] == undefined) {
args[1] = '';
}

typeof type != 'string' && (args[1] = ''+args[1]);

// strip off starting full colons
args[1] = args[1].replace(/:\s{1}/, ' ');

// add [INFO] type prefixes for each line
prefix = (args[0] != "_") ? "[" + args[0].toUpperCase() + "]" + ((args[0].length===5) ? ' ' : ' ') : "";

// log it to our log file, stripping out the color codes
logFileStream.write("\n" + prefix + args[1].replace(/\x1B\[\d+m/g, ''));

// call the original logger with our cleaned up args
origLoggerLog.apply(logger, [args[0], args.length > 2 ? sprintf.apply(null, args.slice(1)) : args[1]]);

// restore padding
logger.padLevels = padLevels;
}
}

/**
* Outputs environment details at the top of the log file
* for each run of `titanium build`
* @param {Object} cli - The CLI instance
*
* See http://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history for
* os.release() to version mapping for OS X. (e.g. 14.0.0 is v10.10 Yosemite)
*/
function logBanner(cli) {
var os = require('os');
return '\n\n---------------------------------\n\n' +
new Date().toLocaleString() + '\n\n' +
'Build Environment \n' +
' Host OS = ' + (os.platform()==='darwin' ? "OS X" : os.platform()) + ' ' + os.release() + ', ' + os.arch() + '\n' +
' Target platform = ' + ti.resolvePlatform(cli.argv.platform) + '\n' +
' CLI version = ' + cli.version + '\n' +
' SDK version = ' + cli.argv.sdk + '\n' +
' SDK path = ' + cli.sdk.path + '\n' +
' Node version = ' + process.version + '\n' +
' Command = ' + cli.argv.$ + ' ' + cli.argv.$_.join(' ') + '\n' +
'\n';
}
29 changes: 18 additions & 11 deletions cli/commands/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ exports.run = function (logger, config, cli) {
} else {
logger.debug(__('Directory does not exist %s', dir.cyan));
}
dir = path.join(buildDir, 'build_' + platform + '.log');
if (appc.fs.exists(dir)) {
logger.debug(__('Deleting %s', dir.cyan));
fs.unlinkSync(dir);
} else {
logger.debug(__('Build log does not exist %s', dir.cyan));
}
cli.fireHook('clean.' + platform + '.post', function () {
cli.fireHook('clean.post', function () {
next();
Expand All @@ -113,18 +120,18 @@ exports.run = function (logger, config, cli) {
cli.fireHook('clean.pre', function () {
async.series(fs.readdirSync(buildDir).map(function (dir) {
return function (next) {
var fulldir = path.join(buildDir, dir);
if (fs.lstatSync(fulldir).isDirectory()) {
cli.fireHook('clean.' + dir + '.pre', function () {
logger.debug(__('Deleting %s', fulldir.cyan));
wrench.rmdirSyncRecursive(fulldir);
cli.fireHook('clean.' + dir + '.post', function () {
next();
});
var file = path.join(buildDir, dir);
cli.fireHook('clean.' + dir + '.pre', function () {
logger.debug(__('Deleting %s', file.cyan));
if (fs.lstatSync(file).isDirectory()) {
wrench.rmdirSyncRecursive(file);
} else {
fs.unlinkSync(file);
}
cli.fireHook('clean.' + dir + '.post', function () {
next();
});
} else {
next();
}
});
};
}), function () {
cli.fireHook('clean.post', function () {
Expand Down
1 change: 1 addition & 0 deletions node_modules/sprintf/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions node_modules/sprintf/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e336c9

Please sign in to comment.