microsoft/vscode-react-native

Public

mirrored from https://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 · modeblame

8a67e140Artem Egorov8 years ago1// 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";
df8c800dArtem Egorov8 years ago6import * as semver from "semver";
8a67e140Artem Egorov8 years ago7
8import {ChildProcess} from "../../common/node/childProcess";
9import {CommandExecutor} from "../../common/commandExecutor";
0a68f8dbArtem Egorov8 years ago10import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform";
11import {IIOSRunOptions} from "../launchArgs";
12import {PlistBuddy} from "./plistBuddy";
13import {IOSDebugModeManager} from "./iOSDebugModeManager";
8a67e140Artem Egorov8 years ago14import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
0a68f8dbArtem Egorov8 years ago15import {ErrorHelper} from "../../common/error/errorHelper";
a41f5c68Artem Egorov8 years ago16import {SettingsHelper} from "../settingsHelper";
7daed3fcArtem Egorov8 years ago17import {RemoteExtension} from "../../common/remoteExtension";
df8c800dArtem Egorov8 years ago18import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper";
8022afdfVladimir Kotikov8 years ago19
8a67e140Artem Egorov8 years ago20export class IOSPlatform extends GeneralMobilePlatform {
21public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
7daed3fcArtem Egorov8 years ago22private static remoteExtension: RemoteExtension;
0a68f8dbArtem Egorov8 years ago23
8a67e140Artem Egorov8 years ago24private plistBuddy = new PlistBuddy();
8022afdfVladimir Kotikov8 years ago25private targetType: TargetType = "simulator";
0db0be15Artem Egorov8 years ago26private iosProjectRoot: string;
7daed3fcArtem Egorov8 years ago27private iosDebugModeManager: IOSDebugModeManager;
28
8a67e140Artem Egorov8 years ago29
0a68f8dbArtem Egorov8 years ago30// We should add the common iOS build/run errors we find to this list
8a67e140Artem Egorov8 years ago31private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
32pattern: "No devices are booted",
3c172a05Artem Egorov8 years ago33message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago34}, {
35pattern: "FBSOpenApplicationErrorDomain",
3c172a05Artem Egorov8 years ago36message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago37}, {
38pattern: "ios-deploy",
3c172a05Artem Egorov8 years ago39message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound,
8a67e140Artem Egorov8 years ago40}];
41
42private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
43
4edcda70Artem Egorov8 years ago44public static showDevMenu(fsPath: string, deviceId?: string): Q.Promise<void> {
45return this.remote(fsPath).showDevMenu(deviceId);
7daed3fcArtem Egorov8 years ago46}
47
4edcda70Artem Egorov8 years ago48public static reloadApp(fsPath: string, deviceId?: string): Q.Promise<void> {
49return this.remote(fsPath).reloadApp(deviceId);
7daed3fcArtem Egorov8 years ago50}
51
0a68f8dbArtem Egorov8 years ago52constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
53super(runOptions, platformDeps);
8a67e140Artem Egorov8 years ago54
0db0be15Artem Egorov8 years ago55if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
0a68f8dbArtem Egorov8 years ago56this.logger.warning("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead");
8a67e140Artem Egorov8 years ago57}
58
0a68f8dbArtem Egorov8 years ago59this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
7daed3fcArtem Egorov8 years ago60this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago61
0db0be15Artem Egorov8 years ago62if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
8022afdfVladimir Kotikov8 years ago63this.targetType = (this.runOptions.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
64IOSPlatform.deviceString : IOSPlatform.simulatorString;
65return;
66}
0db0be15Artem Egorov8 years ago67
8022afdfVladimir Kotikov8 years ago68if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
69this.runOptions.target !== IOSPlatform.deviceString)) {
0db0be15Artem Egorov8 years ago70
8022afdfVladimir Kotikov8 years ago71this.targetType = IOSPlatform.simulatorString;
72return;
8a67e140Artem Egorov8 years ago73}
8022afdfVladimir Kotikov8 years ago74
75this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
8a67e140Artem Egorov8 years ago76}
77
78public runApp(): Q.Promise<void> {
79// Compile, deploy, and launch the app on either a simulator or a device
80const runArguments = this.getRunArgument();
81
df8c800dArtem Egorov8 years ago82return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
83.then(version => {
84if (semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
85runArguments.push("--no-packager");
86}
87const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", runArguments);
88return new OutputVerifier(
89() =>
90this.generateSuccessPatterns(),
91() =>
92Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
93});
8a67e140Artem Egorov8 years ago94}
95
96public enableJSDebuggingMode(): Q.Promise<void> {
97// Configure the app for debugging
98if (this.targetType === IOSPlatform.deviceString) {
99// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago100this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago101return Q.resolve<void>(void 0);
102}
103
104// Wait until the configuration file exists, and check to see if debugging is enabled
105return Q.all<boolean | string>([
0a68f8dbArtem Egorov8 years ago106this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
8a67e140Artem Egorov8 years ago107this.getBundleId(),
108])
109.spread((debugModeEnabled: boolean, bundleId: string) => {
110if (debugModeEnabled) {
111return 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.
118const childProcess = new ChildProcess();
119
120return 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]
123const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
124const 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
127return 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
0a68f8dbArtem Egorov8 years ago131return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
8a67e140Artem Egorov8 years ago132})
133.then(() => {
134// Relaunch the app
135return this.runApp();
136});
137});
138}
139
0a68f8dbArtem Egorov8 years ago140public disableJSDebuggingMode(): Q.Promise<void> {
141return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false);
142}
143
8a67e140Artem Egorov8 years ago144public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago145return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago146}
147
148public getRunArgument(): string[] {
149let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago150
151if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago152runArguments = this.runOptions.runArguments;
153} else {
154if (this.runOptions.target) {
155if (this.runOptions.target === IOSPlatform.deviceString ||
156this.runOptions.target === IOSPlatform.simulatorString) {
157
158runArguments.push(`--${this.runOptions.target}`);
159} else {
160runArguments.push("--simulator", `${this.runOptions.target}`);
161}
162}
8abbd163Artem Egorov8 years ago163
b57ea017Artem Egorov8 years ago164if (this.runOptions.iosRelativeProjectPath) {
165runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago166}
8a67e140Artem Egorov8 years ago167
b57ea017Artem Egorov8 years ago168// provide any defined scheme
169if (this.runOptions.scheme) {
170runArguments.push("--scheme", this.runOptions.scheme);
171}
8a67e140Artem Egorov8 years ago172}
173
174return runArguments;
175}
176
177private generateSuccessPatterns(): Q.Promise<string[]> {
ed8367fdVladimir Kotikov8 years ago178return this.targetType === IOSPlatform.deviceString ?
179Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
180this.getBundleId()
181.then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
182.concat([`Launching ${bundleId}\n${bundleId}: `]));
8a67e140Artem Egorov8 years ago183}
184
185private getBundleId(): Q.Promise<string> {
0db0be15Artem Egorov8 years ago186return this.plistBuddy.getBundleId(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago187}
7daed3fcArtem Egorov8 years ago188
4edcda70Artem Egorov8 years ago189private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago190if (this.remoteExtension) {
191return this.remoteExtension;
192} else {
4edcda70Artem Egorov8 years ago193return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago194}
195}
8a67e140Artem Egorov8 years ago196}