microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ffdf159274093d12e32a09aafae6d0de3099bb4a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

408lines · 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 path from "path";
5import * as semver from "semver";
6
7import { ChildProcess } from "../../common/node/childProcess";
8import { CommandExecutor } from "../../common/commandExecutor";
9import { GeneralMobilePlatform, MobilePlatformDeps, TargetType } from "../generalMobilePlatform";
10import { IIOSRunOptions, PlatformType } from "../launchArgs";
11import { PlistBuddy } from "./plistBuddy";
12import { IOSDebugModeManager } from "./iOSDebugModeManager";
13import { OutputVerifier, PatternToFailure } from "../../common/outputVerifier";
14import { TelemetryHelper } from "../../common/telemetryHelper";
15import { InternalErrorCode } from "../../common/error/internalErrorCode";
16import * as nls from "vscode-nls";
17import { AppLauncher } from "../appLauncher";
18import { IiOSSimulator, IOSSimulatorManager } from "./iOSSimulatorManager";
19nls.config({
20 messageFormat: nls.MessageFormat.bundle,
21 bundleFormat: nls.BundleFormat.standalone,
22})();
23const localize = nls.loadMessageBundle();
24
25export class IOSPlatform extends GeneralMobilePlatform {
26 public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
27
28 private plistBuddy = new PlistBuddy();
29 private targetType: TargetType = "simulator";
30 private iosProjectRoot: string;
31 private iosDebugModeManager: IOSDebugModeManager;
32 private simulatorManager: IOSSimulatorManager;
33
34 private defaultConfiguration: string = "Debug";
35 private configurationArgumentName: string = "--configuration";
36
37 // We should add the common iOS build/run errors we find to this list
38 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [
39 {
40 pattern: "No devices are booted",
41 errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
42 },
43 {
44 pattern: "FBSOpenApplicationErrorDomain",
45 errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
46 },
47 {
48 pattern: "ios-deploy",
49 errorCode: InternalErrorCode.IOSDeployNotFound,
50 },
51 ];
52
53 private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
54
55 public showDevMenu(appLauncher: AppLauncher): Promise<void> {
56 const worker = appLauncher.getAppWorker();
57 if (worker) {
58 worker.showDevMenuCommand();
59 }
60
61 return Promise.resolve();
62 }
63
64 public reloadApp(appLauncher: AppLauncher): Promise<void> {
65 const worker = appLauncher.getAppWorker();
66 if (worker) {
67 worker.reloadAppCommand();
68 }
69 return Promise.resolve();
70 }
71
72 constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
73 super(runOptions, platformDeps);
74
75 this.simulatorManager = new IOSSimulatorManager();
76 this.runOptions.configuration = this.getConfiguration();
77
78 if (this.runOptions.iosRelativeProjectPath) {
79 // Deprecated option
80 this.logger.warning(
81 localize(
82 "iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead",
83 "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead.",
84 ),
85 );
86 }
87
88 const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(
89 this.runArguments,
90 "--project-path",
91 false,
92 );
93 this.iosProjectRoot = path.join(
94 this.projectPath,
95 iosProjectFolderPath ||
96 this.runOptions.iosRelativeProjectPath ||
97 IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH,
98 );
99 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
100 this.iosDebugModeManager = new IOSDebugModeManager(
101 this.iosProjectRoot,
102 this.projectPath,
103 schemeFromArgs ? schemeFromArgs : this.runOptions.scheme,
104 );
105
106 if (this.runArguments && this.runArguments.length > 0) {
107 this.targetType =
108 this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0
109 ? IOSPlatform.deviceString
110 : IOSPlatform.simulatorString;
111 return;
112 }
113
114 if (
115 this.runOptions.target &&
116 this.runOptions.target !== IOSPlatform.simulatorString &&
117 this.runOptions.target !== IOSPlatform.deviceString
118 ) {
119 this.targetType = IOSPlatform.simulatorString;
120 return;
121 }
122
123 this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
124 }
125
126 public resolveVirtualDevice(target: string): Promise<IiOSSimulator | null> {
127 if (target === "simulator") {
128 return this.simulatorManager
129 .startSelection()
130 .then((simulatorName: string | undefined) => {
131 if (simulatorName) {
132 const simulator = this.simulatorManager.findSimulator(simulatorName);
133 if (simulator) {
134 GeneralMobilePlatform.removeRunArgument(
135 this.runArguments,
136 "--simulator",
137 true,
138 );
139 GeneralMobilePlatform.setRunArgument(
140 this.runArguments,
141 "--udid",
142 simulator.id,
143 );
144 }
145 return simulator;
146 } else {
147 return null;
148 }
149 });
150 } else if (!target.includes("device")) {
151 return this.simulatorManager.collectSimulators().then(simulators => {
152 let simulator = this.simulatorManager.getSimulatorById(target, simulators);
153 if (simulator) {
154 GeneralMobilePlatform.removeRunArgument(
155 this.runArguments,
156 "--simulator",
157 false,
158 );
159 GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id);
160 }
161 return null;
162 });
163 } else {
164 return Promise.resolve(null);
165 }
166 }
167
168 public runApp(): Promise<void> {
169 let extProps = {
170 platform: {
171 value: PlatformType.iOS,
172 isPii: false,
173 },
174 };
175
176 extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
177 this.runOptions,
178 this.runOptions.reactNativeVersions,
179 extProps,
180 );
181
182 return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
183 // Compile, deploy, and launch the app on either a simulator or a device
184 const env = GeneralMobilePlatform.getEnvArgument(
185 process.env,
186 this.runOptions.env,
187 this.runOptions.envFile,
188 );
189
190 if (
191 !semver.valid(
192 this.runOptions.reactNativeVersions.reactNativeVersion,
193 ) /*Custom RN implementations should support this flag*/ ||
194 semver.gte(
195 this.runOptions.reactNativeVersions.reactNativeVersion,
196 IOSPlatform.NO_PACKAGER_VERSION,
197 )
198 ) {
199 this.runArguments.push("--no-packager");
200 }
201 // Since @react-native-community/cli@2.1.0 build output are hidden by default
202 // we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns
203 if (semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, "0.60.0")) {
204 this.runArguments.push("--verbose");
205 }
206 const runIosSpawn = new CommandExecutor(
207 this.runOptions.nodeModulesRoot,
208 this.projectPath,
209 this.logger,
210 ).spawnReactCommand("run-ios", this.runArguments, { env });
211 return new OutputVerifier(
212 () =>
213 this.generateSuccessPatterns(
214 this.runOptions.reactNativeVersions.reactNativeVersion,
215 ),
216 () => Promise.resolve(IOSPlatform.RUN_IOS_FAILURE_PATTERNS),
217 PlatformType.iOS,
218 ).process(runIosSpawn);
219 });
220 }
221
222 public enableJSDebuggingMode(): Promise<void> {
223 // Configure the app for debugging
224 if (this.targetType === IOSPlatform.deviceString) {
225 // Note that currently we cannot automatically switch the device into debug mode.
226 this.logger.info(
227 "Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.",
228 );
229 return Promise.resolve();
230 }
231
232 // Wait until the configuration file exists, and check to see if debugging is enabled
233 return Promise.all<boolean | string>([
234 this.iosDebugModeManager.getAppRemoteDebuggingSetting(
235 this.runOptions.configuration,
236 this.runOptions.productName,
237 ),
238 this.getBundleId(),
239 ]).then(([debugModeEnabled, bundleId]) => {
240 if (debugModeEnabled) {
241 return Promise.resolve();
242 }
243
244 // Debugging must still be enabled
245 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
246 // but that file is written to by the app on occasion. To avoid races, we shut the app
247 // down before writing to the file.
248 const childProcess = new ChildProcess();
249
250 return childProcess
251 .execToString("xcrun simctl spawn booted launchctl list")
252 .then((output: string) => {
253 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
254 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
255 const match = regex.exec(output);
256
257 // If we don't find a match, the app must not be running and so we do not need to close it
258 return match
259 ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`)
260 : null;
261 })
262 .then(() => {
263 // Write to the settings file while the app is not running to avoid races
264 return this.iosDebugModeManager.setAppRemoteDebuggingSetting(
265 /*enable=*/ true,
266 this.runOptions.configuration,
267 this.runOptions.productName,
268 );
269 })
270 .then(() => {
271 // Relaunch the app
272 return this.runApp();
273 });
274 });
275 }
276
277 public disableJSDebuggingMode(): Promise<void> {
278 if (this.targetType === IOSPlatform.deviceString) {
279 return Promise.resolve();
280 }
281 return this.iosDebugModeManager.setAppRemoteDebuggingSetting(
282 /*enable=*/ false,
283 this.runOptions.configuration,
284 this.runOptions.productName,
285 );
286 }
287
288 public prewarmBundleCache(): Promise<void> {
289 return this.packager.prewarmBundleCache(PlatformType.iOS);
290 }
291
292 public getRunArguments(): string[] {
293 let runArguments: string[] = [];
294
295 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
296 runArguments = this.runOptions.runArguments;
297 if (this.runOptions.scheme) {
298 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(
299 runArguments,
300 "--scheme",
301 false,
302 );
303 if (!schemeFromArgs) {
304 runArguments.push("--scheme", this.runOptions.scheme);
305 } else {
306 this.logger.warning(
307 localize(
308 "iosSchemeParameterAlreadySetInRunArguments",
309 "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted",
310 ),
311 );
312 }
313 }
314 } else {
315 if (this.runOptions.target) {
316 runArguments.push(...this.handleTargetArg(this.runOptions.target));
317 }
318
319 if (this.runOptions.iosRelativeProjectPath) {
320 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
321 }
322
323 // provide any defined scheme
324 if (this.runOptions.scheme) {
325 runArguments.push("--scheme", this.runOptions.scheme);
326 }
327 }
328
329 return runArguments;
330 }
331
332 private handleTargetArg(target: string): string[] {
333 if (target === IOSPlatform.deviceString || target === IOSPlatform.simulatorString) {
334 return [`--${this.runOptions.target}`];
335 } else {
336 if (target.indexOf(IOSPlatform.deviceString) !== -1) {
337 const deviceArgs = target.split("=");
338 return deviceArgs[1]
339 ? [`--${IOSPlatform.deviceString}`, deviceArgs[1]]
340 : [`--${IOSPlatform.deviceString}`];
341 } else {
342 return [`--${IOSPlatform.simulatorString}`, `${this.runOptions.target}`];
343 }
344 }
345 }
346
347 private generateSuccessPatterns(version: string): Promise<string[]> {
348 // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
349 let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
350 if (this.targetType === IOSPlatform.deviceString) {
351 if (semver.gte(version, "0.60.0")) {
352 successPatterns.push("success Installed the app on the device");
353 } else {
354 successPatterns.push("INSTALLATION SUCCEEDED");
355 }
356 return Promise.resolve(successPatterns);
357 } else {
358 return this.getBundleId().then(bundleId => {
359 if (semver.gte(version, "0.60.0")) {
360 successPatterns.push(
361 `Launching "${bundleId}"\nsuccess Successfully launched the app `,
362 );
363 } else {
364 successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
365 }
366 return successPatterns;
367 });
368 }
369 }
370
371 private getConfiguration(): string {
372 return (
373 IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) ||
374 this.defaultConfiguration
375 );
376 }
377
378 private getBundleId(): Promise<string> {
379 let scheme = this.runOptions.scheme;
380 if (!scheme) {
381 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(
382 this.runArguments,
383 "--scheme",
384 false,
385 );
386 if (schemeFromArgs) {
387 scheme = schemeFromArgs;
388 }
389 }
390 return this.plistBuddy.getBundleId(
391 this.iosProjectRoot,
392 this.projectPath,
393 PlatformType.iOS,
394 true,
395 this.runOptions.configuration,
396 this.runOptions.productName,
397 scheme,
398 );
399 }
400
401 /*private static remote(fsPath: string): RemoteExtension { // TODO replace with a new implementation from appLauncher
402 if (this.remoteExtension) {
403 return this.remoteExtension;
404 } else {
405 return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
406 }
407 }*/
408}
409