microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.15

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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