microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
65bb0c85df6a89b406b370fd653d87f0a7043304

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

104lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5
6import {Log} from "../../common/log";
7import {PromiseUtil} from "../../common/node/promise";
8import {CommandExecutor} from "../../common/commandExecutor";
9import {IAppPlatform} from "../platformResolver";
10import {Compiler} from "./compiler";
11import {DeviceDeployer} from "./deviceDeployer";
12import {DeviceRunner} from "./deviceRunner";
13import {IRunOptions} from "../launchArgs";
14import {SimulatorPlist} from "./simulatorPlist";
15import {PlistBuddy} from "./plistBuddy";
16
17export class IOSPlatform implements IAppPlatform {
18 private static deviceString = "device";
19 private static simulatorString = "simulator";
20
21 private projectPath: string;
22 private simulatorTarget: string;
23 private isSimulator: boolean;
24
25 public runApp(launchArgs: IRunOptions): Q.Promise<void> {
26 // Compile, deploy, and launch the app on either a simulator or a device
27 this.consumeArguments(launchArgs);
28
29 if (this.isSimulator) {
30 // React native supports running on the iOS simulator from the command line
31 let runArguments: string[] = [];
32 if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
33 runArguments.push("--simulator");
34 runArguments.push(this.simulatorTarget);
35 }
36
37 return new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments).then((runIos) => {
38 const deferred = Q.defer<void>();
39 runIos.on("error", (err: Error) => {
40 deferred.reject(err);
41 });
42 runIos.stderr.on("data", (data: Buffer) => {
43 const dataString = data.toString();
44 if (dataString.indexOf("No devices are booted") !== -1 // No emulators are started
45 || dataString.indexOf("FBSOpenApplicationErrorDomain") !== -1) { // The incorrect emulator is started
46 deferred.reject(new Error("Unable to launch iOS simulator. Try specifying a different target."));
47 }
48 });
49 runIos.on("exit", (code: number) => {
50 if (code !== 0) {
51 const err = new Error(`Command failed with exit code ${code}`);
52 Log.commandFailed(["react-native", "run-ios"].concat(runArguments).join(" "), err);
53 deferred.reject(err);
54 } else {
55 deferred.resolve(void 0);
56 }
57 });
58 return deferred.promise;
59 });
60 }
61
62 // TODO: This is currently a stub, device debugging is not yet implemented
63 return new Compiler(this.projectPath).compile().then(() => {
64 return new DeviceDeployer(this.projectPath).deploy();
65 }).then(() => {
66 return new DeviceRunner(this.projectPath).run();
67 });
68 }
69
70 public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> {
71 // Configure the app for debugging
72 this.consumeArguments(launchArgs);
73
74 if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
75 // Note that currently we cannot automatically switch the device into debug mode.
76 Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
77 return Q.resolve<void>(void 0);
78 }
79
80 const plistBuddy = new PlistBuddy();
81 const simulatorPlist = new SimulatorPlist(launchArgs.projectRoot);
82 const pu = new PromiseUtil();
83 // Find the plistFile with the configuration setting
84 // There is a race here between us checking for the plist file, and the application starting up.
85 return pu.retryAsync(() => simulatorPlist.findPlistFile().catch((): string => null),
86 (file: string) => file !== null, 5, 2000, "Unable to find plist file to enable debugging")
87 .then((plistFile: string) => {
88 // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode
89 // This is approximately equivalent to clicking the "Debug in Chrome" button
90 return plistBuddy.setPlistProperty(plistFile, ":RCTDevMenu:executorClass", "RCTWebSocketExecutor");
91 }).then(() => {
92 return plistBuddy.getBundleId(launchArgs.projectRoot);
93 }).then((bundleId: string) => {
94 // Relaunch the app so the new setting can take effect
95 return new CommandExecutor().execute(`xcrun simctl launch booted ${bundleId}`);
96 });
97 }
98
99 private consumeArguments(launchArgs: IRunOptions): void {
100 this.projectPath = launchArgs.projectRoot;
101 this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString;
102 this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
103 }
104}
105