microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f521686613075eb4a9cebc89a01cbcf289ec258f

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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