microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
10bc5320aeda86f9f9aee59bb732e61314aaad7e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

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