Skip to content

Commit e80db60

Browse files
Fatme HavaluovaFatme Havaluova
authored andcommitted
Specify device type for Xcode 6 simulator
1 parent 45740a4 commit e80db60

File tree

6 files changed

+160
-24
lines changed

6 files changed

+160
-24
lines changed

lib/declarations.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,19 @@ interface ICommand {
1111

1212
interface ICommandExecutor {
1313
execute(): IFuture<void>;
14+
}
15+
16+
interface IDevice {
17+
device: any; // NodObjC wrapper to device
18+
deviceTypeIdentifier: string;
19+
runtimeVersion: string;
20+
}
21+
22+
interface IDictionary<T> {
23+
[key: string]: T;
24+
}
25+
26+
interface ISimulator {
27+
validateDeviceIdentifier(): void;
28+
setSimulatedDevice(config: any): void;
1429
}

lib/iphone-simulator-xcode-5.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
///<reference path="./.d.ts"/>
2+
"use strict";
3+
4+
import options = require("./options");
5+
import util = require("util");
6+
7+
8+
export class XCode5Simulator implements ISimulator {
9+
10+
private static DEFAULT_DEVICE_IDENTIFIER = "Resizable-iPad";
11+
12+
private static allowedDeviceIdentifiers = [
13+
"iPhone",
14+
"iPhone Retina (3.5-inch)",
15+
"iPhone Retina (4-inch)",
16+
"iPhone Retina (4-inch 64-bit)",
17+
"iPad",
18+
"iPad Retina",
19+
"iPad Retina (64-bit)"
20+
];
21+
22+
public validateDeviceIdentifier(): void {
23+
}
24+
25+
public setSimulatedDevice(config:any): void {
26+
config("setSimulatedDeviceInfoName", this.deviceIdentifier);
27+
}
28+
29+
private get deviceIdentifier() {
30+
return options.devie || XCode5Simulator.DEFAULT_DEVICE_IDENTIFIER;
31+
}
32+
}

lib/iphone-simulator-xcode-6.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
///<reference path="./.d.ts"/>
2+
"use strict";
3+
import options = require("./options");
4+
import utils = require("./utils");
5+
import util = require("util");
6+
var $ = require("NodObjC");
7+
8+
export class XCode6Simulator implements ISimulator {
9+
10+
private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType";
11+
private static DEFAULT_DEVICE_IDENTIFIER = "Resizable-iPad";
12+
13+
private static allowedDeviceIdentifiers = [
14+
"iPhone-4s",
15+
"iPhone-5",
16+
"iPhone-5s",
17+
"iPhone-6",
18+
"iPhone-6-Plus",
19+
"Resizable-iPhone",
20+
"iPad-2",
21+
"iPad-Retina",
22+
"iPad-Air",
23+
"Resizable-iPad"
24+
];
25+
26+
private availableDevices: IDictionary<IDevice>;
27+
28+
constructor() {
29+
this.availableDevices = Object.create(null);
30+
}
31+
32+
public validateDeviceIdentifier(): void {
33+
if(!_.contains(XCode6Simulator.allowedDeviceIdentifiers, this.deviceIdentifier)) {
34+
throw new Error(util.format("Invalid device identifier %s. Valid device identifiers are %s.", this.deviceIdentifier, utils.stringify(XCode6Simulator.allowedDeviceIdentifiers)));
35+
}
36+
}
37+
38+
public setSimulatedDevice(config: any): void {
39+
var device = this.getDeviceByIdentifier(this.deviceIdentifier);
40+
config("setDevice", device);
41+
}
42+
43+
private get deviceIdentifier(): string {
44+
return options.device || XCode6Simulator.DEFAULT_DEVICE_IDENTIFIER;
45+
}
46+
47+
private getAvailableDevices(): IDictionary<IDevice> {
48+
if(utils.isEmptyDictionary(this.availableDevices)) {
49+
var deviceSet = $.classDefinition.getClassByName("SimDeviceSet")("defaultSet");
50+
var devices = deviceSet("availableDevices");
51+
var count = devices("count");
52+
if(count > 0) {
53+
for(var index=0; index<count; index++) {
54+
var device = devices("objectAtIndex", index);
55+
var deviceTypeIdentifier = device("deviceType")("identifier").toString();
56+
var runtimeVersion = device("runtime")("versionString").toString();
57+
this.availableDevices[deviceTypeIdentifier] = {
58+
device: device,
59+
deviceTypeIdentifier: deviceTypeIdentifier,
60+
runtimeVersion: runtimeVersion
61+
};
62+
}
63+
}
64+
}
65+
66+
return this.availableDevices;
67+
}
68+
69+
private getDeviceByIdentifier(deviceIdentifier: string): any {
70+
var fullDeviceIdentifier = util.format("%s.%s", XCode6Simulator.DEVICE_IDENTIFIER_PREFIX, deviceIdentifier);
71+
var availableDevices = this.getAvailableDevices();
72+
if(!utils.isEmptyDictionary(availableDevices)) {
73+
var device = availableDevices[fullDeviceIdentifier];
74+
if(device) {
75+
return device.device;
76+
}
77+
}
78+
79+
throw new Error(util.format("Unable to find device with identifier ", deviceIdentifier));
80+
}
81+
}

lib/iphone-simulator.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import path = require("path");
88
import util = require("util");
99

1010
import options = require("./options");
11+
import utils = require("./utils");
12+
import xcode6SimulatorLib = require("./iphone-simulator-xcode-6");
13+
import xcode5SimulatorLib = require("./iphone-simulator-xcode-5");
1114

1215
var $ = require("NodObjC");
1316

@@ -52,17 +55,11 @@ export class iPhoneSimulator implements IiPhoneSimulator {
5255

5356
var sessionDelegate = $.NSObject.extend("DTiPhoneSimulatorSessionDelegate");
5457
sessionDelegate.addMethod("session:didEndWithError:", "v@:@@", function(self: any, sel: any, sess: any, error: any) {
55-
console.log("Session ended with error: ");
56-
console.log(error);
57-
process.exit(1);
58+
iPhoneSimulator.logSessionInfo(error, "Session ended without errors.", "Session ended with error ");
59+
process.exit(0);
5860
});
59-
sessionDelegate.addMethod("session:didStart:withError:", "v@:@c@", function(self: any, sel: any, sess: any, did: any, err:any) {
60-
if(err) {
61-
console.log("Session started with error ", err);
62-
process.exit(1);
63-
} else {
64-
console.log("Session started without errors");
65-
}
61+
sessionDelegate.addMethod("session:didStart:withError:", "v@:@c@", function(self: any, sel: any, sess: any, did: any, error:any) {
62+
iPhoneSimulator.logSessionInfo(error, "Session started without errors.", "Session started with error ");
6663
});
6764
sessionDelegate.register();
6865

@@ -73,22 +70,15 @@ export class iPhoneSimulator implements IiPhoneSimulator {
7370
var sdkRoot = options.sdkRoot ? $(options.sdkRoot) : this.getClassByName("DTiPhoneSimulatorSystemRoot")("defaultRoot");
7471
config("setSimulatedSystemRoot", sdkRoot);
7572

76-
var family = 1;
77-
if(options.family) {
78-
if(options.family.toLowerCase() === "ipad") {
79-
family = 2;
80-
}
73+
var simulator: ISimulator;
74+
if(_.contains(config.methods(), "setDevice:")) {
75+
simulator = new xcode6SimulatorLib.XCode6Simulator();
76+
} else {
77+
simulator = new xcode5SimulatorLib.XCode5Simulator();
8178
}
82-
config("setSimulatedDeviceFamily", $.NSNumber("numberWithInt", family));
8379

84-
if(options.env) {
85-
var env = $.NSMutableDictionary("dictionary");
86-
Object.keys(env).forEach(key => {
87-
env("setObject", $(env[key]), "forKey", $(key));
88-
});
89-
90-
config("setSimulatedApplicationLaunchEnvironment", env);
91-
}
80+
simulator.validateDeviceIdentifier();
81+
simulator.setSimulatedDevice(config);
9282

9383
config("setLocalizedClientName", $("ios-sim-portable"));
9484

@@ -167,4 +157,13 @@ export class iPhoneSimulator implements IiPhoneSimulator {
167157
private getClassByName(className: string): any {
168158
return $.classDefinition.getClassByName(className);
169159
}
160+
161+
private static logSessionInfo(error: any, successfulMessage: string, errorMessage: string): void {
162+
if(error) {
163+
console.log(util.format("%s %s", errorMessage, error));
164+
process.exit(1);
165+
} else {
166+
console.log(successfulMessage);
167+
}
168+
}
170169
}

lib/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var yargs = require("yargs");
66
var knownOptions: any = {
77
"sdk": String,
88
"family": String,
9+
"device": String,
910
"stdout": String,
1011
"stderr": String,
1112
"env": String,

lib/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function isEmptyDictionary(dictionary: IDictionary<any>): boolean {
2+
return _.keys(dictionary).length === 0;
3+
}
4+
5+
export function stringify(arr: string[], delimiter?: string): string {
6+
delimiter = delimiter || ", ";
7+
return arr.join(delimiter);
8+
}

0 commit comments

Comments
 (0)