microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
14ebf4e6fdee5f69a41d9a7deea4dc164dd28b7e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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