Skip to content

Commit

Permalink
refactor setDevice logic and capture ios8 device udid
Browse files Browse the repository at this point in the history
  • Loading branch information
jlipps committed Oct 13, 2014
1 parent 07fdefd commit 77b558c
Showing 1 changed file with 108 additions and 104 deletions.
212 changes: 108 additions & 104 deletions lib/devices/ios/ios.js
Expand Up @@ -94,6 +94,7 @@ IOS.prototype.init = function () {
this.xcodeVersion = null;
this.iOSSDKVersion = null;
this.iosSimProcess = null;
this.iosSimUdid = null;
this.logs = {};
this.instruments = null;
this.commandProxy = null;
Expand Down Expand Up @@ -277,8 +278,10 @@ IOS.prototype.start = function (cb, onDie) {
this.setLocale.bind(this),
this.createInstruments.bind(this),
this.setPreferences.bind(this),
this.runSimReset.bind(this),
this.startLogCapture.bind(this),
this.setDeviceAndLaunchSimulator.bind(this),
this.setDeviceInfo.bind(this),
this.prelaunchSimulator.bind(this),
this.setBundleIdFromApp.bind(this),
this.installToRealDevice.bind(this),
this.startInstruments.bind(this),
Expand Down Expand Up @@ -1020,8 +1023,11 @@ IOS.prototype.getDeviceString = function () {
return iosDeviceString;
};

IOS.prototype.setDeviceTypeInInfoPlist = function (deviceTypeCode, cb) {
IOS.prototype.setDeviceTypeInInfoPlist = function (cb) {
var plist = path.resolve(this.args.app, "Info.plist");
var dString = this.getDeviceString();
var isiPhone = dString.toLowerCase().indexOf("ipad") === -1;
var deviceTypeCode = isiPhone ? 1 : 2;
parsePlistFile(plist, function (err, obj) {
if (err) {
logger.error("Could not set the device type in Info.plist");
Expand Down Expand Up @@ -1075,12 +1081,22 @@ IOS.prototype.checkDeviceAvailable = function (cb) {
var dString = this.getDeviceString();
if (this.iOSSDKVersion >= 8) {
var matchedDevice = null;
var matchedUdid = null;
_.each(availDevices, function (device) {
if (device.indexOf(dString) !== -1) {
matchedDevice = device;
try {
matchedUdid = /.+\[([^\]]+)\]/.exec(device)[1];
} catch (e) {
matchedUdid = null;
}
}
});
if (matchedDevice === null) return noDevicesError();
}.bind(this));
if (matchedDevice === null || matchedUdid === null) {
return noDevicesError();
}
this.iosSimUdid = matchedUdid;
logger.debug("iOS8 sim UDID is " + this.iosSimUdid);
return cb();
} else if (!_.contains(availDevices, dString)) {
return noDevicesError();
Expand All @@ -1093,97 +1109,80 @@ IOS.prototype.checkDeviceAvailable = function (cb) {
}
};

IOS.prototype.setDeviceAndLaunchSimulator = function (cb) {
var msg;
IOS.prototype.setDeviceInfo = function (cb) {
this.shouldPrelaunchSimulator = false;
if (this.args.udid) {
logger.debug("Not setting device type since we're connected to a device");
cb();
} else if (!this.args.app && this.args.bundleId) {
logger.debug("Not setting device type since we're on a real device");
return cb();
}

if (!this.args.app && this.args.bundleId) {
logger.debug("Not setting device type since we're using bundle ID and " +
"assuming app is already installed");
cb(null);
} else if (!this.args.deviceName && this.args.forceIphone === null && this.args.forceIpad === null) {
logger.debug("No device specified, current device in the iOS simulator will be used.");
cb(null);
} else if (this.args.defaultDevice || this.iOSSDKVersion >= 8) {
if (this.iOSSDKVersion >= 8) {
logger.debug("We're on iOS8 so forcing defaultDevice on");
return cb();
}

if (!this.args.deviceName &&
this.args.forceIphone === null &&
this.args.forceIpad === null) {
logger.debug("No device specified, current device in the iOS " +
"simulator will be used.");
return cb();
}

if (this.args.defaultDevice || this.iOSSDKVersion >= 7.1) {
if (this.iOSSDKVersion >= 7.1) {
logger.debug("We're on iOS7.1+ so forcing defaultDevice on");
} else {
logger.debug("User specified default device, letting instruments launch it");
}
this.checkDeviceAvailable(function (err) {
if (err) return cb(err);
var dString = this.getDeviceString();
var isiPhone = dString.toLowerCase().indexOf("ipad") === -1;
this.setDeviceTypeInInfoPlist(isiPhone ? 1 : 2, cb);
this.setDeviceTypeInInfoPlist(cb);
}.bind(this));
} else {
var iosSimPath = path.resolve(this.xcodeFolder, "Platforms/iPhoneSimulator.platform/Developer/Applications" +
"/iPhone Simulator.app/Contents/MacOS/iPhone Simulator");
if (!fs.existsSync(iosSimPath)) {
msg = "Could not find ios simulator binary at " + iosSimPath;
logger.error(msg);
cb(new Error(msg));
} else {
return;
}

var cleanup = function (cb) {
if ((this.args.fullReset || this.args.reset) && this.dontDeleteSimApps) {
logger.debug("Not deleting simulator apps since we need to update " +
"their plists before launch");
return cb();
}
if (this.args.fullReset) {
this.deleteSim(cb);
} else if (this.args.reset) {
this.cleanupAppState(cb);
} else {
cb();
}
}.bind(this);

var setDeviceInfo = function (cb) {
var isiPhone = this.getDeviceString().toLowerCase().indexOf("ipad") === -1;
this.setDeviceTypeInInfoPlist(isiPhone ? 1 : 2, cb);
}.bind(this);

var startSim = function (cb) {
logger.debug("Launching device: " + this.getDeviceString());
var iosSimArgs = ["-SimulateDevice", this.getDeviceString()];
this.iosSimProcess = spawn(iosSimPath, iosSimArgs);
var waitForSimulatorLogs = function (countdown) {
if (countdown <= 0 || (this.logs.syslog && (this.logs.syslog.getAllLogs().length > 0 ||
(this.logs.crashlog && this.logs.crashlog.getAllLogs().length > 0)))) {
logger.debug(countdown > 0 ? "Simulator is now ready." : "Waited 10 seconds for simulator to start.");
cb();
} else {
setTimeout(function () {
waitForSimulatorLogs(countdown - 1);
}, 1000);
}
}.bind(this);
waitForSimulatorLogs(10);
}.bind(this);

if (this.iOSSDKVersion < 7.1) {
async.series([
this.endSimulator.bind(this),
cleanup,
setDeviceInfo,
startSim
], function (err) {
cb(err);
});
this.shouldPrelaunchSimulator = true;
this.setDeviceTypeInInfoPlist(cb);
};

IOS.prototype.prelaunchSimulator = function (cb) {
var msg;
if (!this.shouldPrelaunchSimulator) {
logger.debug("Not pre-launching simulator");
return cb();
}

logger.debug("Pre-launching simulator");
var iosSimPath = path.resolve(this.xcodeFolder,
"Platforms/iPhoneSimulator.platform/Developer/Applications" +
"/iPhone Simulator.app/Contents/MacOS/iPhone Simulator");
if (!fs.existsSync(iosSimPath)) {
msg = "Could not find ios simulator binary at " + iosSimPath;
logger.error(msg);
return cb(new Error(msg));
}
this.endSimulator(function (err) {
if (err) return cb(err);
logger.debug("Launching device: " + this.getDeviceString());
var iosSimArgs = ["-SimulateDevice", this.getDeviceString()];
this.iosSimProcess = spawn(iosSimPath, iosSimArgs);
var waitForSimulatorLogs = function (countdown) {
if (countdown <= 0 ||
(this.logs.syslog && (this.logs.syslog.getAllLogs().length > 0 ||
(this.logs.crashlog && this.logs.crashlog.getAllLogs().length > 0)))) {
logger.debug(countdown > 0 ? "Simulator is now ready." :
"Waited 10 seconds for simulator to start.");
cb();
} else {
async.series([
this.endSimulator.bind(this),
this.checkDeviceAvailable.bind(this),
cleanup,
], function (err) {
cb(err);
});
setTimeout(function () {
waitForSimulatorLogs(countdown - 1);
}, 1000);
}
}
}
}.bind(this);
waitForSimulatorLogs(10);
}.bind(this));
};

IOS.prototype.parseLocalizableStrings = function (cb, language) {
Expand Down Expand Up @@ -1320,25 +1319,9 @@ IOS.prototype.cleanupAppState = function (cb) {
}.bind(this));
};

IOS.prototype.postCleanup = function (cb) {
this.curCoords = null;
this.curOrientation = null;

if (!_.isEmpty(this.logs)) {
this.logs.syslog.stopCapture();
this.logs = {};
}

if (this.remote) {
this.stopRemote();
}

var final = function () {
this.isShuttingDown = false;
cb();
}.bind(this);

IOS.prototype.runSimReset = function (cb) {
if (this.args.reset || this.args.fullReset) {
logger.debug("Running ios sim reset flow");
// The simulator process must be ended before we delete applications.
async.series([
function (cb) { this.endSimulator(cb); }.bind(this),
Expand All @@ -1356,12 +1339,33 @@ IOS.prototype.postCleanup = function (cb) {
cb();
}
}.bind(this),
], final);
], cb);
} else {
logger.debug("Reset set to false, not ending sim or cleaning up app state");
final();
logger.debug("Reset not set, not ending sim or cleaning up app state");
cb();
}
};

IOS.prototype.postCleanup = function (cb) {
this.curCoords = null;
this.curOrientation = null;

if (!_.isEmpty(this.logs)) {
this.logs.syslog.stopCapture();
this.logs = {};
}

if (this.remote) {
this.stopRemote();
}

this.runSimReset(function () {
// ignore any errors during reset and continue shutting down
this.isShuttingDown = false;
cb();
}.bind(this));


};

IOS.prototype.endSimulator = function (cb) {
Expand Down

0 comments on commit 77b558c

Please sign in to comment.