microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.14

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

212lines · 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 {ErrorHelper} from "../../common/error/errorHelper";
16import {SettingsHelper} from "../settingsHelper";
17import {RemoteExtension} from "../../common/remoteExtension";
18import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper";
19import {TelemetryHelper} from "../../common/telemetryHelper";
20
21export class IOSPlatform extends GeneralMobilePlatform {
22 public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
23 private static remoteExtension: RemoteExtension;
24
25 private plistBuddy = new PlistBuddy();
26 private targetType: TargetType = "simulator";
27 private iosProjectRoot: string;
28 private iosDebugModeManager: IOSDebugModeManager;
29
30 private defaultConfiguration: string = "Debug";
31 private configurationArgumentName: string = "--configuration";
32
33 // We should add the common iOS build/run errors we find to this list
34 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
35 pattern: "No devices are booted",
36 message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
37 }, {
38 pattern: "FBSOpenApplicationErrorDomain",
39 message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
40 }, {
41 pattern: "ios-deploy",
42 message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound,
43 }];
44
45 private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
46
47 public showDevMenu(deviceId?: string): Q.Promise<void> {
48 return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId);
49 }
50
51 public reloadApp(deviceId?: string): Q.Promise<void> {
52 return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId);
53 }
54
55 constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
56 super(runOptions, platformDeps);
57
58
59 this.runOptions.configuration = this.getConfiguration();
60
61 if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
62 this.logger.warning("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead");
63 }
64
65 this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
66 this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
67
68 if (this.runArguments && this.runArguments.length > 0) {
69 this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
70 IOSPlatform.deviceString : IOSPlatform.simulatorString;
71 return;
72 }
73
74 if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
75 this.runOptions.target !== IOSPlatform.deviceString)) {
76
77 this.targetType = IOSPlatform.simulatorString;
78 return;
79 }
80
81 this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
82 }
83
84 public runApp(): Q.Promise<void> {
85 const extProps = {
86 platform: {
87 value: "ios",
88 isPii: false,
89 },
90 };
91
92 return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
93 // Compile, deploy, and launch the app on either a simulator or a device
94 const env = this.getEnvArgument();
95
96 return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
97 .then(version => {
98 if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
99 this.runArguments.push("--no-packager");
100 }
101 const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
102 return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
103 .process(runIosSpawn);
104 });
105 });
106 }
107
108 public enableJSDebuggingMode(): Q.Promise<void> {
109 // Configure the app for debugging
110 if (this.targetType === IOSPlatform.deviceString) {
111 // Note that currently we cannot automatically switch the device into debug mode.
112 this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
113 return Q.resolve<void>(void 0);
114 }
115
116 // Wait until the configuration file exists, and check to see if debugging is enabled
117 return Q.all<boolean | string>([
118 this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
119 this.getBundleId(),
120 ])
121 .spread((debugModeEnabled: boolean, bundleId: string) => {
122 if (debugModeEnabled) {
123 return Q.resolve(void 0);
124 }
125
126 // Debugging must still be enabled
127 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
128 // but that file is written to by the app on occasion. To avoid races, we shut the app
129 // down before writing to the file.
130 const childProcess = new ChildProcess();
131
132 return childProcess.execToString("xcrun simctl spawn booted launchctl list")
133 .then((output: string) => {
134 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
135 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
136 const match = regex.exec(output);
137
138 // If we don't find a match, the app must not be running and so we do not need to close it
139 return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
140 })
141 .then(() => {
142 // Write to the settings file while the app is not running to avoid races
143 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
144 })
145 .then(() => {
146 // Relaunch the app
147 return this.runApp();
148 });
149 });
150 }
151
152 public disableJSDebuggingMode(): Q.Promise<void> {
153 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
154 }
155
156 public prewarmBundleCache(): Q.Promise<void> {
157 return this.packager.prewarmBundleCache("ios");
158 }
159
160 protected getRunArguments(): string[] {
161 let runArguments: string[] = [];
162
163 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
164 runArguments = this.runOptions.runArguments;
165 } else {
166 if (this.runOptions.target) {
167 if (this.runOptions.target === IOSPlatform.deviceString ||
168 this.runOptions.target === IOSPlatform.simulatorString) {
169
170 runArguments.push(`--${this.runOptions.target}`);
171 } else {
172 runArguments.push("--simulator", `${this.runOptions.target}`);
173 }
174 }
175
176 if (this.runOptions.iosRelativeProjectPath) {
177 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
178 }
179
180 // provide any defined scheme
181 if (this.runOptions.scheme) {
182 runArguments.push("--scheme", this.runOptions.scheme);
183 }
184 }
185
186 return runArguments;
187 }
188
189 private generateSuccessPatterns(): Q.Promise<string[]> {
190 return this.targetType === IOSPlatform.deviceString ?
191 Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
192 this.getBundleId()
193 .then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
194 .concat([`Launching ${bundleId}\n${bundleId}: `]));
195 }
196
197 private getConfiguration(): string {
198 return this.getOptFromRunArgs(this.configurationArgumentName) || this.defaultConfiguration;
199 }
200
201 private getBundleId(): Q.Promise<string> {
202 return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName);
203 }
204
205 private static remote(fsPath: string): RemoteExtension {
206 if (this.remoteExtension) {
207 return this.remoteExtension;
208 } else {
209 return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
210 }
211 }
212}
213