microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4aec730e44bfea8933accd82b72fab4853eadbd0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

253lines · 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 let extProps = {
89 platform: {
90 value: "ios",
91 isPii: false,
92 },
93 };
94
95 extProps = TelemetryHelper.addReactNativeVersionToEventProperties(this.runOptions.reactNativeVersion, extProps);
96
97 return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
98 // Compile, deploy, and launch the app on either a simulator or a device
99 const env = this.getEnvArgument();
100
101 return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
102 .then(version => {
103 if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
104 this.runArguments.push("--no-packager");
105 }
106 // Since @react-native-community/cli@2.1.0 build output are hidden by default
107 // we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns
108 if (semver.gte(version, "0.60.0")) {
109 this.runArguments.push("--verbose");
110 }
111 const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
112 return new OutputVerifier(() => this.generateSuccessPatterns(version), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
113 .process(runIosSpawn);
114 });
115 });
116 }
117
118 public enableJSDebuggingMode(): Q.Promise<void> {
119 // Configure the app for debugging
120 if (this.targetType === IOSPlatform.deviceString) {
121 // Note that currently we cannot automatically switch the device into debug mode.
122 this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
123 return Q.resolve<void>(void 0);
124 }
125
126 // Wait until the configuration file exists, and check to see if debugging is enabled
127 return Q.all<boolean | string>([
128 this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
129 this.getBundleId(),
130 ])
131 .spread((debugModeEnabled: boolean, bundleId: string) => {
132 if (debugModeEnabled) {
133 return Q.resolve(void 0);
134 }
135
136 // Debugging must still be enabled
137 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
138 // but that file is written to by the app on occasion. To avoid races, we shut the app
139 // down before writing to the file.
140 const childProcess = new ChildProcess();
141
142 return childProcess.execToString("xcrun simctl spawn booted launchctl list")
143 .then((output: string) => {
144 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
145 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
146 const match = regex.exec(output);
147
148 // If we don't find a match, the app must not be running and so we do not need to close it
149 return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
150 })
151 .then(() => {
152 // Write to the settings file while the app is not running to avoid races
153 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
154 })
155 .then(() => {
156 // Relaunch the app
157 return this.runApp();
158 });
159 });
160 }
161
162 public disableJSDebuggingMode(): Q.Promise<void> {
163 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
164 }
165
166 public prewarmBundleCache(): Q.Promise<void> {
167 return this.packager.prewarmBundleCache("ios");
168 }
169
170 public getRunArguments(): string[] {
171 let runArguments: string[] = [];
172
173 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
174 runArguments = this.runOptions.runArguments;
175 if (this.runOptions.scheme) {
176 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false);
177 if (!schemeFromArgs) {
178 runArguments.push("--scheme", this.runOptions.scheme);
179 } else {
180 this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted"));
181 }
182 }
183 } else {
184 if (this.runOptions.target) {
185 if (this.runOptions.target === IOSPlatform.deviceString ||
186 this.runOptions.target === IOSPlatform.simulatorString) {
187
188 runArguments.push(`--${this.runOptions.target}`);
189 } else {
190 runArguments.push("--simulator", `${this.runOptions.target}`);
191 }
192 }
193
194 if (this.runOptions.iosRelativeProjectPath) {
195 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
196 }
197
198 // provide any defined scheme
199 if (this.runOptions.scheme) {
200 runArguments.push("--scheme", this.runOptions.scheme);
201 }
202 }
203
204 return runArguments;
205 }
206
207 private generateSuccessPatterns(version: string): Q.Promise<string[]> {
208 // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
209 let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
210 if (this.targetType === IOSPlatform.deviceString) {
211 if (semver.gte(version, "0.60.0")) {
212 successPatterns.push("success Installed the app on the device");
213 } else {
214 successPatterns.push("INSTALLATION SUCCEEDED");
215 }
216 return Q(successPatterns);
217 } else {
218 return this.getBundleId()
219 .then(bundleId => {
220 if (semver.gte(version, "0.60.0")) {
221 successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `);
222 } else {
223 successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
224 }
225 return successPatterns;
226 });
227 }
228
229 }
230
231 private getConfiguration(): string {
232 return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
233 }
234
235 private getBundleId(): Q.Promise<string> {
236 let scheme = this.runOptions.scheme;
237 if (!scheme) {
238 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
239 if (schemeFromArgs) {
240 scheme = schemeFromArgs;
241 }
242 }
243 return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme);
244 }
245
246 private static remote(fsPath: string): RemoteExtension {
247 if (this.remoteExtension) {
248 return this.remoteExtension;
249 } else {
250 return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
251 }
252 }
253}
254