Skip to content

Commit 7b828dc

Browse files
Fatme HavaluovaFatme Havaluova
authored andcommitted
Device-types command
1 parent 9d8c691 commit 7b828dc

File tree

6 files changed

+66
-32
lines changed

6 files changed

+66
-32
lines changed

lib/command-executor.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import options = require("./options");
1111
export class CommandExecutor implements ICommandExecutor {
1212

1313
public execute(): IFuture<void> {
14-
return (() => {
15-
var commandName = this.getCommandName();
16-
var commandArguments = this.getCommandArguments();
14+
var commandName = this.getCommandName();
15+
var commandArguments = this.getCommandArguments();
1716

18-
this.executeCore(commandName, commandArguments).wait();
19-
}).future<void>()();
17+
return this.executeCore(commandName, commandArguments);
2018
}
2119

2220
private executeCore(commandName: string, commandArguments: string[]): IFuture<void> {

lib/commands/device-types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///<reference path=".././.d.ts"/>
2+
"use strict";
3+
import iphoneSimulatorLibPath = require("./../iphone-simulator");
4+
5+
export class Command implements ICommand {
6+
public execute(args: string[]): IFuture<void> {
7+
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
8+
return iphoneSimulator.printDeviceTypes();
9+
}
10+
}

lib/declarations.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
interface IiPhoneSimulator {
55
run(appName: string): IFuture<void>;
6+
printDeviceTypes(): void;
67
}
78

89
interface ICommand {
@@ -26,4 +27,9 @@ interface IDictionary<T> {
2627
interface ISimulator {
2728
validDeviceIdentifiers: string[];
2829
setSimulatedDevice(config: any): void;
30+
}
31+
32+
interface IExecuteOptions {
33+
canRunMainLoop: boolean;
34+
appPath?: string;
2935
}

lib/iphone-simulator-xcode-6.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var $ = require("NodObjC");
99
export class XCode6Simulator implements ISimulator {
1010

1111
private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType";
12-
private static DEFAULT_DEVICE_IDENTIFIER = "Resizable-iPad";
12+
private static DEFAULT_DEVICE_IDENTIFIER = "iPhone-4s";
1313

1414
private static allowedDeviceIdentifiers = [
1515
"iPhone-4s",
@@ -44,7 +44,7 @@ export class XCode6Simulator implements ISimulator {
4444
}
4545

4646
private getAvailableDevices(): IDictionary<IDevice> {
47-
if(utils.isEmptyDictionary(this.availableDevices)) {
47+
if(_.isEmpty(this.availableDevices)) {
4848
var deviceSet = $.classDefinition.getClassByName("SimDeviceSet")("defaultSet");
4949
var devices = deviceSet("availableDevices");
5050
var count = devices("count");
@@ -68,10 +68,10 @@ export class XCode6Simulator implements ISimulator {
6868
private getDeviceByIdentifier(deviceIdentifier: string): any {
6969
var fullDeviceIdentifier = util.format("%s.%s", XCode6Simulator.DEVICE_IDENTIFIER_PREFIX, deviceIdentifier);
7070
var availableDevices = this.getAvailableDevices();
71-
if(!utils.isEmptyDictionary(availableDevices)) {
72-
var device = availableDevices[fullDeviceIdentifier];
73-
if(device) {
74-
return device.device;
71+
if(!_.isEmpty(availableDevices)) {
72+
var selectedDevice = availableDevices[fullDeviceIdentifier];
73+
if(selectedDevice) {
74+
return selectedDevice.device;
7575
}
7676
}
7777

lib/iphone-simulator.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,25 @@ export class iPhoneSimulator implements IiPhoneSimulator {
2727
private static SIMULATOR_FRAMEWORK_RELATIVE_PATH = "../SharedFrameworks/DVTiPhoneSimulatorRemoteClient.framework";
2828

2929
public run(appPath: string): IFuture<void> {
30-
return (() => {
31-
if(!fs.existsSync(appPath)) {
32-
errors.fail("Path does not exist ", appPath);
33-
}
30+
if(!fs.existsSync(appPath)) {
31+
errors.fail("Path does not exist ", appPath);
32+
}
33+
34+
return this.execute(this.launch, { canRunMainLoop: true, appPath: appPath});
35+
}
36+
37+
public printDeviceTypes(): IFuture<void> {
38+
39+
var action = () => {
40+
var simulator = this.createSimulator();
41+
_.each(simulator.validDeviceIdentifiers, (identifier: any) => console.log(identifier));
42+
}
3443

44+
return this.execute(action, { canRunMainLoop: false });
45+
}
46+
47+
private execute(action: (appPath?: string) => any, opts: IExecuteOptions): IFuture<void> {
48+
return (() => {
3549
$.importFramework(iPhoneSimulator.FOUNDATION_FRAMEWORK_NAME);
3650
$.importFramework(iPhoneSimulator.APPKIT_FRAMEWORK_NAME);
3751

@@ -43,17 +57,18 @@ export class iPhoneSimulator implements IiPhoneSimulator {
4357
}
4458

4559
this.loadFrameworks(developerDirectoryPath);
46-
this.launch(developerDirectoryPath, appPath);
4760

48-
$.NSRunLoop("mainRunLoop")("run");
61+
action.apply(this, [opts.appPath]);
62+
63+
if(opts.canRunMainLoop) {
64+
$.NSRunLoop("mainRunLoop")("run");
65+
}
4966

5067
pool("release");
5168
}).future<void>()();
5269
}
5370

54-
private launch(developerDirectoryPath: string, appPath: string): void {
55-
this.loadFrameworks(developerDirectoryPath);
56-
71+
private launch(appPath: string): void {
5772
var sessionDelegate = $.NSObject.extend("DTiPhoneSimulatorSessionDelegate");
5873
sessionDelegate.addMethod("session:didEndWithError:", "v@:@@", function(self: any, sel: any, sess: any, error: any) {
5974
iPhoneSimulator.logSessionInfo(error, "Session ended without errors.", "Session ended with error ");
@@ -71,13 +86,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
7186
var sdkRoot = options.sdkRoot ? $(options.sdkRoot) : this.getClassByName("DTiPhoneSimulatorSystemRoot")("defaultRoot");
7287
config("setSimulatedSystemRoot", sdkRoot);
7388

74-
var simulator: ISimulator;
75-
if(_.contains(config.methods(), "setDevice:")) {
76-
simulator = new xcode6SimulatorLib.XCode6Simulator();
77-
} else {
78-
simulator = new xcode5SimulatorLib.XCode5Simulator();
79-
}
80-
89+
var simulator = this.createSimulator(config);
8190
if(options.device) {
8291
var validDeviceIdentifiers = simulator.validDeviceIdentifiers;
8392
if(!_.contains(validDeviceIdentifiers, options.device)) {
@@ -168,8 +177,23 @@ export class iPhoneSimulator implements IiPhoneSimulator {
168177
if(error) {
169178
console.log(util.format("%s %s", errorMessage, error));
170179
process.exit(1);
180+
}
181+
182+
console.log(successfulMessage);
183+
}
184+
185+
private createSimulator(config?: any): ISimulator {
186+
if(!config) {
187+
config = this.getClassByName("DTiPhoneSimulatorSessionConfig")("alloc")("init")("autorelease");
188+
}
189+
190+
var simulator: ISimulator;
191+
if(_.contains(config.methods(), "setDevice:")) {
192+
simulator = new xcode6SimulatorLib.XCode6Simulator();
171193
} else {
172-
console.log(successfulMessage);
194+
simulator = new xcode5SimulatorLib.XCode5Simulator();
173195
}
196+
197+
return simulator;
174198
}
175199
}

lib/utils.ts

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

0 commit comments

Comments
 (0)