microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

213lines · 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";
a41f5c68Artem Egorov8 years ago15import {SettingsHelper} from "../settingsHelper";
7daed3fcArtem Egorov8 years ago16import {RemoteExtension} from "../../common/remoteExtension";
df8c800dArtem Egorov8 years ago17import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper";
031832ffArtem Egorov7 years ago18import {TelemetryHelper} from "../../common/telemetryHelper";
fc602bb6Yuri Skorokhodov7 years ago19import { InternalErrorCode } from "../../common/error/internalErrorCode";
d7d405aeYuri Skorokhodov7 years ago20import * as nls from "vscode-nls";
21const localize = nls.loadMessageBundle();
8022afdfVladimir Kotikov8 years ago22
8a67e140Artem Egorov8 years ago23export class IOSPlatform extends GeneralMobilePlatform {
24public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
7daed3fcArtem Egorov8 years ago25private static remoteExtension: RemoteExtension;
0a68f8dbArtem Egorov8 years ago26
8a67e140Artem Egorov8 years ago27private plistBuddy = new PlistBuddy();
8022afdfVladimir Kotikov8 years ago28private targetType: TargetType = "simulator";
0db0be15Artem Egorov8 years ago29private iosProjectRoot: string;
7daed3fcArtem Egorov8 years ago30private iosDebugModeManager: IOSDebugModeManager;
31
db6fd42aRuslan Bikkinin7 years ago32private defaultConfiguration: string = "Debug";
33private configurationArgumentName: string = "--configuration";
8a67e140Artem Egorov8 years ago34
0a68f8dbArtem Egorov8 years ago35// We should add the common iOS build/run errors we find to this list
8a67e140Artem Egorov8 years ago36private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
37pattern: "No devices are booted",
d7d405aeYuri Skorokhodov7 years ago38errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago39}, {
40pattern: "FBSOpenApplicationErrorDomain",
d7d405aeYuri Skorokhodov7 years ago41errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago42}, {
43pattern: "ios-deploy",
d7d405aeYuri Skorokhodov7 years ago44errorCode: InternalErrorCode.IOSDeployNotFound,
8a67e140Artem Egorov8 years ago45}];
46
47private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
48
db6fd42aRuslan Bikkinin7 years ago49public showDevMenu(deviceId?: string): Q.Promise<void> {
50return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId);
7daed3fcArtem Egorov8 years ago51}
52
db6fd42aRuslan Bikkinin7 years ago53public reloadApp(deviceId?: string): Q.Promise<void> {
54return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId);
7daed3fcArtem Egorov8 years ago55}
56
0a68f8dbArtem Egorov8 years ago57constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
58super(runOptions, platformDeps);
8a67e140Artem Egorov8 years ago59
db6fd42aRuslan Bikkinin7 years ago60this.runOptions.configuration = this.getConfiguration();
61
0db0be15Artem Egorov8 years ago62if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
d7d405aeYuri Skorokhodov7 years ago63this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead."));
8a67e140Artem Egorov8 years ago64}
65
0a68f8dbArtem Egorov8 years ago66this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
7daed3fcArtem Egorov8 years ago67this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
8a67e140Artem Egorov8 years ago68
db6fd42aRuslan Bikkinin7 years ago69if (this.runArguments && this.runArguments.length > 0) {
70this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
8022afdfVladimir Kotikov8 years ago71IOSPlatform.deviceString : IOSPlatform.simulatorString;
72return;
73}
0db0be15Artem Egorov8 years ago74
8022afdfVladimir Kotikov8 years ago75if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
76this.runOptions.target !== IOSPlatform.deviceString)) {
0db0be15Artem Egorov8 years ago77
8022afdfVladimir Kotikov8 years ago78this.targetType = IOSPlatform.simulatorString;
79return;
8a67e140Artem Egorov8 years ago80}
8022afdfVladimir Kotikov8 years ago81
82this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
8a67e140Artem Egorov8 years ago83}
84
85public runApp(): Q.Promise<void> {
031832ffArtem Egorov7 years ago86const extProps = {
87platform: {
88value: "ios",
89isPii: false,
90},
91};
92
93return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
94// Compile, deploy, and launch the app on either a simulator or a device
95const env = this.getEnvArgument();
96
97return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
98.then(version => {
99if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
db6fd42aRuslan Bikkinin7 years ago100this.runArguments.push("--no-packager");
031832ffArtem Egorov7 years ago101}
db6fd42aRuslan Bikkinin7 years ago102const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
031832ffArtem Egorov7 years ago103return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
104.process(runIosSpawn);
105});
106});
8a67e140Artem Egorov8 years ago107}
108
109public enableJSDebuggingMode(): Q.Promise<void> {
110// Configure the app for debugging
111if (this.targetType === IOSPlatform.deviceString) {
112// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago113this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago114return Q.resolve<void>(void 0);
115}
116
117// Wait until the configuration file exists, and check to see if debugging is enabled
118return Q.all<boolean | string>([
db6fd42aRuslan Bikkinin7 years ago119this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
8a67e140Artem Egorov8 years ago120this.getBundleId(),
121])
122.spread((debugModeEnabled: boolean, bundleId: string) => {
123if (debugModeEnabled) {
124return Q.resolve(void 0);
125}
126
127// Debugging must still be enabled
128// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
129// but that file is written to by the app on occasion. To avoid races, we shut the app
130// down before writing to the file.
131const childProcess = new ChildProcess();
132
133return childProcess.execToString("xcrun simctl spawn booted launchctl list")
134.then((output: string) => {
135// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
136const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
137const match = regex.exec(output);
138
139// If we don't find a match, the app must not be running and so we do not need to close it
140return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
141})
142.then(() => {
143// Write to the settings file while the app is not running to avoid races
db6fd42aRuslan Bikkinin7 years ago144return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
8a67e140Artem Egorov8 years ago145})
146.then(() => {
147// Relaunch the app
148return this.runApp();
149});
150});
151}
152
0a68f8dbArtem Egorov8 years ago153public disableJSDebuggingMode(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago154return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
0a68f8dbArtem Egorov8 years ago155}
156
8a67e140Artem Egorov8 years ago157public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago158return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago159}
160
cbc7ac5bArtem Egorov7 years ago161public getRunArguments(): string[] {
8a67e140Artem Egorov8 years ago162let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago163
164if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago165runArguments = this.runOptions.runArguments;
166} else {
167if (this.runOptions.target) {
168if (this.runOptions.target === IOSPlatform.deviceString ||
169this.runOptions.target === IOSPlatform.simulatorString) {
170
171runArguments.push(`--${this.runOptions.target}`);
172} else {
173runArguments.push("--simulator", `${this.runOptions.target}`);
174}
175}
8abbd163Artem Egorov8 years ago176
b57ea017Artem Egorov8 years ago177if (this.runOptions.iosRelativeProjectPath) {
178runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago179}
8a67e140Artem Egorov8 years ago180
b57ea017Artem Egorov8 years ago181// provide any defined scheme
182if (this.runOptions.scheme) {
183runArguments.push("--scheme", this.runOptions.scheme);
184}
8a67e140Artem Egorov8 years ago185}
186
187return runArguments;
188}
189
190private generateSuccessPatterns(): Q.Promise<string[]> {
ed8367fdVladimir Kotikov8 years ago191return this.targetType === IOSPlatform.deviceString ?
192Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
193this.getBundleId()
194.then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
195.concat([`Launching ${bundleId}\n${bundleId}: `]));
8a67e140Artem Egorov8 years ago196}
197
db6fd42aRuslan Bikkinin7 years ago198private getConfiguration(): string {
199return this.getOptFromRunArgs(this.configurationArgumentName) || this.defaultConfiguration;
200}
201
8a67e140Artem Egorov8 years ago202private getBundleId(): Q.Promise<string> {
db6fd42aRuslan Bikkinin7 years ago203return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName);
8a67e140Artem Egorov8 years ago204}
7daed3fcArtem Egorov8 years ago205
4edcda70Artem Egorov8 years ago206private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago207if (this.remoteExtension) {
208return this.remoteExtension;
209} else {
4edcda70Artem Egorov8 years ago210return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago211}
212}
8a67e140Artem Egorov8 years ago213}