microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/ios/iOSPlatform.ts

167lines · 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";
6
7import {Log} from "../../common/log/log";
8import {ChildProcess} from "../../common/node/childProcess";
9import {CommandExecutor} from "../../common/commandExecutor";
1ca47c7cArtem Egorov8 years ago10import {GeneralMobilePlatform, TargetType} from "../../common/generalMobilePlatform";
0db0be15Artem Egorov8 years ago11import {IIOSRunOptions} from "../../common/launchArgs";
8a67e140Artem Egorov8 years ago12import {PlistBuddy} from "../../common/ios/plistBuddy";
13import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
14import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
15import {RemoteExtension} from "../../common/remoteExtension";
8022afdfVladimir Kotikov8 years ago16import { ErrorHelper } from "../../common/error/errorHelper";
17
8a67e140Artem Egorov8 years ago18export class IOSPlatform extends GeneralMobilePlatform {
19public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
20public static DEFAULT_IOS_SIMULATOR_TARGET = "iPhone 5";
21private plistBuddy = new PlistBuddy();
8022afdfVladimir Kotikov8 years ago22private targetType: TargetType = "simulator";
0db0be15Artem Egorov8 years ago23private iosProjectRoot: string;
8a67e140Artem Egorov8 years ago24
25// We should add the common iOS build/run erros we find to this list
26private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
27pattern: "No devices are booted",
3c172a05Artem Egorov8 years ago28message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago29}, {
30pattern: "FBSOpenApplicationErrorDomain",
3c172a05Artem Egorov8 years ago31message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago32}, {
33pattern: "ios-deploy",
3c172a05Artem Egorov8 years ago34message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound,
8a67e140Artem Egorov8 years ago35}];
36
37private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
38
39// We set remoteExtension = null so that if there is an instance of iOSPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests.
8022afdfVladimir Kotikov8 years ago40constructor(protected runOptions: IIOSRunOptions, { remoteExtension = undefined }: { remoteExtension?: RemoteExtension } = {}) {
8a67e140Artem Egorov8 years ago41super(runOptions, { remoteExtension: remoteExtension });
42
0db0be15Artem Egorov8 years ago43if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
44Log.logMessage("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead");
8a67e140Artem Egorov8 years ago45}
46
ed8367fdVladimir Kotikov8 years ago47this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || "ios");
8a67e140Artem Egorov8 years ago48
0db0be15Artem Egorov8 years ago49if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
8022afdfVladimir Kotikov8 years ago50this.targetType = (this.runOptions.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
51IOSPlatform.deviceString : IOSPlatform.simulatorString;
52return;
53}
0db0be15Artem Egorov8 years ago54
8022afdfVladimir Kotikov8 years ago55if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
56this.runOptions.target !== IOSPlatform.deviceString)) {
0db0be15Artem Egorov8 years ago57
8022afdfVladimir Kotikov8 years ago58this.targetType = IOSPlatform.simulatorString;
59return;
8a67e140Artem Egorov8 years ago60}
8022afdfVladimir Kotikov8 years ago61
62this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
8a67e140Artem Egorov8 years ago63}
64
65public runApp(): Q.Promise<void> {
66// Compile, deploy, and launch the app on either a simulator or a device
67const runArguments = this.getRunArgument();
68
69const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
70return new OutputVerifier(
71() =>
72this.generateSuccessPatterns(),
73() =>
74Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
75}
76
77public enableJSDebuggingMode(): Q.Promise<void> {
78// Configure the app for debugging
79if (this.targetType === IOSPlatform.deviceString) {
80// Note that currently we cannot automatically switch the device into debug mode.
81Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
82return Q.resolve<void>(void 0);
83}
84
0db0be15Artem Egorov8 years ago85const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago86
87// Wait until the configuration file exists, and check to see if debugging is enabled
88return Q.all<boolean | string>([
89iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
90this.getBundleId(),
91])
92.spread((debugModeEnabled: boolean, bundleId: string) => {
93if (debugModeEnabled) {
94return Q.resolve(void 0);
95}
96
97// Debugging must still be enabled
98// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
99// but that file is written to by the app on occasion. To avoid races, we shut the app
100// down before writing to the file.
101const childProcess = new ChildProcess();
102
103return childProcess.execToString("xcrun simctl spawn booted launchctl list")
104.then((output: string) => {
105// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
106const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
107const match = regex.exec(output);
108
109// If we don't find a match, the app must not be running and so we do not need to close it
110return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
111})
112.then(() => {
113// Write to the settings file while the app is not running to avoid races
114return iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
115})
116.then(() => {
117// Relaunch the app
118return this.runApp();
119});
120});
121}
122
123public prewarmBundleCache(): Q.Promise<void> {
124return this.remoteExtension.prewarmBundleCache(this.platformName);
125}
126
127public getRunArgument(): string[] {
128let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago129
130if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
131return this.runOptions.runArguments;
132}
133
8022afdfVladimir Kotikov8 years ago134if (this.runOptions.target) {
135if (this.runOptions.target === IOSPlatform.deviceString ||
136this.runOptions.target === IOSPlatform.simulatorString) {
8abbd163Artem Egorov8 years ago137
8022afdfVladimir Kotikov8 years ago138runArguments.push(`--${this.runOptions.target}`);
139} else {
140runArguments.push("--simulator", `${this.runOptions.target}`);
141}
8a67e140Artem Egorov8 years ago142}
143
144if (this.runOptions.iosRelativeProjectPath) {
145runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
146}
147
148// provide any defined scheme
149if (this.runOptions.scheme) {
150runArguments.push("--scheme", this.runOptions.scheme);
151}
152
153return runArguments;
154}
155
156private generateSuccessPatterns(): Q.Promise<string[]> {
ed8367fdVladimir Kotikov8 years ago157return this.targetType === IOSPlatform.deviceString ?
158Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
159this.getBundleId()
160.then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
161.concat([`Launching ${bundleId}\n${bundleId}: `]));
8a67e140Artem Egorov8 years ago162}
163
164private getBundleId(): Q.Promise<string> {
0db0be15Artem Egorov8 years ago165return this.plistBuddy.getBundleId(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago166}
167}