microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.5.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

197lines · 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.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(
90 () =>
91 this.generateSuccessPatterns(),
92 () =>
93 Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
94 });
95 }
96
97 public enableJSDebuggingMode(): Q.Promise<void> {
98 // Configure the app for debugging
99 if (this.targetType === IOSPlatform.deviceString) {
100 // Note that currently we cannot automatically switch the device into debug mode.
101 this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
102 return Q.resolve<void>(void 0);
103 }
104
105 // Wait until the configuration file exists, and check to see if debugging is enabled
106 return Q.all<boolean | string>([
107 this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
108 this.getBundleId(),
109 ])
110 .spread((debugModeEnabled: boolean, bundleId: string) => {
111 if (debugModeEnabled) {
112 return Q.resolve(void 0);
113 }
114
115 // Debugging must still be enabled
116 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
117 // but that file is written to by the app on occasion. To avoid races, we shut the app
118 // down before writing to the file.
119 const childProcess = new ChildProcess();
120
121 return childProcess.execToString("xcrun simctl spawn booted launchctl list")
122 .then((output: string) => {
123 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
124 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
125 const match = regex.exec(output);
126
127 // If we don't find a match, the app must not be running and so we do not need to close it
128 return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
129 })
130 .then(() => {
131 // Write to the settings file while the app is not running to avoid races
132 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
133 })
134 .then(() => {
135 // Relaunch the app
136 return this.runApp();
137 });
138 });
139 }
140
141 public disableJSDebuggingMode(): Q.Promise<void> {
142 return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false);
143 }
144
145 public prewarmBundleCache(): Q.Promise<void> {
146 return this.packager.prewarmBundleCache("ios");
147 }
148
149 public getRunArgument(): string[] {
150 let runArguments: string[] = [];
151
152 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
153 runArguments = this.runOptions.runArguments;
154 } else {
155 if (this.runOptions.target) {
156 if (this.runOptions.target === IOSPlatform.deviceString ||
157 this.runOptions.target === IOSPlatform.simulatorString) {
158
159 runArguments.push(`--${this.runOptions.target}`);
160 } else {
161 runArguments.push("--simulator", `${this.runOptions.target}`);
162 }
163 }
164
165 if (this.runOptions.iosRelativeProjectPath) {
166 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
167 }
168
169 // provide any defined scheme
170 if (this.runOptions.scheme) {
171 runArguments.push("--scheme", this.runOptions.scheme);
172 }
173 }
174
175 return runArguments;
176 }
177
178 private generateSuccessPatterns(): Q.Promise<string[]> {
179 return this.targetType === IOSPlatform.deviceString ?
180 Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
181 this.getBundleId()
182 .then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
183 .concat([`Launching ${bundleId}\n${bundleId}: `]));
184 }
185
186 private getBundleId(): Q.Promise<string> {
187 return this.plistBuddy.getBundleId(this.iosProjectRoot);
188 }
189
190 private static remote(fsPath: string): RemoteExtension {
191 if (this.remoteExtension) {
192 return this.remoteExtension;
193 } else {
194 return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
195 }
196 }
197}
198