microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.5.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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