Skip to content

Commit d32c238

Browse files
teobugslayerFatme Havaluova
authored andcommitted
Update code to address PR comments, ES2015 and TS 1.6
1 parent 72e88c8 commit d32c238

File tree

3 files changed

+135
-122
lines changed

3 files changed

+135
-122
lines changed

lib/child-process.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
///<reference path="./.d.ts"/>
22
"use strict";
33

4-
import child_process = require("child_process");
5-
import errors = require("./errors");
4+
import * as child_process from "child_process";
5+
import * as errors from "./errors";
66
import Future = require("fibers/future");
7-
import util = require("util");
7+
import * as util from "util";
88

99
export function exec(command: string): IFuture<any> {
1010
var future = new Future<any>();
1111

1212
child_process.exec(command, (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) => {
13-
//console.log(util.format("Executing: %s", command));
14-
1513
if(error) {
16-
errors.fail(util.format("Error %s while executing %s.", error.message, command));
14+
errors.fail(`Error ${error.message} while executing ${command}.`);
1715
} else {
1816
future.return(stdout ? stdout.toString() : "");
1917
}
@@ -42,7 +40,7 @@ export function spawn(command: string, args: string[]): IFuture<string> {
4240
}
4341

4442
childProcess.on("close", (arg: any) => {
45-
var exitCode = typeof arg == 'number' ? arg : arg && arg.code;
43+
var exitCode = typeof arg === 'number' ? arg : arg && arg.code;
4644
if(exitCode === 0) {
4745
future.return(capturedOut ? capturedOut.trim() : null);
4846
} else {

lib/iphone-interop-simulator-base.ts

Lines changed: 99 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
///<reference path="./.d.ts"/>
22
"use strict";
33

4-
import child_process = require("child_process");
5-
import errors = require("./errors");
6-
import fs = require("fs");
4+
import * as child_process from "child_process";
5+
import * as errors from "./errors";
6+
import * as fs from "fs";
77
import Future = require("fibers/future");
8-
import options = require("./options");
9-
import os = require("os");
10-
import path = require("path");
11-
import util = require("util");
12-
import utils = require("./utils");
13-
14-
var $ = require("NodObjC");
8+
import * as options from "./options";
9+
import * as os from "os";
10+
import * as path from "path";
11+
import * as util from "util";
12+
import * as utils from "./utils";
13+
let $ = require("nodobjc");
1514

1615
export class IPhoneInteropSimulatorBase {
1716
constructor(private simulator: IInteropSimulator) { }
@@ -30,9 +29,9 @@ export class IPhoneInteropSimulatorBase {
3029
public run(appPath: string): IFuture<void> {
3130
return this.execute(this.launch, { canRunMainLoop: true, appPath: appPath });
3231
}
33-
34-
private launch(appPath: string): void {
35-
var sessionDelegate = $.NSObject.extend("DTiPhoneSimulatorSessionDelegate");
32+
33+
private setupSessionDelegate(appPath: string): any {
34+
let sessionDelegate = $.NSObject.extend("DTiPhoneSimulatorSessionDelegate");
3635
sessionDelegate.addMethod("session:didEndWithError:", "v@:@@", function(self: any, sel: any, sess: any, error: any) {
3736
IPhoneInteropSimulatorBase.logSessionInfo(error, "Session ended without errors.", "Session ended with error ");
3837
process.exit(0);
@@ -41,73 +40,87 @@ export class IPhoneInteropSimulatorBase {
4140
IPhoneInteropSimulatorBase.logSessionInfo(error, "Session started without errors.", "Session started with error ");
4241

4342
console.log(`${appPath}: ${session("simulatedApplicationPID")}`);
44-
if(options.exit) {
43+
if (options.exit) {
4544
process.exit(0);
4645
}
4746
});
4847
sessionDelegate.register();
48+
49+
return sessionDelegate;
50+
}
51+
52+
private getTimeout(): number {
53+
let timeoutParam = IPhoneInteropSimulatorBase.DEFAULT_TIMEOUT_IN_SECONDS;
54+
if (options.timeout || options.timeout === 0) {
55+
let parsedValue = parseInt(options.timeout);
56+
if(!isNaN(parsedValue) && parsedValue > 0) {
57+
timeoutParam = parsedValue;
58+
}
59+
else {
60+
console.log(`Specify the timeout in number of seconds to wait. It should be greater than 0. Default value ${IPhoneInteropSimulatorBase.DEFAULT_TIMEOUT_IN_SECONDS} seconds will be used.`);
61+
}
62+
}
63+
return timeoutParam;
64+
}
65+
66+
private validateDevice() {
67+
if (options.device) {
68+
let devices = this.simulator.getDevices().wait();
69+
let validDeviceIdentifiers = _.map(devices, device => device.id);
70+
if(!_.contains(validDeviceIdentifiers, options.device)) {
71+
errors.fail("Invalid device identifier %s. Valid device identifiers are %s.", options.device, utils.stringify(validDeviceIdentifiers));
72+
}
73+
}
74+
}
4975

50-
var appSpec = this.getClassByName("DTiPhoneSimulatorApplicationSpecifier")("specifierWithApplicationPath", $(appPath));
51-
var config = this.getClassByName("DTiPhoneSimulatorSessionConfig")("alloc")("init")("autorelease");
76+
private launch(appPath: string): void {
77+
let sessionDelegate = this.setupSessionDelegate(appPath);
78+
79+
let appSpec = this.getClassByName("DTiPhoneSimulatorApplicationSpecifier")("specifierWithApplicationPath", $(appPath));
80+
let config = this.getClassByName("DTiPhoneSimulatorSessionConfig")("alloc")("init")("autorelease");
5281
config("setApplicationToSimulateOnStart", appSpec);
5382
config("setSimulatedApplicationShouldWaitForDebugger", options.waitForDebugger);
5483

55-
var sdkRoot = options.sdkVersion ? $(this.getSdkRootPathByVersion(options.sdkVersion)) : this.getClassByName("DTiPhoneSimulatorSystemRoot")("defaultRoot");
84+
let sdkRoot = options.sdkVersion ? $(this.getSdkRootPathByVersion(options.sdkVersion)) : this.getClassByName("DTiPhoneSimulatorSystemRoot")("defaultRoot");
5685
config("setSimulatedSystemRoot", sdkRoot);
5786

58-
var simulator = this.simulator;
59-
if(options.device) {
60-
let devices = simulator.getDevices().wait();
61-
var validDeviceIdentifiers = _.map(devices, device => device.id);
62-
if(!_.contains(validDeviceIdentifiers, options.device)) {
63-
errors.fail("Invalid device identifier %s. Valid device identifiers are %s.", options.device, utils.stringify(validDeviceIdentifiers));
64-
}
65-
}
66-
simulator.setSimulatedDevice(config);
87+
this.validateDevice();
88+
this.simulator.setSimulatedDevice(config);
6789

68-
if(options.logging) {
69-
var logPath = this.createLogPipe(appPath).wait();
90+
if (options.logging) {
91+
let logPath = this.createLogPipe(appPath).wait();
7092
fs.createReadStream(logPath, { encoding: "utf8" }).pipe(process.stdout);
7193
config("setSimulatedApplicationStdErrPath", $(logPath));
7294
config("setSimulatedApplicationStdOutPath", $(logPath));
7395
} else {
74-
if(options.stderr) {
96+
if (options.stderr) {
7597
config("setSimulatedApplicationStdErrPath", $(options.stderr));
7698
}
77-
if(options.stdout) {
99+
if (options.stdout) {
78100
config("setSimulatedApplicationStdOutPath", $(options.stdout));
79101
}
80102
}
81103

82104
if (options.args) {
83-
var args = options.args.trim().split(/\s+/);
84-
var nsArgs = $.NSMutableArray("array");
105+
let args = options.args.trim().split(/\s+/);
106+
let nsArgs = $.NSMutableArray("array");
85107
args.forEach((x: string) => nsArgs("addObject", $(x)));
86108
config("setSimulatedApplicationLaunchArgs", nsArgs);
87109
}
88110

89111
config("setLocalizedClientName", $("ios-sim-portable"));
90112

91-
var sessionError: any = new Buffer("");
92-
var timeoutParam = IPhoneInteropSimulatorBase.DEFAULT_TIMEOUT_IN_SECONDS;
93-
if (options.timeout || options.timeout === 0) {
94-
var parsedValue = parseInt(options.timeout);
95-
if(!isNaN(parsedValue) && parsedValue > 0) {
96-
timeoutParam = parsedValue;
97-
}
98-
else {
99-
console.log(util.format("Specify the timeout in number of seconds to wait. It should be greater than 0. Default value %s seconds will be used.", IPhoneInteropSimulatorBase.DEFAULT_TIMEOUT_IN_SECONDS.toString()));
100-
}
101-
}
113+
let sessionError: any = new Buffer("");
114+
let timeoutParam = this.getTimeout();
102115

103-
var time = $.NSNumber("numberWithDouble", timeoutParam);
104-
var timeout = time("doubleValue");
116+
let time = $.NSNumber("numberWithDouble", timeoutParam);
117+
let timeout = time("doubleValue");
105118

106-
var session = this.getClassByName("DTiPhoneSimulatorSession")("alloc")("init")("autorelease");
107-
var delegate = sessionDelegate("alloc")("init");
119+
let session = this.getClassByName("DTiPhoneSimulatorSession")("alloc")("init")("autorelease");
120+
let delegate = sessionDelegate("alloc")("init");
108121
session("setDelegate", delegate);
109122

110-
if(!session("requestStartWithConfig", config, "timeout", timeout, "error", sessionError)) {
123+
if (!session("requestStartWithConfig", config, "timeout", timeout, "error", sessionError)) {
111124
errors.fail("Could not start simulator session ", sessionError);
112125
}
113126
}
@@ -116,19 +129,22 @@ export class IPhoneInteropSimulatorBase {
116129
$.importFramework(IPhoneInteropSimulatorBase.FOUNDATION_FRAMEWORK_NAME);
117130
$.importFramework(IPhoneInteropSimulatorBase.APPKIT_FRAMEWORK_NAME);
118131

119-
var pool = $.NSAutoreleasePool("alloc")("init");
120-
121-
var developerDirectoryPath = this.findDeveloperDirectory().wait();
132+
let developerDirectoryPath = this.findDeveloperDirectory().wait();
122133
if(!developerDirectoryPath) {
123134
errors.fail("Unable to find developer directory");
124135
}
125136

126137
this.loadFrameworks(developerDirectoryPath);
127138

128139
let result = action.apply(this, [opts.appPath]);
129-
130-
var future = new Future<any>();
131-
if(opts.canRunMainLoop) {
140+
return this.runCFLoop(opts.canRunMainLoop, result);
141+
}
142+
143+
private runCFLoop(canRunMainLoop: boolean, result: any): IFuture<any> {
144+
let pool = $.NSAutoreleasePool("alloc")("init");
145+
let future = new Future<any>();
146+
147+
if (canRunMainLoop) {
132148
// Keeps the Node loop running
133149
(function runLoop() {
134150
if($.CFRunLoopRunInMode($.kCFRunLoopDefaultMode, 0.1, false)) {
@@ -141,6 +157,7 @@ export class IPhoneInteropSimulatorBase {
141157
} else {
142158
future.return(result);
143159
}
160+
144161
return future;
145162
}
146163

@@ -152,48 +169,48 @@ export class IPhoneInteropSimulatorBase {
152169
this.loadFramework(path.join(developerDirectoryPath, IPhoneInteropSimulatorBase.CORE_SIMULATOR_RELATIVE_PATH));
153170
}
154171

155-
var platformsError: string = null;
156-
var dvtPlatformClass = this.getClassByName("DVTPlatform");
172+
let platformsError: string = null;
173+
let dvtPlatformClass = this.getClassByName("DVTPlatform");
157174
if(!dvtPlatformClass("loadAllPlatformsReturningError", platformsError)) {
158175
errors.fail("Unable to loadAllPlatformsReturningError ", platformsError);
159176
}
160177

161-
var simulatorFrameworkPath = path.join(developerDirectoryPath, IPhoneInteropSimulatorBase.SIMULATOR_FRAMEWORK_RELATIVE_PATH_LEGACY);
178+
let simulatorFrameworkPath = path.join(developerDirectoryPath, IPhoneInteropSimulatorBase.SIMULATOR_FRAMEWORK_RELATIVE_PATH_LEGACY);
162179
if(!fs.existsSync(simulatorFrameworkPath)) {
163180
simulatorFrameworkPath = path.join(developerDirectoryPath, IPhoneInteropSimulatorBase.SIMULATOR_FRAMEWORK_RELATIVE_PATH);
164181
}
165182
this.loadFramework(simulatorFrameworkPath);
166183
}
167184

168185
private loadFramework(frameworkPath: string) {
169-
var bundle = $.NSBundle("bundleWithPath", $(frameworkPath));
186+
let bundle = $.NSBundle("bundleWithPath", $(frameworkPath));
170187
if(!bundle("load")) {
171188
errors.fail("Unable to load ", frameworkPath);
172189
}
173190
}
174191

175192
private findDeveloperDirectory(): IFuture<string> {
176-
var future = new Future<string>();
177-
var capturedOut = "";
178-
var capturedErr = "";
193+
let future = new Future<string>();
194+
let capturedOut = "";
195+
let capturedErr = "";
179196

180-
var childProcess = child_process.spawn("xcode-select", ["-print-path"]);
197+
let childProcess = child_process.spawn("xcode-select", ["-print-path"]);
181198

182-
if(childProcess.stdout) {
199+
if (childProcess.stdout) {
183200
childProcess.stdout.on("data", (data: string) => {
184201
capturedOut += data;
185202
});
186203
}
187204

188-
if(childProcess.stderr) {
205+
if (childProcess.stderr) {
189206
childProcess.stderr.on("data", (data: string) => {
190207
capturedErr += data;
191208
});
192209
}
193210

194211
childProcess.on("close", (arg: any) => {
195-
var exitCode = typeof arg == 'number' ? arg : arg && arg.code;
196-
if(exitCode === 0) {
212+
let exitCode = typeof arg == 'number' ? arg : arg && arg.code;
213+
if (exitCode === 0) {
197214
future.return(capturedOut ? capturedOut.trim() : null);
198215
} else {
199216
future.throw(util.format("Command xcode-select -print-path failed with exit code %s. Error output: \n %s", exitCode, capturedErr));
@@ -217,27 +234,27 @@ export class IPhoneInteropSimulatorBase {
217234
}
218235

219236
private getSdkRootPathByVersion(version: string): string {
220-
var sdks = this.getInstalledSdks();
221-
var sdk = _.find(sdks, sdk => sdk.version === version);
222-
if(!sdk) {
237+
let sdks = this.getInstalledSdks();
238+
let sdk = _.find(sdks, sdk => sdk.version === version);
239+
if (!sdk) {
223240
errors.fail("Unable to find installed sdk with version %s. Verify that you have specified correct version and the sdk with that version is installed.", version);
224241
}
225242

226243
return sdk.rootPath;
227244
}
228245

229246
private getInstalledSdks(): ISdk[] {
230-
var systemRootClass = this.getClassByName("DTiPhoneSimulatorSystemRoot");
231-
var roots = systemRootClass("knownRoots");
232-
var count = roots("count");
247+
let systemRootClass = this.getClassByName("DTiPhoneSimulatorSystemRoot");
248+
let roots = systemRootClass("knownRoots");
249+
let count = roots("count");
233250

234-
var sdks: ISdk[] = [];
235-
for(var index=0; index < count; index++) {
236-
var root = roots("objectAtIndex", index);
251+
let sdks: ISdk[] = [];
252+
for (let index=0; index < count; index++) {
253+
let root = roots("objectAtIndex", index);
237254

238-
var displayName = root("sdkDisplayName").toString();
239-
var version = root("sdkVersion").toString();
240-
var rootPath = root("sdkRootPath").toString();
255+
let displayName = root("sdkDisplayName").toString();
256+
let version = root("sdkVersion").toString();
257+
let rootPath = root("sdkRootPath").toString();
241258

242259
sdks.push(new Sdk(displayName, version, rootPath));
243260
}
@@ -246,10 +263,10 @@ export class IPhoneInteropSimulatorBase {
246263
}
247264

248265
private createLogPipe(appPath: string): IFuture<string> {
249-
var future = new Future<string>();
250-
var logPath = path.join(path.dirname(appPath), "." + path.basename(appPath, ".app") + ".log");
266+
let future = new Future<string>();
267+
let logPath = path.join(path.dirname(appPath), "." + path.basename(appPath, ".app") + ".log");
251268

252-
var command = util.format("rm -f \"%s\" && mkfifo \"%s\"", logPath, logPath);
269+
let command = util.format("rm -f \"%s\" && mkfifo \"%s\"", logPath, logPath);
253270
child_process.exec(command, (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) => {
254271
if(error) {
255272
future.throw(error);
@@ -272,4 +289,4 @@ class Sdk implements ISdk {
272289
util.format(" Version: %s", this.version),
273290
util.format(" Root path: %s", this.rootPath)].join(os.EOL);
274291
}
275-
}
292+
}

0 commit comments

Comments
 (0)