microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
43e1a99628463d630522ffd044ae583fe85de4b3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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