microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.10.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

250lines · 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
3021756bYuri Skorokhodov6 years ago47private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
8a67e140Artem Egorov8 years ago48
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);
116c3cb0Ruslan Bikkinin7 years ago67const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
68this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme);
8a67e140Artem Egorov8 years ago69
db6fd42aRuslan Bikkinin7 years ago70if (this.runArguments && this.runArguments.length > 0) {
71this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
8022afdfVladimir Kotikov8 years ago72IOSPlatform.deviceString : IOSPlatform.simulatorString;
73return;
74}
0db0be15Artem Egorov8 years ago75
8022afdfVladimir Kotikov8 years ago76if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
77this.runOptions.target !== IOSPlatform.deviceString)) {
0db0be15Artem Egorov8 years ago78
8022afdfVladimir Kotikov8 years ago79this.targetType = IOSPlatform.simulatorString;
80return;
8a67e140Artem Egorov8 years ago81}
8022afdfVladimir Kotikov8 years ago82
83this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
8a67e140Artem Egorov8 years ago84}
85
86public runApp(): Q.Promise<void> {
031832ffArtem Egorov7 years ago87const extProps = {
88platform: {
89value: "ios",
90isPii: false,
91},
92};
93
94return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
95// Compile, deploy, and launch the app on either a simulator or a device
96const env = this.getEnvArgument();
97
98return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
99.then(version => {
100if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) {
db6fd42aRuslan Bikkinin7 years ago101this.runArguments.push("--no-packager");
031832ffArtem Egorov7 years ago102}
3021756bYuri Skorokhodov6 years ago103// Since @react-native-community/cli@2.1.0 build output are hidden by default
104// we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns
105if (semver.gte(version, "0.60.0")) {
106this.runArguments.push("--verbose");
107}
db6fd42aRuslan Bikkinin7 years ago108const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
3021756bYuri Skorokhodov6 years ago109return new OutputVerifier(() => this.generateSuccessPatterns(version), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
031832ffArtem Egorov7 years ago110.process(runIosSpawn);
111});
112});
8a67e140Artem Egorov8 years ago113}
114
115public enableJSDebuggingMode(): Q.Promise<void> {
116// Configure the app for debugging
117if (this.targetType === IOSPlatform.deviceString) {
118// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago119this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago120return Q.resolve<void>(void 0);
121}
122
123// Wait until the configuration file exists, and check to see if debugging is enabled
124return Q.all<boolean | string>([
db6fd42aRuslan Bikkinin7 years ago125this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
8a67e140Artem Egorov8 years ago126this.getBundleId(),
127])
128.spread((debugModeEnabled: boolean, bundleId: string) => {
129if (debugModeEnabled) {
130return Q.resolve(void 0);
131}
132
133// Debugging must still be enabled
134// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
135// but that file is written to by the app on occasion. To avoid races, we shut the app
136// down before writing to the file.
137const childProcess = new ChildProcess();
138
139return childProcess.execToString("xcrun simctl spawn booted launchctl list")
140.then((output: string) => {
141// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
142const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
143const match = regex.exec(output);
144
145// If we don't find a match, the app must not be running and so we do not need to close it
146return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
147})
148.then(() => {
149// Write to the settings file while the app is not running to avoid races
db6fd42aRuslan Bikkinin7 years ago150return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
8a67e140Artem Egorov8 years ago151})
152.then(() => {
153// Relaunch the app
154return this.runApp();
155});
156});
157}
158
0a68f8dbArtem Egorov8 years ago159public disableJSDebuggingMode(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago160return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
0a68f8dbArtem Egorov8 years ago161}
162
8a67e140Artem Egorov8 years ago163public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago164return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago165}
166
cbc7ac5bArtem Egorov7 years ago167public getRunArguments(): string[] {
8a67e140Artem Egorov8 years ago168let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago169
170if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago171runArguments = this.runOptions.runArguments;
116c3cb0Ruslan Bikkinin7 years ago172if (this.runOptions.scheme) {
173const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false);
174if (!schemeFromArgs) {
175runArguments.push("--scheme", this.runOptions.scheme);
176} else {
177this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted"));
178}
179}
b57ea017Artem Egorov8 years ago180} else {
181if (this.runOptions.target) {
182if (this.runOptions.target === IOSPlatform.deviceString ||
183this.runOptions.target === IOSPlatform.simulatorString) {
184
185runArguments.push(`--${this.runOptions.target}`);
186} else {
187runArguments.push("--simulator", `${this.runOptions.target}`);
188}
189}
8abbd163Artem Egorov8 years ago190
b57ea017Artem Egorov8 years ago191if (this.runOptions.iosRelativeProjectPath) {
192runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago193}
8a67e140Artem Egorov8 years ago194
b57ea017Artem Egorov8 years ago195// provide any defined scheme
196if (this.runOptions.scheme) {
197runArguments.push("--scheme", this.runOptions.scheme);
198}
8a67e140Artem Egorov8 years ago199}
200
201return runArguments;
202}
203
3021756bYuri Skorokhodov6 years ago204private generateSuccessPatterns(version: string): Q.Promise<string[]> {
205// Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
206let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
207if (this.targetType === IOSPlatform.deviceString) {
208if (semver.gte(version, "0.60.0")) {
209successPatterns.push("success Installed the app on the device");
210} else {
211successPatterns.push("INSTALLATION SUCCEEDED");
212}
213return Q(successPatterns);
214} else {
215return this.getBundleId()
216.then(bundleId => {
217if (semver.gte(version, "0.60.0")) {
218successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `);
219} else {
220successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
221}
222return successPatterns;
223});
224}
225
8a67e140Artem Egorov8 years ago226}
227
db6fd42aRuslan Bikkinin7 years ago228private getConfiguration(): string {
116c3cb0Ruslan Bikkinin7 years ago229return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
db6fd42aRuslan Bikkinin7 years ago230}
231
8a67e140Artem Egorov8 years ago232private getBundleId(): Q.Promise<string> {
116c3cb0Ruslan Bikkinin7 years ago233let scheme = this.runOptions.scheme;
234if (!scheme) {
235const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
236if (schemeFromArgs) {
237scheme = schemeFromArgs;
238}
239}
240return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName, scheme);
8a67e140Artem Egorov8 years ago241}
7daed3fcArtem Egorov8 years ago242
4edcda70Artem Egorov8 years ago243private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago244if (this.remoteExtension) {
245return this.remoteExtension;
246} else {
4edcda70Artem Egorov8 years ago247return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago248}
249}
8a67e140Artem Egorov8 years ago250}