microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e68903489b95998fd4d1f3c1ea0127f369d0982e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

251lines · 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";
6import * as semver from "semver";
7
8import {ChildProcess} from "../../common/node/childProcess";
9import {CommandExecutor} from "../../common/commandExecutor";
10import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform";
11import {IIOSRunOptions} from "../launchArgs";
12import {PlistBuddy} from "./plistBuddy";
13import {IOSDebugModeManager} from "./iOSDebugModeManager";
14import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
15import {SettingsHelper} from "../settingsHelper";
16import {RemoteExtension} from "../../common/remoteExtension";
17import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper";
18import {TelemetryHelper} from "../../common/telemetryHelper";
19import { InternalErrorCode } from "../../common/error/internalErrorCode";
20import * as nls from "vscode-nls";
21const localize = nls.loadMessageBundle();
22
23export class IOSPlatform extends GeneralMobilePlatform {
24 public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
25 private static remoteExtension: RemoteExtension;
26
27 private plistBuddy = new PlistBuddy();
28 private targetType: TargetType = "simulator";
29 private iosProjectRoot: string;
30 private iosDebugModeManager: IOSDebugModeManager;
31
32 private defaultConfiguration: string = "Debug";
33 private configurationArgumentName: string = "--configuration";
34
35 // We should add the common iOS build/run errors we find to this list
36 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
37 pattern: "No devices are booted",
38 errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
39 }, {
40 pattern: "FBSOpenApplicationErrorDomain",
41 errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
42 }, {
43 pattern: "ios-deploy",
44 errorCode: InternalErrorCode.IOSDeployNotFound,
45 }];
46
47 private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
48
49 public showDevMenu(deviceId?: string): Q.Promise<void> {
50 return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId);
51 }
52
53 public reloadApp(deviceId?: string): Q.Promise<void> {
54 return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId);
55 }
56
57 constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
58 super(runOptions, platformDeps);
59
60 this.runOptions.configuration = this.getConfiguration();
61
62 if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
63 this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead."));
64 }
65
66 const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(this.runArguments, "--project-path", false);
67 this.iosProjectRoot = path.join(this.projectPath, iosProjectFolderPath || this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
68 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
69 this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.projectPath, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme);
70
71 if (this.runArguments && this.runArguments.length > 0) {
72 this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
73 IOSPlatform.deviceString : IOSPlatform.simulatorString;
74 return;
75 }
76
77 if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
78 this.runOptions.target !== IOSPlatform.deviceString)) {
79
80 this.targetType = IOSPlatform.simulatorString;
81 return;
82 }
83
84 this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
85 }
86
87 public runApp(): Q.Promise<void> {
88 const extProps = {
89 platform: {
90 value: "ios",
91 isPii: false,
92 },
93 };
94
95 return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
96 // Compile, deploy, and launch the app on either a simulator or a device
97 const env = this.getEnvArgument();
98
99 return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
100 .then(version => {
101 if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
102 this.runArguments.push("--no-packager");
103 }
104 // Since @react-native-community/cli@2.1.0 build output are hidden by default
105 // we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns
106 if (semver.gte(version, "0.60.0")) {
107 this.runArguments.push("--verbose");
108 }
109 const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
110 return new OutputVerifier(() => this.generateSuccessPatterns(version), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
111 .process(runIosSpawn);
112 });
113 });
114 }
115
116 public enableJSDebuggingMode(): Q.Promise<void> {
117 // Configure the app for debugging
118 if (this.targetType === IOSPlatform.deviceString) {
119 // Note that currently we cannot automatically switch the device into debug mode.
120 this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
121 return Q.resolve<void>(void 0);
122 }
123
124 // Wait until the configuration file exists, and check to see if debugging is enabled
125 return Q.all<boolean | string>([
126 this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
127 this.getBundleId(),
128 ])
129 .spread((debugModeEnabled: boolean, bundleId: string) => {
130 if (debugModeEnabled) {
131 return Q.resolve(void 0);
132 }
133
134 // Debugging must still be enabled
135 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
136 // but that file is written to by the app on occasion. To avoid races, we shut the app
137 // down before writing to the file.
138 const childProcess = new ChildProcess();
139
140 return childProcess.execToString("xcrun simctl spawn booted launchctl list")
141 .then((output: string) => {
142 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
143 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
144 const match = regex.exec(output);
145
146 // If we don't find a match, the app must not be running and so we do not need to close it
147 return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
148 })
149 .then(() => {
150 // Write to the settings file while the app is not running to avoid races
151 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
152 })
153 .then(() => {
154 // Relaunch the app
155 return this.runApp();
156 });
157 });
158 }
159
160 public disableJSDebuggingMode(): Q.Promise<void> {
161 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
162 }
163
164 public prewarmBundleCache(): Q.Promise<void> {
165 return this.packager.prewarmBundleCache("ios");
166 }
167
168 public getRunArguments(): string[] {
169 let runArguments: string[] = [];
170
171 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
172 runArguments = this.runOptions.runArguments;
173 if (this.runOptions.scheme) {
174 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false);
175 if (!schemeFromArgs) {
176 runArguments.push("--scheme", this.runOptions.scheme);
177 } else {
178 this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted"));
179 }
180 }
181 } else {
182 if (this.runOptions.target) {
183 if (this.runOptions.target === IOSPlatform.deviceString ||
184 this.runOptions.target === IOSPlatform.simulatorString) {
185
186 runArguments.push(`--${this.runOptions.target}`);
187 } else {
188 runArguments.push("--simulator", `${this.runOptions.target}`);
189 }
190 }
191
192 if (this.runOptions.iosRelativeProjectPath) {
193 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
194 }
195
196 // provide any defined scheme
197 if (this.runOptions.scheme) {
198 runArguments.push("--scheme", this.runOptions.scheme);
199 }
200 }
201
202 return runArguments;
203 }
204
205 private generateSuccessPatterns(version: string): Q.Promise<string[]> {
206 // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
207 let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
208 if (this.targetType === IOSPlatform.deviceString) {
209 if (semver.gte(version, "0.60.0")) {
210 successPatterns.push("success Installed the app on the device");
211 } else {
212 successPatterns.push("INSTALLATION SUCCEEDED");
213 }
214 return Q(successPatterns);
215 } else {
216 return this.getBundleId()
217 .then(bundleId => {
218 if (semver.gte(version, "0.60.0")) {
219 successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `);
220 } else {
221 successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
222 }
223 return successPatterns;
224 });
225 }
226
227 }
228
229 private getConfiguration(): string {
230 return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
231 }
232
233 private getBundleId(): Q.Promise<string> {
234 let scheme = this.runOptions.scheme;
235 if (!scheme) {
236 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
237 if (schemeFromArgs) {
238 scheme = schemeFromArgs;
239 }
240 }
241 return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme);
242 }
243
244 private static remote(fsPath: string): RemoteExtension {
245 if (this.remoteExtension) {
246 return this.remoteExtension;
247 } else {
248 return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
249 }
250 }
251}
252