microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dbe130e490f33a2e524a918990d449206b94028e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

117lines · 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 {ChildProcess} from "../../common/node/childProcess";
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 {PlistBuddy} from "../../common/ios/plistBuddy";
15import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
16
17export class IOSPlatform implements IAppPlatform {
18 private static deviceString = "device";
19 private static simulatorString = "simulator";
20
21 private plistBuddy = new PlistBuddy();
22
23 private projectPath: string;
24 private simulatorTarget: string;
25 private isSimulator: boolean;
26
27 public runApp(launchArgs: IRunOptions): Q.Promise<void> {
28 // Compile, deploy, and launch the app on either a simulator or a device
29 this.consumeArguments(launchArgs);
30
31 if (this.isSimulator) {
32 // React native supports running on the iOS simulator from the command line
33 let runArguments: string[] = [];
34 if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
35 runArguments.push("--simulator");
36 runArguments.push(this.simulatorTarget);
37 }
38
39 const runIos = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
40 const deferred = Q.defer<void>();
41 runIos.on("error", (err: Error) => {
42 deferred.reject(err);
43 });
44 runIos.stderr.on("data", (data: Buffer) => {
45 const dataString = data.toString();
46 if (dataString.indexOf("No devices are booted") !== -1 // No emulators are started
47 || dataString.indexOf("FBSOpenApplicationErrorDomain") !== -1) { // The incorrect emulator is started
48 deferred.reject(new Error("Unable to launch iOS simulator. Try specifying a different target."));
49 }
50 });
51 runIos.on("exit", (code: number) => {
52 if (code !== 0) {
53 const err = new Error(`Command failed with exit code ${code}`);
54 Log.commandFailed(["react-native", "run-ios"].concat(runArguments).join(" "), err);
55 deferred.reject(err);
56 } else {
57 deferred.resolve(void 0);
58 }
59 });
60 return deferred.promise;
61 }
62
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 iosDebugModeManager = new IOSDebugModeManager(this.projectPath);
81
82 // Wait until the configuration file exists, and check to see if debugging is enabled
83 return Q.all([
84 iosDebugModeManager.getSimulatorJSDebuggingModeSetting(),
85 this.plistBuddy.getBundleId(launchArgs.projectRoot)
86 ]).spread((debugModeSetting: string, bundleId: string) => {
87 if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
88 // Debugging must still be enabled
89 const commandExecutor = new CommandExecutor();
90 const childProcess = new ChildProcess();
91 const launchAppString = `xcrun simctl launch booted ${bundleId}`;
92 // simctl launch returns the process ID of the app in the simulator
93 return childProcess.exec(launchAppString).outcome.then((buffer: Buffer) => {
94 // Kill the simulated app
95 const pidMatch = buffer.toString().match(/: ([0-9]+)/);
96 const pid = pidMatch[1];
97 return commandExecutor.execute(`kill ${pid} # Restarting app`);
98 }).then(() => {
99 // Give the process some time to exit
100 return Q.delay(1000);
101 }).then(() => {
102 // Write to the settings file while the app is not running to avoid races
103 return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
104 }).then(() => {
105 // Relaunch the app
106 return this.runApp(launchArgs);
107 });
108 }
109 });
110 }
111
112 private consumeArguments(launchArgs: IRunOptions): void {
113 this.projectPath = launchArgs.projectRoot;
114 this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString;
115 this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
116 }
117}
118