microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
398e2b0b72a5c6d40ff7912643fed1123c69d867

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

122lines · 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";
16import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
17
18export class IOSPlatform implements IAppPlatform {
19 private static deviceString = "device";
20 private static simulatorString = "simulator";
21
22 private plistBuddy = new PlistBuddy();
23
24 private projectPath: string;
25 private simulatorTarget: string;
26 private isSimulator: boolean;
27 private iosProjectPath: string;
28
29 // We should add the common iOS build/run erros we find to this list
30 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure = {
31 "No devices are booted": "Unable to launch iOS simulator. Try specifying a different target.",
32 "FBSOpenApplicationErrorDomain": "Unable to launch iOS simulator. Try specifying a different target.",
33 };
34
35 private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
36
37 constructor(private runOptions: IRunOptions) {
38 this.projectPath = this.runOptions.projectRoot;
39 this.simulatorTarget = this.runOptions.target || IOSPlatform.simulatorString;
40 this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
41 this.iosProjectPath = this.runOptions.iosProjectPath || null;
42 }
43
44 public runApp(): Q.Promise<void> {
45 // Compile, deploy, and launch the app on either a simulator or a device
46 if (this.isSimulator) {
47 // React native supports running on the iOS simulator from the command line
48 let runArguments: string[] = [];
49 if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
50 runArguments.push("--simulator", this.simulatorTarget);
51 }
52
53 if (this.iosProjectPath) {
54 runArguments.push("--project-path", this.iosProjectPath);
55 }
56
57 const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
58 return new OutputVerifier(
59 () =>
60 this.generateSuccessPatterns(),
61 () =>
62 Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
63 }
64
65 return new Compiler(this.iosProjectPath || this.projectPath).compile().then(() => {
66 return new DeviceDeployer(this.iosProjectPath || this.projectPath).deploy();
67 }).then(() => {
68 return new DeviceRunner(this.iosProjectPath || this.projectPath).run();
69 });
70 }
71
72 public enableJSDebuggingMode(): Q.Promise<void> {
73 // Configure the app for debugging
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.iosProjectPath || 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(this.iosProjectPath || this.projectPath),
86 ]).spread((debugModeSetting: string, bundleId: string) => {
87 if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
88 // Debugging must still be enabled
89 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
90 // but that file is written to by the app on occasion. To avoid races, we shut the app
91 // down before writing to the file.
92 const childProcess = new ChildProcess();
93
94 return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => {
95 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
96 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
97 const match = regex.exec(output);
98
99 // If we don't find a match, the app must not be running and so we do not need to close it
100 if (match) {
101 return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`);
102 }
103 }).then(() => {
104 // Write to the settings file while the app is not running to avoid races
105 return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
106 }).then(() => {
107 // Relaunch the app
108 return this.runApp();
109 });
110 }
111 });
112 }
113
114 private generateSuccessPatterns(): Q.Promise<string[]> {
115 return this.getBundleId().then(bundleId =>
116 IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `]));
117 }
118
119 private getBundleId(): Q.Promise<string> {
120 return this.plistBuddy.getBundleId(this.iosProjectPath || this.projectPath);
121 }
122}
123