microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

197lines · 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();
1174ee3dArtem Egorov8 years ago81const env = this.getEnvArgument();
8a67e140Artem Egorov8 years ago82
df8c800dArtem Egorov8 years ago83return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
84.then(version => {
85if (semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
86runArguments.push("--no-packager");
87}
1174ee3dArtem Egorov8 years ago88const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", runArguments, {env});
df8c800dArtem Egorov8 years ago89return new OutputVerifier(
90() =>
91this.generateSuccessPatterns(),
92() =>
93Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
94});
8a67e140Artem Egorov8 years ago95}
96
97public enableJSDebuggingMode(): Q.Promise<void> {
98// Configure the app for debugging
99if (this.targetType === IOSPlatform.deviceString) {
100// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago101this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago102return Q.resolve<void>(void 0);
103}
104
105// Wait until the configuration file exists, and check to see if debugging is enabled
106return Q.all<boolean | string>([
0a68f8dbArtem Egorov8 years ago107this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
8a67e140Artem Egorov8 years ago108this.getBundleId(),
109])
110.spread((debugModeEnabled: boolean, bundleId: string) => {
111if (debugModeEnabled) {
112return 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.
119const childProcess = new ChildProcess();
120
121return 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]
124const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
125const 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
128return 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
0a68f8dbArtem Egorov8 years ago132return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
8a67e140Artem Egorov8 years ago133})
134.then(() => {
135// Relaunch the app
136return this.runApp();
137});
138});
139}
140
0a68f8dbArtem Egorov8 years ago141public disableJSDebuggingMode(): Q.Promise<void> {
142return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false);
143}
144
8a67e140Artem Egorov8 years ago145public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago146return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago147}
148
149public getRunArgument(): string[] {
150let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago151
152if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago153runArguments = this.runOptions.runArguments;
154} else {
155if (this.runOptions.target) {
156if (this.runOptions.target === IOSPlatform.deviceString ||
157this.runOptions.target === IOSPlatform.simulatorString) {
158
159runArguments.push(`--${this.runOptions.target}`);
160} else {
161runArguments.push("--simulator", `${this.runOptions.target}`);
162}
163}
8abbd163Artem Egorov8 years ago164
b57ea017Artem Egorov8 years ago165if (this.runOptions.iosRelativeProjectPath) {
166runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago167}
8a67e140Artem Egorov8 years ago168
b57ea017Artem Egorov8 years ago169// provide any defined scheme
170if (this.runOptions.scheme) {
171runArguments.push("--scheme", this.runOptions.scheme);
172}
8a67e140Artem Egorov8 years ago173}
174
175return runArguments;
176}
177
178private generateSuccessPatterns(): Q.Promise<string[]> {
ed8367fdVladimir Kotikov8 years ago179return this.targetType === IOSPlatform.deviceString ?
180Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
181this.getBundleId()
182.then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
183.concat([`Launching ${bundleId}\n${bundleId}: `]));
8a67e140Artem Egorov8 years ago184}
185
186private getBundleId(): Q.Promise<string> {
0db0be15Artem Egorov8 years ago187return this.plistBuddy.getBundleId(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago188}
7daed3fcArtem Egorov8 years ago189
4edcda70Artem Egorov8 years ago190private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago191if (this.remoteExtension) {
192return this.remoteExtension;
193} else {
4edcda70Artem Egorov8 years ago194return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago195}
196}
8a67e140Artem Egorov8 years ago197}