microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0bfa4e58a91c5a692df46886a7dbb23ba6c1be3d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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