microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a289475be0da2ee07b9b056760f2f0a3076877b2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

111lines · 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/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 "../../common/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 runIosSpawn = new CommandExecutor(this.projectPath).spawnChildReactCommandProcess("run-ios", runArguments);
40 const deferred = Q.defer<void>();
41 runIosSpawn.stderr.on("data", (data: Buffer) => {
42 const dataString = data.toString();
43 if (dataString.indexOf("No devices are booted") !== -1 // No emulators are started
44 || dataString.indexOf("FBSOpenApplicationErrorDomain") !== -1) { // The incorrect emulator is started
45 deferred.reject(new Error("Unable to launch iOS simulator. Try specifying a different target."));
46 }
47 });
48
49 return runIosSpawn.outcome.then(() => {
50 deferred.resolve(void 0); // We resolve deferred when the process ends, in case it wasn't already rejected
51 return deferred.promise; // We return the promise. If an error was detected on stderr, this will be a rejection
52 });
53 }
54
55 return new Compiler(this.projectPath).compile().then(() => {
56 return new DeviceDeployer(this.projectPath).deploy();
57 }).then(() => {
58 return new DeviceRunner(this.projectPath).run();
59 });
60 }
61
62 public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> {
63 // Configure the app for debugging
64 this.consumeArguments(launchArgs);
65
66 if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
67 // Note that currently we cannot automatically switch the device into debug mode.
68 Log.logToConsole("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
69 return Q.resolve<void>(void 0);
70 }
71
72 const iosDebugModeManager = new IOSDebugModeManager(this.projectPath);
73
74 // Wait until the configuration file exists, and check to see if debugging is enabled
75 return Q.all([
76 iosDebugModeManager.getSimulatorJSDebuggingModeSetting(),
77 this.plistBuddy.getBundleId(launchArgs.projectRoot)
78 ]).spread((debugModeSetting: string, bundleId: string) => {
79 if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
80 // Debugging must still be enabled
81 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
82 // but that file is written to by the app on occasion. To avoid races, we shut the app
83 // down before writing to the file.
84 const childProcess = new ChildProcess();
85
86 return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => {
87 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
88 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
89 const match = regex.exec(output);
90
91 // If we don't find a match, the app must not be running and so we do not need to close it
92 if (match) {
93 return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`);
94 }
95 }).then(() => {
96 // Write to the settings file while the app is not running to avoid races
97 return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
98 }).then(() => {
99 // Relaunch the app
100 return this.runApp(launchArgs);
101 });
102 }
103 });
104 }
105
106 private consumeArguments(launchArgs: IRunOptions): void {
107 this.projectPath = launchArgs.projectRoot;
108 this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString;
109 this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
110 }
111}
112