Skip to content

Commit

Permalink
Merge pull request #3298 from cb1kenobi/master
Browse files Browse the repository at this point in the history
[TIMOB-11443] [TIMOB-11533] [TIMOB-11519]
  • Loading branch information
cb1kenobi committed Oct 19, 2012
2 parents 2842e39 + de7b83f commit d98e18d
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 102 deletions.
30 changes: 14 additions & 16 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var ti = require('titanium-sdk'),
version = appc.version,
wrench = require('wrench'),
androidEnv,
tiapp,
deployTypes = ['production', 'test', 'development'],
targets = ['emulator', 'device', 'dist-playstore'],
javaKeywords = [
Expand Down Expand Up @@ -203,7 +202,7 @@ exports.validate = function (logger, config, cli) {
i;

ti.validateProjectDir(logger, cli, cli.argv, 'project-dir');
if (!ti.validateCorrectSDK(logger, config, cli, cli.argv['project-dir'])) {
if (!ti.validateCorrectSDK(logger, config, cli)) {
// we're running the build command for the wrong SDK version, gracefully return
return false;
}
Expand Down Expand Up @@ -238,8 +237,7 @@ exports.validate = function (logger, config, cli) {
}

// Validate App ID
tiapp = new ti.tiappxml(path.join(cli.argv['project-dir'], 'tiapp.xml'));
tokens = tiapp.id.split('.');
tokens = cli.tiapp.id.split('.');
for ( i = 0; i < tokens.length; i++) {
if (javaKeywords.indexOf(tokens[i]) != -1) {
logger.error(__('Invalid java keyword used in project app id: %s', tokens[i]) + '\n');
Expand Down Expand Up @@ -360,16 +358,16 @@ function sendAnalytics(cli) {

cli.addAnalyticsEvent(eventName, {
dir: cli.argv['project-dir'],
name: tiapp.name,
publisher: tiapp.publisher,
url: tiapp.url,
image: tiapp.image,
appid: tiapp.id,
description: tiapp.description,
name: cli.tiapp.name,
publisher: cli.tiapp.publisher,
url: cli.tiapp.url,
image: cli.tiapp.image,
appid: cli.tiapp.id,
description: cli.tiapp.description,
type: cli.argv.type,
guid: tiapp.guid,
version: tiapp.version,
copyright: tiapp.copyright,
guid: cli.tiapp.guid,
version: cli.tiapp.version,
copyright: cli.tiapp.copyright,
date: (new Date()).toDateString()
});
}
Expand All @@ -386,7 +384,7 @@ function build(logger, config, cli, finished) {
// not actually used, yet
// logger.info(__('Compiling "%s" build', cli.argv['deploy-type']));

ti.legacy.constructLegacyCommand(cli, tiapp, cli.argv.platform , cmd, emulatorCmd);
ti.legacy.constructLegacyCommand(cli, cli.tiapp, cli.argv.platform , cmd, emulatorCmd);

// console.log('Forking correct SDK command: ' + ('python ' + cmd.join(' ')).cyan + '\n');

Expand All @@ -412,7 +410,7 @@ function build(logger, config, cli, finished) {
} else if (cli.argv['target'] == 'emulator') {
// Call the logcat command in the old builder.py after the emulator, so we get logcat output
spawn('python', [
path.join(path.resolve(cli.env.sdks[tiapp['sdk-version']].path), cli.argv.platform, 'builder.py'),
path.join(path.resolve(cli.env.sdks[cli.tiapp['sdk-version']].path), cli.argv.platform, 'builder.py'),
'logcat',
cli.argv['android-sdk'],
'-e'
Expand All @@ -426,7 +424,7 @@ function build(logger, config, cli, finished) {
'shell', 'am', 'start',
'-a', 'android.intent.action.MAIN',
'-c', 'android.intent.category.LAUNCHER',
'-n', tiapp.id + '/.' + appnameToClassname(tiapp.name) + 'Activity',
'-n', cli.tiapp.id + '/.' + appnameToClassname(cli.tiapp.name) + 'Activity',
'-f', '0x10200000'
], options).on('exit', function (code) {
if (code) {
Expand Down
2 changes: 1 addition & 1 deletion android/cli/commands/_create.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.config = function (logger, config, cli) {
exports.run = function (logger, config, cli, projectConfig) {
var templatePath = afs.resolvePath(path.dirname(module.filename), '..', '..', 'templates', cli.argv.type, cli.argv.template),
ignoreExtRegExp = /\.(png','.gif','.jpg','.zip','.a','.o', '.jar)$/,
projectDir = afs.resolvePath(cli.argv['project-dir'], cli.argv.name);
projectDir = afs.resolvePath(cli.argv['workspace-dir'], cli.argv.name);

if (afs.exists(templatePath)) {
if (cli.argv.type == 'app') {
Expand Down
50 changes: 34 additions & 16 deletions iphone/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ exports.config = function (logger, config, cli) {
},
'device-family': {
abbr: 'F',
default: process.env.TARGETED_DEVICE_FAMILY === '1' ? 'iphone' : process.env.TARGETED_DEVICE_FAMILY == '2' ? 'ipad' : 'universal',
desc: __('the device family to build for'),
values: Object.keys(deviceFamilies)
},
Expand Down Expand Up @@ -255,7 +254,7 @@ exports.validate = function (logger, config, cli) {

ti.validateProjectDir(logger, cli, cli.argv, 'project-dir');

if (!ti.validateCorrectSDK(logger, config, cli, cli.argv['project-dir'])) {
if (!ti.validateCorrectSDK(logger, config, cli)) {
// we're running the build command for the wrong SDK version, gracefully return
return false;
}
Expand Down Expand Up @@ -451,13 +450,32 @@ exports.validate = function (logger, config, cli) {
}
}

var deviceFamily = cli.argv['device-family'];
var deviceFamily = cli.argv['device-family'],
deploymentTargets = cli.tiapp['deployment-targets'];
if (!deviceFamily && process.env.TARGETED_DEVICE_FAMILY) {
// device family was not specified at the command line, but we did get it via an environment variable!
deviceFamily = process.env.TARGETED_DEVICE_FAMILY === '1' ? 'iphone' : process.env.TARGETED_DEVICE_FAMILY == '2' ? 'ipad' : 'universal';
}
if (!deviceFamily && deploymentTargets) {
// device family was not an environment variable, construct via the tiapp.xml's deployment targets
if (deploymentTargets.iphone && deploymentTargets.ipad) {
deviceFamily = 'universal';
} else if (deploymentTargets.iphone) {
deviceFamily = 'iphone';
} else if (deploymentTargets.ipad) {
deviceFamily = 'ipad';
}
}

if (!deviceFamily || !deviceFamilies[deviceFamily]) {
logger.error(__('Invalid device family "%s"', deviceFamily) + '\n');
appc.string.suggest(deviceFamily, Object.keys(deviceFamilies), logger.log, 3);
process.exit(1);
}

// device family may have been modified, so set it back in the args
cli.argv['device-family'] = deviceFamily;

if (cli.argv['debug-host'] && cli.argv.target != 'dist-appstore') {
if (typeof cli.argv['debug-host'] == 'number') {
logger.error(__('Invalid debug host "%s"', cli.argv['debug-host']) + '\n');
Expand Down Expand Up @@ -494,7 +512,7 @@ exports.run = function (logger, config, cli, finished) {
if (cli.argv.xcode) {
// basically, we bypass the pre, post, and finalize hooks for xcode builds
var buildObj = new build(logger, config, cli, finished);
sendAnalytics(cli, buildObj.tiapp);
sendAnalytics(cli);
} else {
cli.fireHook('build.pre.construct', function () {
new build(logger, config, cli, function (err) {
Expand All @@ -505,7 +523,7 @@ exports.run = function (logger, config, cli, finished) {
line && logger.error(line);
});
}
sendAnalytics(cli, this.tiapp);
sendAnalytics(cli);
cli.fireHook('build.finalize', this, function () {
finished(err);
});
Expand All @@ -515,7 +533,7 @@ exports.run = function (logger, config, cli, finished) {
}
};

function sendAnalytics(cli, tiapp) {
function sendAnalytics(cli) {
var eventName = cli.argv['device-family'] + '.' + cli.argv.target;

if (cli.argv.target == 'dist-appstore' || cli.argv.target == 'dist-adhoc') {
Expand All @@ -528,16 +546,16 @@ function sendAnalytics(cli, tiapp) {

cli.addAnalyticsEvent(eventName, {
dir: cli.argv['project-dir'],
name: tiapp.name,
publisher: tiapp.publisher,
url: tiapp.url,
image: tiapp.image,
appid: tiapp.id,
description: tiapp.description,
name: cli.tiapp.name,
publisher: cli.tiapp.publisher,
url: cli.tiapp.url,
image: cli.tiapp.image,
appid: cli.tiapp.id,
description: cli.tiapp.description,
type: cli.argv.type,
guid: tiapp.guid,
version: tiapp.version,
copyright: tiapp.copyright,
guid: cli.tiapp.guid,
version: cli.tiapp.version,
copyright: cli.tiapp.copyright,
date: (new Date()).toDateString()
});
}
Expand All @@ -554,7 +572,7 @@ function build(logger, config, cli, finished) {
this.projectDir = cli.argv['project-dir'];
this.buildDir = path.join(this.projectDir, 'build', this.platformName);
this.assetsDir = path.join(this.buildDir, 'assets');
this.tiapp = new ti.tiappxml(path.join(this.projectDir, 'tiapp.xml'));
this.tiapp = cli.tiapp;
this.target = cli.argv.target;
this.provisioningProfileUUID = cli.argv['pp-uuid'];

Expand Down
2 changes: 1 addition & 1 deletion iphone/cli/commands/_create.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.config = function (logger, config, cli) {

exports.run = function (logger, config, cli, projectConfig) {
var templatePath = afs.resolvePath(path.dirname(module.filename), '..', '..', 'templates', cli.argv.type, cli.argv.template),
projectDir = afs.resolvePath(cli.argv['project-dir'], cli.argv.name);
projectDir = afs.resolvePath(cli.argv['workspace-dir'], cli.argv.name);
if (afs.exists(templatePath)) {
wrench.copyDirSyncRecursive(templatePath, projectDir, { preserve: true });
}
Expand Down
102 changes: 76 additions & 26 deletions iphone/cli/hooks/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var appc = require('node-appc'),
path = require('path'),
parallel = require('async').parallel,
cp = require('child_process'),
exec = cp.exec;
exec = cp.exec,
spawn = cp.spawn;

exports.cliVersion = '>=3.X';

Expand All @@ -32,6 +33,9 @@ exports.init = function (logger, config, cli) {

logger.info(__('Running application in iOS Simulator'));

var simulatorDir = afs.resolvePath('~/Library/Application Support/iPhone Simulator/' + build.iosSimVersion + '/Applications'),
logFile = build.tiapp.guid + '.log';

parallel([
function (next) {
logger.debug(__('Terminating all iOS simulators'));
Expand All @@ -47,23 +51,17 @@ exports.init = function (logger, config, cli) {
},

function (next) {
var simulatorDir = afs.resolvePath('~/Library/Application Support/iPhone Simulator/' + build.iosSimVersion),
logFile = build.tiapp.guid + '.log';

// sometimes the simulator doesn't remove old log files in which case we get
// our logging jacked - we need to remove them before running the simulator
if (!afs.exists(simulatorDir)) {
return next();
}

afs.visitFiles(simulatorDir, function (filename, fullpath) {
if (filename == logFile) {
try {
logger.debug(__('Removing old log file: %s', fullpath.cyan));
fs.unlink(fullpath);
} catch (e) {}
afs.exists(simulatorDir) && fs.readdirSync(simulatorDir).forEach(function (guid) {
var file = path.join(simulatorDir, guid, 'Documents', logFile);
if (afs.exists(file)) {
logger.debug(__('Removing old log file: %s', file.cyan));
fs.unlinkSync(file);
}
}, next);
});

next();
}
], function () {
var cmd = [
Expand All @@ -75,41 +73,93 @@ exports.init = function (logger, config, cli) {
'--family',
build.deviceFamily
],
timer = setTimeout(function () {
findLogTimer,
simActivateTimer = setTimeout(function () {
exec([
'osascript',
'"' + path.join(build.titaniumIosSdkPath, 'iphone_sim_activate.scpt') + '"',
'"' + path.join(build.xcodeEnv.path, 'Platforms', 'iPhoneSimulator.platform', 'Developer', 'Applications', 'iPhone Simulator.app') + '"'
].join(' '), function (err, stdout, stderr) {
if (err) {
console.log('ACT ERR! ' + stderr);
logger.error(__('Failed to activate the iPhone Simulator window'));
logger.error(stderr);
}
})
}, 500);
}, 500),
simProcess,
simErr = [],
stripLogLevelRE = new RegExp('[(?:' + logger.getLevels().join('|') + ')] '),
logLevelRE = new RegExp('(\u001b\\[\\d+m)?\\[?(' + logger.getLevels().join('|') + ')\\]?\s*(\u001b\\[\\d+m)?(.*)', 'i'),
logProcess;

cli.argv.retina && cmd.push('--retina');
cmd = cmd.join(' ');

logger.info(__('Launching application in iOS Simulator'));
logger.debug(__('Simulator command: %s', cmd.cyan));

exec('/bin/sh -c "' + cmd.replace(/"/g, '\\"') + '"', {
simProcess = spawn('/bin/sh', ['-c', cmd], {
cwd: build.titaniumIosSdkPath,
env: {
DYLD_FRAMEWORK_PATH: path.join(build.xcodeEnv.path, 'Platforms', 'iPhoneSimulator.platform', 'Developer', 'Library', 'PrivateFrameworks') +
':' + afs.resolvePath(build.xcodeEnv.path, '..', 'OtherFrameworks')
}
}, function (err, stdout, stderr) {
if (err) {
clearTimeout(timer);
finished(new appc.exception(__('An error occurred running the iOS Simulator'), stderr.split('\n').map(function (line) {
return line.replace(/^[(?:TRACE|DEBUG|INFO|WARN|ERROR)] /, '');
})));
});

simProcess.stderr.on('data', function (data) {
data.toString().split('\n').forEach(function (line) {
line.length && simErr.push(line.replace(stripLogLevelRE, ''));
}, this);
}.bind(this));

simProcess.on('exit', function (code, signal) {
clearTimeout(simActivateTimer);
clearTimeout(findLogTimer);
logProcess && logProcess.kill();

if (code) {
finished(new appc.exception(__('An error occurred running the iOS Simulator'), simErr));
} else {
logger.info(__('Application has exited from iOS Simulator'));
finished();
}
});
}.bind(this));

function findLogFile() {
var files = fs.readdirSync(simulatorDir),
file,
i = 0,
l = files.length;

for (; i < l; i++) {
file = path.join(simulatorDir, files[i], 'Documents', logFile);
if (afs.exists(file)) {
logger.debug(__('Found iPhone Simulator log file: %s', file.cyan));
logger.info(__('iPhone Simulator log:'));

logProcess = spawn('/usr/bin/tail', ['-f', file]);

logProcess.stdout.on('data', function (data) {
data.toString().split('\n').forEach(function (line) {
if (line) {
var m = line.match(logLevelRE);
if (m) {
logger[m[2].toLowerCase()](m[4].trim());
} else {
logger.debug(line);
}
}
});
});

return;
}
}

findLogTimer = setTimeout(findLogFile, 250);
}

afs.exists(simulatorDir) && findLogFile();
});
}
});
Expand Down

0 comments on commit d98e18d

Please sign in to comment.