microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bb77358c8dc7ea46fae9d6aa601a11fde8eed0fd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

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