microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.14.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

249lines · 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";
031832ffArtem Egorov8 years ago17import {TelemetryHelper} from "../../common/telemetryHelper";
fc602bb6Yuri Skorokhodov7 years ago18import { InternalErrorCode } from "../../common/error/internalErrorCode";
d7d405aeYuri Skorokhodov7 years ago19import * as nls from "vscode-nls";
20const localize = nls.loadMessageBundle();
8022afdfVladimir Kotikov8 years ago21
8a67e140Artem Egorov8 years ago22export class IOSPlatform extends GeneralMobilePlatform {
23public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
7daed3fcArtem Egorov8 years ago24private static remoteExtension: RemoteExtension;
0a68f8dbArtem Egorov8 years ago25
8a67e140Artem Egorov8 years ago26private plistBuddy = new PlistBuddy();
8022afdfVladimir Kotikov8 years ago27private targetType: TargetType = "simulator";
0db0be15Artem Egorov8 years ago28private iosProjectRoot: string;
7daed3fcArtem Egorov8 years ago29private iosDebugModeManager: IOSDebugModeManager;
30
db6fd42aRuslan Bikkinin7 years ago31private defaultConfiguration: string = "Debug";
32private configurationArgumentName: string = "--configuration";
8a67e140Artem Egorov8 years ago33
0a68f8dbArtem Egorov8 years ago34// We should add the common iOS build/run errors we find to this list
8a67e140Artem Egorov8 years ago35private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
36pattern: "No devices are booted",
d7d405aeYuri Skorokhodov7 years ago37errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago38}, {
39pattern: "FBSOpenApplicationErrorDomain",
d7d405aeYuri Skorokhodov7 years ago40errorCode: InternalErrorCode.IOSSimulatorNotLaunchable,
8a67e140Artem Egorov8 years ago41}, {
42pattern: "ios-deploy",
d7d405aeYuri Skorokhodov7 years ago43errorCode: InternalErrorCode.IOSDeployNotFound,
8a67e140Artem Egorov8 years ago44}];
45
3021756bYuri Skorokhodov6 years ago46private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
8a67e140Artem Egorov8 years ago47
db6fd42aRuslan Bikkinin7 years ago48public showDevMenu(deviceId?: string): Q.Promise<void> {
49return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId);
7daed3fcArtem Egorov8 years ago50}
51
db6fd42aRuslan Bikkinin7 years ago52public reloadApp(deviceId?: string): Q.Promise<void> {
53return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId);
7daed3fcArtem Egorov8 years ago54}
55
0a68f8dbArtem Egorov8 years ago56constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
57super(runOptions, platformDeps);
8a67e140Artem Egorov8 years ago58
db6fd42aRuslan Bikkinin7 years ago59this.runOptions.configuration = this.getConfiguration();
60
0db0be15Artem Egorov8 years ago61if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
d7d405aeYuri Skorokhodov7 years ago62this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead."));
8a67e140Artem Egorov8 years ago63}
64
764655c9Yuri Skorokhodov6 years ago65const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(this.runArguments, "--project-path", false);
66this.iosProjectRoot = path.join(this.projectPath, iosProjectFolderPath || this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
116c3cb0Ruslan Bikkinin7 years ago67const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
764655c9Yuri Skorokhodov6 years ago68this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.projectPath, 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> {
ba953e9fRedMickey6 years ago87let extProps = {
031832ffArtem Egorov8 years ago88platform: {
89value: "ios",
90isPii: false,
91},
92};
93
7fa90b3bRedMickey6 years ago94extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps);
ba953e9fRedMickey6 years ago95
031832ffArtem Egorov8 years ago96return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
97// Compile, deploy, and launch the app on either a simulator or a device
98const env = this.getEnvArgument();
99
7fa90b3bRedMickey6 years ago100if (!semver.valid(this.runOptions.reactNativeVersions.reactNativeVersion) /*Custom RN implementations should support this flag*/ || semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, IOSPlatform.NO_PACKAGER_VERSION)) {
101this.runArguments.push("--no-packager");
102}
103// 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(this.runOptions.reactNativeVersions.reactNativeVersion, "0.60.0")) {
106this.runArguments.push("--verbose");
107}
108const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
109return new OutputVerifier(() => this.generateSuccessPatterns(this.runOptions.reactNativeVersions.reactNativeVersion), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
110.process(runIosSpawn);
031832ffArtem Egorov8 years ago111});
8a67e140Artem Egorov8 years ago112}
113
114public enableJSDebuggingMode(): Q.Promise<void> {
115// Configure the app for debugging
116if (this.targetType === IOSPlatform.deviceString) {
117// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago118this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago119return Q.resolve<void>(void 0);
120}
121
122// Wait until the configuration file exists, and check to see if debugging is enabled
123return Q.all<boolean | string>([
db6fd42aRuslan Bikkinin7 years ago124this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
8a67e140Artem Egorov8 years ago125this.getBundleId(),
126])
127.spread((debugModeEnabled: boolean, bundleId: string) => {
128if (debugModeEnabled) {
129return Q.resolve(void 0);
130}
131
132// Debugging must still be enabled
133// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
134// but that file is written to by the app on occasion. To avoid races, we shut the app
135// down before writing to the file.
136const childProcess = new ChildProcess();
137
138return childProcess.execToString("xcrun simctl spawn booted launchctl list")
139.then((output: string) => {
140// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
141const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
142const match = regex.exec(output);
143
144// If we don't find a match, the app must not be running and so we do not need to close it
145return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
146})
147.then(() => {
148// Write to the settings file while the app is not running to avoid races
db6fd42aRuslan Bikkinin7 years ago149return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
8a67e140Artem Egorov8 years ago150})
151.then(() => {
152// Relaunch the app
153return this.runApp();
154});
155});
156}
157
0a68f8dbArtem Egorov8 years ago158public disableJSDebuggingMode(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago159return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
0a68f8dbArtem Egorov8 years ago160}
161
8a67e140Artem Egorov8 years ago162public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago163return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago164}
165
cbc7ac5bArtem Egorov7 years ago166public getRunArguments(): string[] {
8a67e140Artem Egorov8 years ago167let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago168
169if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago170runArguments = this.runOptions.runArguments;
116c3cb0Ruslan Bikkinin7 years ago171if (this.runOptions.scheme) {
172const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false);
173if (!schemeFromArgs) {
174runArguments.push("--scheme", this.runOptions.scheme);
175} else {
176this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted"));
177}
178}
b57ea017Artem Egorov8 years ago179} else {
180if (this.runOptions.target) {
181if (this.runOptions.target === IOSPlatform.deviceString ||
182this.runOptions.target === IOSPlatform.simulatorString) {
183
184runArguments.push(`--${this.runOptions.target}`);
185} else {
186runArguments.push("--simulator", `${this.runOptions.target}`);
187}
188}
8abbd163Artem Egorov8 years ago189
b57ea017Artem Egorov8 years ago190if (this.runOptions.iosRelativeProjectPath) {
191runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago192}
8a67e140Artem Egorov8 years ago193
b57ea017Artem Egorov8 years ago194// provide any defined scheme
195if (this.runOptions.scheme) {
196runArguments.push("--scheme", this.runOptions.scheme);
197}
8a67e140Artem Egorov8 years ago198}
199
200return runArguments;
201}
202
3021756bYuri Skorokhodov6 years ago203private generateSuccessPatterns(version: string): Q.Promise<string[]> {
204// Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
205let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
206if (this.targetType === IOSPlatform.deviceString) {
207if (semver.gte(version, "0.60.0")) {
208successPatterns.push("success Installed the app on the device");
209} else {
210successPatterns.push("INSTALLATION SUCCEEDED");
211}
212return Q(successPatterns);
213} else {
214return this.getBundleId()
215.then(bundleId => {
216if (semver.gte(version, "0.60.0")) {
217successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `);
218} else {
219successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
220}
221return successPatterns;
222});
223}
224
8a67e140Artem Egorov8 years ago225}
226
db6fd42aRuslan Bikkinin7 years ago227private getConfiguration(): string {
116c3cb0Ruslan Bikkinin7 years ago228return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
db6fd42aRuslan Bikkinin7 years ago229}
230
8a67e140Artem Egorov8 years ago231private getBundleId(): Q.Promise<string> {
116c3cb0Ruslan Bikkinin7 years ago232let scheme = this.runOptions.scheme;
233if (!scheme) {
234const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
235if (schemeFromArgs) {
236scheme = schemeFromArgs;
237}
238}
764655c9Yuri Skorokhodov6 years ago239return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme);
8a67e140Artem Egorov8 years ago240}
7daed3fcArtem Egorov8 years ago241
4edcda70Artem Egorov8 years ago242private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago243if (this.remoteExtension) {
244return this.remoteExtension;
245} else {
4edcda70Artem Egorov8 years ago246return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago247}
248}
8a67e140Artem Egorov8 years ago249}