Skip to content

Commit f358b1d

Browse files
author
Fatme
authored
Merge pull request #106 from telerik/fatme/async-api
Make ios-sim API async
2 parents ad24748 + 1417480 commit f358b1d

File tree

9 files changed

+95
-91
lines changed

9 files changed

+95
-91
lines changed

lib/commands/device-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import iphoneSimulatorLibPath = require("./../iphone-simulator");
44

55
export class Command implements ICommand {
6-
public execute(args: string[]): void {
6+
public execute(args: string[]): Promise<void> {
77
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
88
return iphoneSimulator.printDeviceTypes();
99
}

lib/commands/launch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import iphoneSimulatorLibPath = require("./../iphone-simulator");
22
import options = require("../options");
33

44
export class Command implements ICommand {
5-
public execute(args: string[]): string {
5+
public execute(args: string[]): Promise<string> {
66
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
77
return iphoneSimulator.run(args[0], args[1], options);
88
}

lib/commands/notify-post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import iphoneSimulatorLibPath = require("./../iphone-simulator");
22

33
export class Command implements ICommand {
4-
public execute(args: string[]): void {
4+
public execute(args: string[]): Promise<void> {
55
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
66
return iphoneSimulator.sendNotification(args[0], args[1]);
77
}

lib/commands/sdks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import iphoneSimulatorLibPath = require("./../iphone-simulator");
44

55
export class Command implements ICommand {
6-
public execute(args: string[]): void {
6+
public execute(args: string[]): Promise<void> {
77
var iphoneSimulator = new iphoneSimulatorLibPath.iPhoneSimulator();
88
return iphoneSimulator.printSDKS();
99
}

lib/declarations.ts

Lines changed: 23 additions & 23 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, options: IOptions): string;
6-
printDeviceTypes(): void;
7-
printSDKS(): void;
8-
sendNotification(notification: string, deviceId: string): void;
5+
run(applicationPath: string, applicationIdentifier: string, options: IOptions): Promise<string>;
6+
printDeviceTypes(): Promise<void>;
7+
printSDKS(): Promise<void>;
8+
sendNotification(notification: string, deviceId: string): Promise<void>;
99
createSimulator(): ISimulator;
1010
}
1111

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

2929
interface ISimctl {
30-
launch(deviceId: string, applicationIdentifier: string, options: IOptions): string;
31-
boot(deviceId: string): void;
32-
terminate(deviceId: string, appIdentifier: string): string;
33-
install(deviceId: string, applicationPath: string): void;
34-
uninstall(deviceId: string, applicationIdentifier: string, opts?: any): void;
35-
notifyPost(deviceId: string, notification: string): void;
36-
getDevices(): IDevice[];
30+
launch(deviceId: string, applicationIdentifier: string, options: IOptions): Promise<string>;
31+
boot(deviceId: string): Promise<void>;
32+
terminate(deviceId: string, appIdentifier: string): Promise<string>;
33+
install(deviceId: string, applicationPath: string): Promise<void>;
34+
uninstall(deviceId: string, applicationIdentifier: string, opts?: any): Promise<void>;
35+
notifyPost(deviceId: string, notification: string): Promise<void>;
36+
getDevices(): Promise<IDevice[]>;
3737
getLog(deviceId: string, predicate?: string): any;
38-
getAppContainer(deviceId: string, applicationIdentifier: string): string;
38+
getAppContainer(deviceId: string, applicationIdentifier: string): Promise<string>;
3939
}
4040

4141
interface IDictionary<T> {
4242
[key: string]: T;
4343
}
4444

4545
interface ISimulator extends INameGetter {
46-
getDevices(): IDevice[];
47-
getSdks(): ISdk[];
48-
run(applicationPath: string, applicationIdentifier: string, options: IOptions): string;
49-
sendNotification(notification: string, deviceId: string): void;
50-
getApplicationPath(deviceId: string, applicationIdentifier: string): string;
46+
getDevices(): Promise<IDevice[]>;
47+
getSdks(): Promise<ISdk[]>;
48+
run(applicationPath: string, applicationIdentifier: string, options: IOptions): Promise<string>;
49+
sendNotification(notification: string, deviceId: string): Promise<void>;
50+
getApplicationPath(deviceId: string, applicationIdentifier: string): Promise<string>;
5151
getInstalledApplications(deviceId: string): IApplication[];
52-
installApplication(deviceId: string, applicationPath: string): void;
53-
uninstallApplication(deviceId: string, appIdentifier: string): void;
54-
startApplication(deviceId: string, appIdentifier: string, options: IOptions): string;
55-
stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): string;
56-
getDeviceLogProcess(deviceId: string): any;
57-
startSimulator(options: IOptions, device?: IDevice): void;
52+
installApplication(deviceId: string, applicationPath: string): Promise<void>;
53+
uninstallApplication(deviceId: string, appIdentifier: string): Promise<void>;
54+
startApplication(deviceId: string, appIdentifier: string, options: IOptions): Promise<string>;
55+
stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): Promise<string>;
56+
getDeviceLogProcess(deviceId: string): Promise<any>;
57+
startSimulator(options: IOptions, device?: IDevice): Promise<void>;
5858
}
5959

6060
interface INameGetter {

lib/ios-sim.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ Object.defineProperty(publicApi, "getRunningSimulators", {
5252
return (...args: any[]) => {
5353
let isResolved = false;
5454

55-
return new Promise<any>((resolve, reject) => {
55+
return new Promise<any>(async (resolve, reject) => {
5656
const libraryPath = require("./iphone-simulator-xcode-simctl");
5757
const simulator = new libraryPath.XCodeSimctlSimulator();
58-
59-
const tryGetBootedDevices = () => {
58+
59+
const tryGetBootedDevices = async () => {
6060
try {
61-
return simulator.getBootedDevices.apply(simulator, args);
61+
return await simulator.getBootedDevices.apply(simulator, args);
6262
} catch (err) {
6363
if (!isResolved) {
6464
isResolved = true;
@@ -67,17 +67,16 @@ Object.defineProperty(publicApi, "getRunningSimulators", {
6767
}
6868
}
6969

70-
let result = tryGetBootedDevices();
70+
let result = await tryGetBootedDevices();
7171
if (result && result.length) {
7272
isResolved = true;
7373
resolve(result);
7474
return;
7575
}
7676

7777
if (!isResolved && (!result || !result.length)) {
78-
const timer = setTimeout(() => {
79-
result = tryGetBootedDevices();
80-
78+
const timer = setTimeout(async () => {
79+
result = await tryGetBootedDevices();
8180
if (!isResolved) {
8281
isResolved = true;
8382
resolve(result);

lib/iphone-simulator-xcode-simctl.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
3030
this.simctl = new Simctl();
3131
}
3232

33-
public getDevices(): IDevice[] {
33+
public getDevices(): Promise<IDevice[]> {
3434
return this.simctl.getDevices();
3535
}
3636

37-
public getSdks(): ISdk[] {
38-
let devices = this.simctl.getDevices();
37+
public async getSdks(): Promise<ISdk[]> {
38+
let devices = await this.simctl.getDevices();
3939
return _.map(devices, device => {
4040
return {
4141
displayName: `iOS ${device.runtimeVersion}`,
@@ -44,8 +44,8 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
4444
});
4545
}
4646

47-
public run(applicationPath: string, applicationIdentifier: string, options: IOptions): string {
48-
const device = this.getDeviceToRun(options);
47+
public async run(applicationPath: string, applicationIdentifier: string, options: IOptions): Promise<string> {
48+
const device = await this.getDeviceToRun(options);
4949

5050
this.startSimulator(options, device);
5151
if (!options.skipInstall) {
@@ -55,40 +55,41 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
5555
return this.simctl.launch(device.id, applicationIdentifier, options);
5656
}
5757

58-
public sendNotification(notification: string, deviceId: string): void {
58+
public sendNotification(notification: string, deviceId: string): Promise<void> {
5959
let device = this.getDeviceFromIdentifier(deviceId);
60+
6061
if (!device) {
6162
errors.fail("Could not find device.");
6263
}
6364

64-
this.simctl.notifyPost(deviceId, notification);
65+
return this.simctl.notifyPost(deviceId, notification);
6566
}
6667

67-
public getApplicationPath(deviceId: string, applicationIdentifier: string): string {
68+
public getApplicationPath(deviceId: string, applicationIdentifier: string): Promise<string> {
6869
return this.simctl.getAppContainer(deviceId, applicationIdentifier);
6970
}
7071

7172
public getInstalledApplications(deviceId: string): IApplication[] {
7273
return common.getInstalledApplications(deviceId);
7374
}
7475

75-
public installApplication(deviceId: string, applicationPath: string): void {
76+
public installApplication(deviceId: string, applicationPath: string): Promise<void> {
7677
return this.simctl.install(deviceId, applicationPath);
7778
}
7879

79-
public uninstallApplication(deviceId: string, appIdentifier: string): void {
80+
public uninstallApplication(deviceId: string, appIdentifier: string): Promise<void> {
8081
return this.simctl.uninstall(deviceId, appIdentifier, { skipError: true });
8182
}
8283

83-
public startApplication(deviceId: string, appIdentifier: string, options: IOptions): string {
84+
public async startApplication(deviceId: string, appIdentifier: string, options: IOptions): Promise<string> {
8485
// simctl launch command does not launch the process immediately and we have to wait a little bit,
8586
// just to ensure all related processes and services are alive.
86-
const launchResult = this.simctl.launch(deviceId, appIdentifier, options);
87+
const launchResult = await this.simctl.launch(deviceId, appIdentifier, options);
8788
utils.sleep(0.5);
8889
return launchResult;
8990
}
9091

91-
public stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): string {
92+
public async stopApplication(deviceId: string, appIdentifier: string, bundleExecutable: string): Promise<string> {
9293
try {
9394
let xcodeMajorVersion: number = null;
9495
try {
@@ -103,7 +104,7 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
103104
// Xcode 7.x does not have support for `xcrun simctl terminate` command
104105
resultOfTermination = childProcess.execSync(`killall ${bundleExecutable}`, { skipError: true });
105106
} else {
106-
resultOfTermination = this.simctl.terminate(deviceId, appIdentifier);
107+
resultOfTermination = await this.simctl.terminate(deviceId, appIdentifier);
107108
}
108109

109110
// killall command does not terminate the processes immediately and we have to wait a little bit,
@@ -116,9 +117,9 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
116117
}
117118
}
118119

119-
public getDeviceLogProcess(deviceId: string, predicate?: string): child_process.ChildProcess {
120+
public async getDeviceLogProcess(deviceId: string, predicate?: string): Promise<child_process.ChildProcess> {
120121
if (!this.isDeviceLogOperationStarted) {
121-
const device = this.getDeviceFromIdentifier(deviceId);
122+
const device = await this.getDeviceFromIdentifier(deviceId);
122123
const deviceVersion = device ? device.runtimeVersion : "";
123124
const majorVersion = deviceVersion.split(".")[0];
124125

@@ -135,8 +136,8 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
135136
return this.deviceLogChildProcess;
136137
}
137138

138-
private getDeviceToRun(options: IOptions, device?: any): IDevice {
139-
let devices = _.sortBy(this.simctl.getDevices(), (device) => device.runtimeVersion),
139+
private async getDeviceToRun(options: IOptions, device?: any): Promise<IDevice> {
140+
let devices = _.sortBy(await this.simctl.getDevices(), (device) => device.runtimeVersion),
140141
sdkVersion = options.sdkVersion || options.sdk,
141142
deviceIdOrName = options.device;
142143

@@ -181,30 +182,30 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
181182
return device.state === 'Booted';
182183
}
183184

184-
private getBootedDevice(): IDevice {
185-
let devices = this.simctl.getDevices();
185+
private async getBootedDevice(): Promise<IDevice> {
186+
let devices = await this.simctl.getDevices();
186187
return _.find(devices, device => this.isDeviceBooted(device));
187188
}
188189

189-
private getBootedDevices(): IDevice[] {
190-
const devices = this.simctl.getDevices();
190+
private async getBootedDevices(): Promise<IDevice[]> {
191+
const devices = await this.simctl.getDevices();
191192
return _.filter(devices, device => this.isDeviceBooted(device));
192193
}
193194

194-
public startSimulator(options: IOptions, device?: IDevice): void {
195+
public async startSimulator(options: IOptions, device?: IDevice): Promise<void> {
195196
if (!device && options.device) {
196197
this.verifyDevice(options.device);
197198
}
198199

199-
device = device || this.getDeviceToRun(options);
200+
device = device || await this.getDeviceToRun(options);
200201

201202
// In case the id is undefined, skip verification - we'll start default simulator.
202203
if (device && device.id) {
203204
this.verifyDevice(device);
204205
}
205206

206207
if (!device || !device.runtimeVersion || !device.fullId) {
207-
device = this.getDeviceToRun(options, device);
208+
device = await this.getDeviceToRun(options, device);
208209
}
209210

210211
if (!this.isDeviceBooted(device)) {
@@ -214,7 +215,7 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
214215
if (isSimulatorAppRunning) {
215216
// In case user closes simulator window but simulator app is still alive
216217
if (!haveBootedDevices) {
217-
device = this.getDeviceToRun(options);
218+
device = await this.getDeviceToRun(options);
218219
}
219220
this.simctl.boot(device.id);
220221
} else {
@@ -229,8 +230,8 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
229230
}
230231
}
231232

232-
private haveBootedDevices(): boolean {
233-
const bootedDevices = this.getBootedDevices();
233+
private async haveBootedDevices(): Promise<boolean> {
234+
const bootedDevices = await this.getBootedDevices();
234235
return bootedDevices && bootedDevices.length > 0;
235236
}
236237

@@ -245,16 +246,16 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
245246
}
246247
}
247248

248-
private verifyDevice(device: IDevice | string): void {
249-
const availableDevices = this.getDevices();
249+
private async verifyDevice(device: IDevice | string): Promise<void> {
250+
const availableDevices = await this.getDevices();
250251
const deviceId = (<IDevice>device).id || device;
251252
if (!_.find(availableDevices, { id: deviceId }) && !_.find(availableDevices, { name: deviceId })) {
252253
errors.fail(`No simulator image available for device identifier '${deviceId}'.`);
253254
}
254255
}
255256

256-
private getDeviceFromIdentifier(deviceId: string) {
257-
const availableDevices = this.getDevices();
257+
private async getDeviceFromIdentifier(deviceId: string) {
258+
const availableDevices = await this.getDevices();
258259

259260
return _.find(availableDevices, { id: deviceId });
260261
}

lib/iphone-simulator.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ export class iPhoneSimulator implements IiPhoneSimulator {
1818
this.simulator = this.createSimulator();
1919
}
2020

21-
public run(applicationPath: string, applicationIdentifier: string, options: IOptions): string {
21+
public async run(applicationPath: string, applicationIdentifier: string, options: IOptions): Promise<string> {
2222
if (!fs.existsSync(applicationPath)) {
2323
errors.fail("Path does not exist ", applicationPath);
2424
}
2525

2626
if (options.device) {
27-
const hasSuchDevice = _.find(this.simulator.getDevices(), device => device.name === options.device || device.id === options.device);
27+
const hasSuchDevice = _.find(await this.simulator.getDevices(), device => device.name === options.device || device.id === options.device);
2828
if (!hasSuchDevice) {
2929
errors.fail(`Unable to find device ${options.device}.`);
3030
}
3131
}
3232

3333
let sdkVersion = options.sdkVersion || options.sdk;
3434
if (sdkVersion) {
35-
let runtimeVersions = _.unique(_.map(this.simulator.getDevices(), (device: IDevice) => device.runtimeVersion));
35+
let runtimeVersions = _.unique(_.map(await this.simulator.getDevices(), (device: IDevice) => device.runtimeVersion));
3636
if (!_.contains(runtimeVersions, sdkVersion)) {
3737
errors.fail(`Unable to find sdk ${sdkVersion}. The valid runtime versions are ${runtimeVersions.join(", ")}`);
3838
}
@@ -41,13 +41,13 @@ export class iPhoneSimulator implements IiPhoneSimulator {
4141
return this.simulator.run(applicationPath, applicationIdentifier, options);
4242
}
4343

44-
public printDeviceTypes(): void {
45-
let devices = this.simulator.getDevices();
44+
public async printDeviceTypes(): Promise<void> {
45+
let devices = await this.simulator.getDevices();
4646
_.each(devices, device => console.log(`Device Identifier: ${device.fullId}. ${os.EOL}Runtime version: ${device.runtimeVersion} ${os.EOL}`));
4747
}
4848

49-
public printSDKS(): void {
50-
let sdks = this.simulator.getSdks();
49+
public async printSDKS(): Promise<void> {
50+
let sdks = await this.simulator.getSdks();
5151
_.each(sdks, (sdk) => {
5252
let output = ` Display Name: ${sdk.displayName} ${os.EOL} Version: ${sdk.version} ${os.EOL}`;
5353
if (sdk.rootPath) {
@@ -57,7 +57,7 @@ export class iPhoneSimulator implements IiPhoneSimulator {
5757
});
5858
}
5959

60-
public sendNotification(notification: string, deviceId: string): void {
60+
public sendNotification(notification: string, deviceId: string): Promise<void> {
6161
if (!notification) {
6262
errors.fail("Notification required.");
6363
}

0 commit comments

Comments
 (0)