microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.16

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

204lines · 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";
031832ffArtem Egorov8 years ago19import {TelemetryHelper} from "../../common/telemetryHelper";
8022afdfVladimir Kotikov8 years ago20
8a67e140Artem Egorov8 years ago21export class IOSPlatform extends GeneralMobilePlatform {
22public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
7daed3fcArtem Egorov8 years ago23private static remoteExtension: RemoteExtension;
0a68f8dbArtem Egorov8 years ago24
8a67e140Artem Egorov8 years ago25private plistBuddy = new PlistBuddy();
8022afdfVladimir Kotikov8 years ago26private targetType: TargetType = "simulator";
0db0be15Artem Egorov8 years ago27private iosProjectRoot: string;
7daed3fcArtem Egorov8 years ago28private iosDebugModeManager: IOSDebugModeManager;
29
8a67e140Artem Egorov8 years ago30
0a68f8dbArtem Egorov8 years ago31// We should add the common iOS build/run errors we find to this list
8a67e140Artem Egorov8 years ago32private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
33pattern: "No devices are booted",
3c172a05Artem Egorov8 years ago34message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago35}, {
36pattern: "FBSOpenApplicationErrorDomain",
3c172a05Artem Egorov8 years ago37message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago38}, {
39pattern: "ios-deploy",
3c172a05Artem Egorov8 years ago40message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound,
8a67e140Artem Egorov8 years ago41}];
42
43private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
44
634e3ecaRuslan Bikkinin7 years ago45public static showDevMenu(fsPath: string, deviceId?: string): Q.Promise<void> {
46return this.remote(fsPath).showDevMenu(deviceId);
7daed3fcArtem Egorov8 years ago47}
48
634e3ecaRuslan Bikkinin7 years ago49public static reloadApp(fsPath: string, deviceId?: string): Q.Promise<void> {
50return this.remote(fsPath).reloadApp(deviceId);
7daed3fcArtem Egorov8 years ago51}
52
0a68f8dbArtem Egorov8 years ago53constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
54super(runOptions, platformDeps);
8a67e140Artem Egorov8 years ago55
0db0be15Artem Egorov8 years ago56if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
0a68f8dbArtem Egorov8 years ago57this.logger.warning("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead");
8a67e140Artem Egorov8 years ago58}
59
0a68f8dbArtem Egorov8 years ago60this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
7daed3fcArtem Egorov8 years ago61this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago62
634e3ecaRuslan Bikkinin7 years ago63if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
64this.targetType = (this.runOptions.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
8022afdfVladimir Kotikov8 years ago65IOSPlatform.deviceString : IOSPlatform.simulatorString;
66return;
67}
0db0be15Artem Egorov8 years ago68
8022afdfVladimir Kotikov8 years ago69if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
70this.runOptions.target !== IOSPlatform.deviceString)) {
0db0be15Artem Egorov8 years ago71
8022afdfVladimir Kotikov8 years ago72this.targetType = IOSPlatform.simulatorString;
73return;
8a67e140Artem Egorov8 years ago74}
8022afdfVladimir Kotikov8 years ago75
76this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
8a67e140Artem Egorov8 years ago77}
78
79public runApp(): Q.Promise<void> {
031832ffArtem Egorov8 years ago80const extProps = {
81platform: {
82value: "ios",
83isPii: false,
84},
85};
86
87return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
88// Compile, deploy, and launch the app on either a simulator or a device
634e3ecaRuslan Bikkinin7 years ago89const runArguments = this.getRunArgument();
031832ffArtem Egorov8 years ago90const env = this.getEnvArgument();
91
92return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
93.then(version => {
94if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
634e3ecaRuslan Bikkinin7 years ago95runArguments.push("--no-packager");
031832ffArtem Egorov8 years ago96}
634e3ecaRuslan Bikkinin7 years ago97const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", runArguments, {env});
031832ffArtem Egorov8 years ago98return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
99.process(runIosSpawn);
100});
101});
8a67e140Artem Egorov8 years ago102}
103
104public enableJSDebuggingMode(): Q.Promise<void> {
105// Configure the app for debugging
106if (this.targetType === IOSPlatform.deviceString) {
107// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago108this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago109return Q.resolve<void>(void 0);
110}
111
112// Wait until the configuration file exists, and check to see if debugging is enabled
113return Q.all<boolean | string>([
634e3ecaRuslan Bikkinin7 years ago114this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
8a67e140Artem Egorov8 years ago115this.getBundleId(),
116])
117.spread((debugModeEnabled: boolean, bundleId: string) => {
118if (debugModeEnabled) {
119return Q.resolve(void 0);
120}
121
122// Debugging must still be enabled
123// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
124// but that file is written to by the app on occasion. To avoid races, we shut the app
125// down before writing to the file.
126const childProcess = new ChildProcess();
127
128return childProcess.execToString("xcrun simctl spawn booted launchctl list")
129.then((output: string) => {
130// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
131const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
132const match = regex.exec(output);
133
134// If we don't find a match, the app must not be running and so we do not need to close it
135return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
136})
137.then(() => {
138// Write to the settings file while the app is not running to avoid races
634e3ecaRuslan Bikkinin7 years ago139return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
8a67e140Artem Egorov8 years ago140})
141.then(() => {
142// Relaunch the app
143return this.runApp();
144});
145});
146}
147
0a68f8dbArtem Egorov8 years ago148public disableJSDebuggingMode(): Q.Promise<void> {
634e3ecaRuslan Bikkinin7 years ago149return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false);
0a68f8dbArtem Egorov8 years ago150}
151
8a67e140Artem Egorov8 years ago152public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago153return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago154}
155
634e3ecaRuslan Bikkinin7 years ago156public getRunArgument(): string[] {
8a67e140Artem Egorov8 years ago157let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago158
159if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago160runArguments = this.runOptions.runArguments;
161} else {
162if (this.runOptions.target) {
163if (this.runOptions.target === IOSPlatform.deviceString ||
164this.runOptions.target === IOSPlatform.simulatorString) {
165
166runArguments.push(`--${this.runOptions.target}`);
167} else {
168runArguments.push("--simulator", `${this.runOptions.target}`);
169}
170}
8abbd163Artem Egorov8 years ago171
b57ea017Artem Egorov8 years ago172if (this.runOptions.iosRelativeProjectPath) {
173runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago174}
8a67e140Artem Egorov8 years ago175
b57ea017Artem Egorov8 years ago176// provide any defined scheme
177if (this.runOptions.scheme) {
178runArguments.push("--scheme", this.runOptions.scheme);
179}
8a67e140Artem Egorov8 years ago180}
181
182return runArguments;
183}
184
185private generateSuccessPatterns(): Q.Promise<string[]> {
ed8367fdVladimir Kotikov8 years ago186return this.targetType === IOSPlatform.deviceString ?
187Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
188this.getBundleId()
189.then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
190.concat([`Launching ${bundleId}\n${bundleId}: `]));
8a67e140Artem Egorov8 years ago191}
192
193private getBundleId(): Q.Promise<string> {
634e3ecaRuslan Bikkinin7 years ago194return this.plistBuddy.getBundleId(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago195}
7daed3fcArtem Egorov8 years ago196
4edcda70Artem Egorov8 years ago197private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago198if (this.remoteExtension) {
199return this.remoteExtension;
200} else {
4edcda70Artem Egorov8 years ago201return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago202}
203}
8a67e140Artem Egorov8 years ago204}