Skip to content

Commit

Permalink
Fixes #156 - ios-sim 4.x - start does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed May 14, 2015
1 parent ac5cde8 commit ffd1a0a
Showing 1 changed file with 69 additions and 15 deletions.
84 changes: 69 additions & 15 deletions src/commands.js
Expand Up @@ -29,6 +29,50 @@ var path = require('path'),
simctl,
bplist;

function findFirstAvailableDevice(list) {
/*
// Example result:
{
name : 'iPhone 6',
id : 'A1193D97-F5EE-468D-9DBA-786F403766E6',
runtime : 'iOS 8.3'
}
*/

// the object to return
var ret_obj = {
name : null,
id : null,
runtime : null
};

var available_runtimes = {};

list.runtimes.forEach(function(runtime) {
if (runtime.available) {
available_runtimes[ runtime.name ] = true;
}
});


list.devices.some(function(deviceGroup) {
deviceGroup.devices.some(function(device){
if (available_runtimes[deviceGroup.runtime]) {
ret_obj = {
name : device.name,
id : device.id,
runtime : deviceGroup.runtime
};
return true;
}
return false;
});
return false;
});

return ret_obj;
}

function findRuntimesGroupByDeviceProperty(list, deviceProperty, availableOnly) {
/*
// Example result:
Expand Down Expand Up @@ -82,7 +126,15 @@ function findAvailableRuntime(list, device_name) {
return druntime.sort().pop();
}

function processDeviceTypeId(devicetypeid) {
function getDeviceFromDeviceTypeId(devicetypeid) {
/*
// Example result:
{
name : 'iPhone 6',
id : 'A1193D97-F5EE-468D-9DBA-786F403766E6',
runtime : 'iOS 8.3'
}
*/

// the object to return
var ret_obj = {
Expand All @@ -91,21 +143,26 @@ function processDeviceTypeId(devicetypeid) {
runtime : null
};

var options = { 'silent': true };
var list = simctl.list(options).json;

var arr = [];
if (devicetypeid) {
arr = devicetypeid.split(',');
}

// get the devicetype from --devicetypeid
// --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
var devicetype = null;
if (arr.length < 1) {
console.error('--devicetypeid was not specified.');
process.exit(1);
}

var devicetype = arr[0].trim();
if (arr.length > 1) {
ret_obj.runtime = arr[1].trim();
var dv = findFirstAvailableDevice(list);
console.error(util.format('--devicetypeid was not specified, using first available device: %s.', dv.name));
return dv;
} else {
devicetype = arr[0].trim();
if (arr.length > 1) {
ret_obj.runtime = arr[1].trim();
}
}

// check whether devicetype has the "com.apple.CoreSimulator.SimDeviceType." prefix, if not, add it
Expand All @@ -115,9 +172,6 @@ function processDeviceTypeId(devicetypeid) {
}

// now find the devicename from the devicetype
var options = { 'silent': true };
var list = simctl.list(options).json;

var devicename_found = list.devicetypes.some(function(deviceGroup) {
if (deviceGroup.id === devicetype) {
ret_obj.name = deviceGroup.name;
Expand Down Expand Up @@ -237,7 +291,7 @@ var command_lib = {

// get the deviceid from --devicetypeid
// --devicetypeid is a string in the form "devicetype, runtime_version" (optional: runtime_version)
var device = processDeviceTypeId(args.devicetypeid);
var device = getDeviceFromDeviceTypeId(args.devicetypeid);

// so now we have the deviceid, we can proceed
simctl.extensions.start(device.id);
Expand All @@ -256,11 +310,11 @@ var command_lib = {
start : function(args) {
var device = {};
try {
device = processDeviceTypeId(args.devicetypeid);
device = getDeviceFromDeviceTypeId(args.devicetypeid);
} catch (e) {
// do nothing
console.error(e);
}

simctl.extensions.start(device.id);
}
}
Expand Down

0 comments on commit ffd1a0a

Please sign in to comment.