microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4c757eeb398e0299d0e9cd9bc95b68dd2f87a06e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

256lines · 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 = this.getEnvArgument();
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 if (this.runOptions.target === IOSPlatform.deviceString ||
189 this.runOptions.target === IOSPlatform.simulatorString) {
190
191 runArguments.push(`--${this.runOptions.target}`);
192 } else {
193 runArguments.push("--simulator", `${this.runOptions.target}`);
194 }
195 }
196
197 if (this.runOptions.iosRelativeProjectPath) {
198 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
199 }
200
201 // provide any defined scheme
202 if (this.runOptions.scheme) {
203 runArguments.push("--scheme", this.runOptions.scheme);
204 }
205 }
206
207 return runArguments;
208 }
209
210 private generateSuccessPatterns(version: string): Q.Promise<string[]> {
211 // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
212 let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
213 if (this.targetType === IOSPlatform.deviceString) {
214 if (semver.gte(version, "0.60.0")) {
215 successPatterns.push("success Installed the app on the device");
216 } else {
217 successPatterns.push("INSTALLATION SUCCEEDED");
218 }
219 return Q(successPatterns);
220 } else {
221 return this.getBundleId()
222 .then(bundleId => {
223 if (semver.gte(version, "0.60.0")) {
224 successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `);
225 } else {
226 successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
227 }
228 return successPatterns;
229 });
230 }
231
232 }
233
234 private getConfiguration(): string {
235 return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
236 }
237
238 private getBundleId(): Q.Promise<string> {
239 let scheme = this.runOptions.scheme;
240 if (!scheme) {
241 const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
242 if (schemeFromArgs) {
243 scheme = schemeFromArgs;
244 }
245 }
246 return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme);
247 }
248
249 /*private static remote(fsPath: string): RemoteExtension { // TODO replace with a new implementation from appLauncher
250 if (this.remoteExtension) {
251 return this.remoteExtension;
252 } else {
253 return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
254 }
255 }*/
256}