microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

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