Skip to content

Commit 02aa408

Browse files
author
Fatme
authored
Merge pull request #103 from telerik/fatme/ios-debugger-port
Get debugger port from device logs
2 parents b0bfc2a + b728f68 commit 02aa408

10 files changed

+66
-80
lines changed

lib/commands/launch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import iphoneSimulatorLibPath = require("./../iphone-simulator");
2+
import options = require("../options");
23

34
export class Command implements ICommand {
4-
public execute(args: string[]): void {
5+
public execute(args: string[]): string {
56
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
6-
return iphoneSimulator.run(args[0], args[1]);
7+
return iphoneSimulator.run(args[0], args[1], options);
78
}
89
}

lib/commands/notify-post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import iphoneSimulatorLibPath = require("./../iphone-simulator");
33
export class Command implements ICommand {
44
public execute(args: string[]): void {
55
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
6-
return iphoneSimulator.sendNotification(args[0]);
6+
return iphoneSimulator.sendNotification(args[0], args[1]);
77
}
88
}

lib/declarations.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"use strict";
33

44
interface IiPhoneSimulator {
5-
run(applicationPath: string, applicationIdentifier: string): void;
5+
run(applicationPath: string, applicationIdentifier: string, options: IOptions): string;
66
printDeviceTypes(): void;
77
printSDKS(): void;
8-
sendNotification(notification: string): void;
8+
sendNotification(notification: string, deviceId: string): void;
99
createSimulator(): ISimulator;
1010
}
1111

@@ -27,7 +27,7 @@ interface IDevice {
2727
}
2828

2929
interface ISimctl {
30-
launch(deviceId: string, applicationIdentifier: string): string;
30+
launch(deviceId: string, applicationIdentifier: string, options: IOptions): string;
3131
boot(deviceId: string): void;
3232
terminate(deviceId: string, appIdentifier: string): string;
3333
install(deviceId: string, applicationPath: string): void;
@@ -45,17 +45,16 @@ interface IDictionary<T> {
4545
interface ISimulator extends INameGetter {
4646
getDevices(): IDevice[];
4747
getSdks(): ISdk[];
48-
run(applicationPath: string, applicationIdentifier: string): void;
49-
sendNotification(notification: string): void;
48+
run(applicationPath: string, applicationIdentifier: string, options: IOptions): string;
49+
sendNotification(notification: string, deviceId: string): void;
5050
getApplicationPath(deviceId: string, applicationIdentifier: string): string;
5151
getInstalledApplications(deviceId: string): IApplication[];
5252
installApplication(deviceId: string, applicationPath: string): void;
5353
uninstallApplication(deviceId: string, appIdentifier: string): void;
54-
startApplication(deviceId: string, appIdentifier: string): string;
54+
startApplication(deviceId: string, appIdentifier: string, options: IOptions): string;
5555
stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): string;
56-
printDeviceLog(deviceId: string, launchResult?: string): any;
5756
getDeviceLogProcess(deviceId: string): any;
58-
startSimulator(): void;
57+
startSimulator(options: IOptions, device?: IDevice): void;
5958
}
6059

6160
interface INameGetter {
@@ -85,3 +84,12 @@ interface IXcodeVersionData {
8584
minor: string;
8685
build: string;
8786
}
87+
88+
interface IOptions {
89+
skipInstall?: boolean;
90+
waitForDebugger?: boolean;
91+
args?: any;
92+
sdkVersion?: string;
93+
sdk?: string;
94+
device?: string;
95+
}

lib/ios-sim.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,36 @@ Object.defineProperty(publicApi, "getInstalledApplications", {
107107
}
108108
});
109109

110+
Object.defineProperty(publicApi, "launchApplication", {
111+
get: () => {
112+
return (...args: any[]) => {
113+
const libraryPath = require("./iphone-simulator");
114+
const obj = new libraryPath.iPhoneSimulator();
115+
return obj.run.apply(obj, args);
116+
}
117+
}
118+
});
119+
120+
Object.defineProperty(publicApi, "printDeviceTypes", {
121+
get: () => {
122+
return (...args: any[]) => {
123+
const libraryPath = require("./iphone-simulator");
124+
const obj = new libraryPath.iPhoneSimulator();
125+
return obj.printDeviceTypes.apply(obj, args);
126+
}
127+
}
128+
});
129+
110130
["installApplication",
111131
"uninstallApplication",
112132
"startApplication",
113133
"stopApplication",
114-
"printDeviceLog",
134+
"run",
115135
"getDeviceLogProcess",
116136
"startSimulator",
117137
"getSimulatorName",
118-
"getDevices"].forEach(methodName => {
138+
"getDevices",
139+
"sendNotification"].forEach(methodName => {
119140
Object.defineProperty(publicApi, methodName, {
120141
get: () => {
121142
return (...args: any[]) => {

lib/iphone-simulator-name-getter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
///<reference path="./.d.ts"/>
22
"use strict";
3-
import * as options from "./options";
43

54
export abstract class IPhoneSimulatorNameGetter implements INameGetter {
65
private _simulatorName: string;
@@ -9,7 +8,7 @@ export abstract class IPhoneSimulatorNameGetter implements INameGetter {
98

109
public getSimulatorName(deviceName?: string): string {
1110
if (!this._simulatorName) {
12-
this._simulatorName = options.device || deviceName || this.defaultDeviceIdentifier;
11+
this._simulatorName = deviceName || this.defaultDeviceIdentifier;
1312
}
1413

1514
return this._simulatorName;

lib/iphone-simulator-xcode-simctl.ts

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as child_process from "child_process";
66
import errors = require("./errors");
77

88
import common = require("./iphone-simulator-common");
9-
import options = require("./options");
109
import path = require("path");
1110
import { Simctl } from "./simctl";
1211
import util = require("util");
@@ -45,31 +44,28 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
4544
});
4645
}
4746

48-
public run(applicationPath: string, applicationIdentifier: string): void {
49-
let device = this.getDeviceToRun();
47+
public run(applicationPath: string, applicationIdentifier: string, options: IOptions): string {
48+
let device = this.getDeviceToRun(options);
5049
let currentBootedDevice = _.find(this.getDevices(), device => this.isDeviceBooted(device));
5150
if (currentBootedDevice && (currentBootedDevice.name.toLowerCase() !== device.name.toLowerCase() || currentBootedDevice.runtimeVersion !== device.runtimeVersion)) {
5251
this.killSimulator();
5352
}
5453

55-
this.startSimulator(device);
54+
this.startSimulator(options, device);
5655
if (!options.skipInstall) {
5756
this.simctl.install(device.id, applicationPath);
5857
}
59-
let launchResult = this.simctl.launch(device.id, applicationIdentifier);
6058

61-
if (options.logging) {
62-
this.printDeviceLog(device.id, launchResult);
63-
}
59+
return this.simctl.launch(device.id, applicationIdentifier, options);
6460
}
6561

66-
public sendNotification(notification: string): void {
67-
let device = this.getBootedDevice();
62+
public sendNotification(notification: string, deviceId: string): void {
63+
let device = this.getDeviceFromIdentifier(deviceId);
6864
if (!device) {
6965
errors.fail("Could not find device.");
7066
}
7167

72-
this.simctl.notifyPost("booted", notification);
68+
this.simctl.notifyPost(deviceId, notification);
7369
}
7470

7571
public getApplicationPath(deviceId: string, applicationIdentifier: string): string {
@@ -88,10 +84,10 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
8884
return this.simctl.uninstall(deviceId, appIdentifier, { skipError: true });
8985
}
9086

91-
public startApplication(deviceId: string, appIdentifier: string): string {
87+
public startApplication(deviceId: string, appIdentifier: string, options: IOptions): string {
9288
// simctl launch command does not launch the process immediately and we have to wait a little bit,
9389
// just to ensure all related processes and services are alive.
94-
const launchResult = this.simctl.launch(deviceId, appIdentifier);
90+
const launchResult = this.simctl.launch(deviceId, appIdentifier, options);
9591
utils.sleep(0.5);
9692
return launchResult;
9793
}
@@ -124,40 +120,6 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
124120
}
125121
}
126122

127-
public printDeviceLog(deviceId: string, launchResult?: string): child_process.ChildProcess {
128-
let pid = "";
129-
let deviceLogChildProcess;
130-
131-
if (launchResult) {
132-
pid = launchResult.split(":")[1].trim();
133-
}
134-
135-
if (!this.isDeviceLogOperationStarted) {
136-
deviceLogChildProcess = this.getDeviceLogProcess(deviceId);
137-
if (deviceLogChildProcess.stdout) {
138-
deviceLogChildProcess.stdout.on("data", this.logDataHandler.bind(this, pid));
139-
}
140-
141-
if (deviceLogChildProcess.stderr) {
142-
deviceLogChildProcess.stderr.on("data", this.logDataHandler.bind(this, pid));
143-
}
144-
}
145-
146-
return deviceLogChildProcess;
147-
}
148-
149-
private logDataHandler(pid: string, logData: NodeBuffer): void {
150-
const dataAsString = logData.toString();
151-
152-
if (pid) {
153-
if (dataAsString.indexOf(`[${pid}]`) > -1) {
154-
process.stdout.write(dataAsString);
155-
}
156-
} else {
157-
process.stdout.write(dataAsString);
158-
}
159-
}
160-
161123
public getDeviceLogProcess(deviceId: string, predicate?: string): child_process.ChildProcess {
162124
if (!this.isDeviceLogOperationStarted) {
163125
const device = this.getDeviceFromIdentifier(deviceId);
@@ -177,7 +139,7 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
177139
return this.deviceLogChildProcess;
178140
}
179141

180-
private getDeviceToRun(device?: any): IDevice {
142+
private getDeviceToRun(options: IOptions, device?: any): IDevice {
181143
let devices = _.sortBy(this.simctl.getDevices(), (device) => device.runtimeVersion),
182144
sdkVersion = options.sdkVersion || options.sdk,
183145
deviceIdOrName = options.device;
@@ -233,14 +195,16 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
233195
return _.filter(devices, device => this.isDeviceBooted(device));
234196
}
235197

236-
public startSimulator(device?: IDevice): void {
198+
public startSimulator(options: IOptions, device?: IDevice): void {
199+
device = device || this.getDeviceToRun(options);
200+
237201
// In case the id is undefined, skip verification - we'll start default simulator.
238202
if (device && device.id) {
239203
this.verifyDevice(device);
240204
}
241205

242206
if (!device || !device.runtimeVersion || !device.fullId) {
243-
device = this.getDeviceToRun(device);
207+
device = this.getDeviceToRun(options, device);
244208
}
245209

246210
if (!this.isDeviceBooted(device)) {
@@ -250,7 +214,7 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
250214
if (isSimulatorAppRunning) {
251215
// In case user closes simulator window but simulator app is still alive
252216
if (!haveBootedDevices) {
253-
device = this.getDeviceToRun();
217+
device = this.getDeviceToRun(options);
254218
}
255219
this.simctl.boot(device.id);
256220
} else {

lib/iphone-simulator.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path = require("path");
55
import util = require("util");
66

77
import errors = require("./errors");
8-
import options = require("./options");
98
import xcode = require("./xcode");
109

1110
import { XCodeSimctlSimulator } from "./iphone-simulator-xcode-simctl";
@@ -19,7 +18,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
1918
this.simulator = this.createSimulator();
2019
}
2120

22-
public run(applicationPath: string, applicationIdentifier: string): void {
21+
public run(applicationPath: string, applicationIdentifier: string, options: IOptions): string {
2322
if (!fs.existsSync(applicationPath)) {
2423
errors.fail("Path does not exist ", applicationPath);
2524
}
@@ -39,7 +38,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
3938
}
4039
}
4140

42-
return this.simulator.run(applicationPath, applicationIdentifier);
41+
return this.simulator.run(applicationPath, applicationIdentifier, options);
4342
}
4443

4544
public printDeviceTypes(): void {
@@ -58,12 +57,12 @@ export class iPhoneSimulator implements IiPhoneSimulator {
5857
});
5958
}
6059

61-
public sendNotification(notification: string): void {
60+
public sendNotification(notification: string, deviceId: string): void {
6261
if (!notification) {
6362
errors.fail("Notification required.");
6463
}
6564

66-
return this.simulator.sendNotification(notification);
65+
return this.simulator.sendNotification(notification, deviceId);
6766
}
6867

6968
public createSimulator(): ISimulator {

lib/options.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ class OptionType {
1111

1212
var knownOptions: any = {
1313
"debug": { type: OptionType.Boolean },
14-
"exit": { type: OptionType.Boolean },
1514
"device": { type: OptionType.String },
16-
"stdout": { type: OptionType.String },
17-
"stderr": { type: OptionType.String },
18-
"env": { type: OptionType.String },
1915
"args": { type: OptionType.String },
20-
"timeout": { type: OptionType.String },
2116
"help": { type: OptionType.Boolean },
22-
"logging": { type: OptionType.Boolean },
2317
"waitForDebugger": { type: OptionType.Boolean },
2418
"sdkVersion": { type: OptionType.String }, // Obsolete, use sdk instead.
2519
"sdk": { type: OptionType.String },

lib/simctl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import childProcess = require("./child-process");
22
import * as child_process from "child_process";
33
import errors = require("./errors");
4-
import options = require("./options");
54
import * as _ from "lodash";
65

76
export class Simctl implements ISimctl {
87

9-
public launch(deviceId: string, appIdentifier: string): string {
8+
public launch(deviceId: string, appIdentifier: string, options: IOptions): string {
9+
options = options || {};
1010
let args: string[] = [];
1111
if (options.waitForDebugger) {
1212
args.push("-w");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ios-sim-portable",
3-
"version": "3.3.3",
3+
"version": "3.4.0",
44
"description": "",
55
"main": "./lib/ios-sim.js",
66
"scripts": {

0 commit comments

Comments
 (0)