microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0db0be15f47e1f4a9fb28cb991afb47ab8f62d29

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/ios/iOSPlatform.ts

193lines · 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 {GeneralMobilePlatform} from "../../common/generalMobilePlatform";
11import {IIOSRunOptions} from "../../common/launchArgs";
12import {PlistBuddy} from "../../common/ios/plistBuddy";
13import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
14import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
15import {RemoteExtension} from "../../common/remoteExtension";
16
17/* tslint:disable:no-var-requires */
18const ErrorStrings = require("../../../errorStrings/errorStrings.json");
19/* tslint:enable:no-var-requires */
20
21export class IOSPlatform extends GeneralMobilePlatform {
22 public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
23 public static DEFAULT_IOS_SIMULATOR_TARGET = "iPhone 5";
24
25 private static deviceString = "device";
26 private static simulatorString = "simulator";
27
28 private plistBuddy = new PlistBuddy();
29 private target: string = "";
30 private targetType: string = "simulator";
31 private iosProjectRoot: string;
32
33 // We should add the common iOS build/run erros we find to this list
34 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
35 pattern: "No devices are booted",
36 message: ErrorStrings.IOSSimulatorNotLaunchable,
37 }, {
38 pattern: "FBSOpenApplicationErrorDomain",
39 message: ErrorStrings.IOSSimulatorNotLaunchable,
40 }, {
41 pattern: "ios-deploy",
42 message: ErrorStrings.IOSDeployNotFound,
43 }];
44
45 private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
46
47 // We set remoteExtension = null so that if there is an instance of iOSPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests.
48 constructor(protected runOptions: IIOSRunOptions, { remoteExtension = undefined }: {remoteExtension?: RemoteExtension} = {}) {
49 super(runOptions, { remoteExtension: remoteExtension });
50
51 if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
52 Log.logMessage("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead");
53 }
54
55 this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || "");
56
57 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
58 if (this.runOptions.runArguments.indexOf(`--${IOSPlatform.deviceString}`) > -1) {
59 this.targetType = IOSPlatform.deviceString;
60 } else {
61 this.targetType = IOSPlatform.simulatorString;
62 }
63 } else {
64 if (this.runOptions.targetType) {
65 if (this.runOptions.targetType !== IOSPlatform.simulatorString &&
66 this.runOptions.targetType !== IOSPlatform.deviceString) {
67 throw Error(`Invalid Run iOS targetType: '${this.runOptions.targetType}' in .vscode/launch.json.` +
68 "Please use 'simulator' or 'device' targetType instead");
69 }
70 }
71
72 if (this.runOptions.target === IOSPlatform.simulatorString) {
73 this.targetType = this.runOptions.target;
74 this.target = IOSPlatform.DEFAULT_IOS_SIMULATOR_TARGET;
75 return;
76 }
77
78 if (this.runOptions.target === IOSPlatform.deviceString) {
79 this.targetType = this.runOptions.target;
80 this.target = "";
81 return;
82 }
83
84 this.targetType = this.runOptions.targetType || IOSPlatform.simulatorString;
85
86 if (this.runOptions.target) {
87 this.target = this.runOptions.target;
88 } else if (this.targetType === IOSPlatform.simulatorString) {
89 this.target = IOSPlatform.DEFAULT_IOS_SIMULATOR_TARGET;
90 } else {
91 this.target = "";
92 }
93 }
94 }
95
96 public runApp(): Q.Promise<void> {
97 // Compile, deploy, and launch the app on either a simulator or a device
98 const runArguments = this.getRunArgument();
99
100 const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
101 return new OutputVerifier(
102 () =>
103 this.generateSuccessPatterns(),
104 () =>
105 Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
106 }
107
108 public enableJSDebuggingMode(): Q.Promise<void> {
109 // Configure the app for debugging
110 if (this.targetType === IOSPlatform.deviceString) {
111 // Note that currently we cannot automatically switch the device into debug mode.
112 Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
113 return Q.resolve<void>(void 0);
114 }
115
116 const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
117
118 // Wait until the configuration file exists, and check to see if debugging is enabled
119 return Q.all<boolean | string>([
120 iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
121 this.getBundleId(),
122 ])
123 .spread((debugModeEnabled: boolean, bundleId: string) => {
124 if (debugModeEnabled) {
125 return Q.resolve(void 0);
126 }
127
128 // Debugging must still be enabled
129 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
130 // but that file is written to by the app on occasion. To avoid races, we shut the app
131 // down before writing to the file.
132 const childProcess = new ChildProcess();
133
134 return childProcess.execToString("xcrun simctl spawn booted launchctl list")
135 .then((output: string) => {
136 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
137 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
138 const match = regex.exec(output);
139
140 // If we don't find a match, the app must not be running and so we do not need to close it
141 return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
142 })
143 .then(() => {
144 // Write to the settings file while the app is not running to avoid races
145 return iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
146 })
147 .then(() => {
148 // Relaunch the app
149 return this.runApp();
150 });
151 });
152 }
153
154 public prewarmBundleCache(): Q.Promise<void> {
155 return this.remoteExtension.prewarmBundleCache(this.platformName);
156 }
157
158 public getRunArgument(): string[] {
159 let runArguments: string[] = [];
160
161 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
162 return this.runOptions.runArguments;
163 }
164
165 if (this.targetType) {
166 runArguments.push(`--${this.targetType}`);
167 }
168
169 if (this.target) {
170 runArguments.push(this.target);
171 }
172
173 if (this.runOptions.iosRelativeProjectPath) {
174 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
175 }
176
177 // provide any defined scheme
178 if (this.runOptions.scheme) {
179 runArguments.push("--scheme", this.runOptions.scheme);
180 }
181
182 return runArguments;
183 }
184
185 private generateSuccessPatterns(): Q.Promise<string[]> {
186 return this.getBundleId().then(bundleId =>
187 IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `]));
188 }
189
190 private getBundleId(): Q.Promise<string> {
191 return this.plistBuddy.getBundleId(this.iosProjectRoot);
192 }
193}
194