microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.9.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

229lines · 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 Egorov8 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);
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 Egorov8 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 Egorov8 years ago102}
db6fd42aRuslan Bikkinin7 years ago103const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
031832ffArtem Egorov8 years ago104return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios")
105.process(runIosSpawn);
106});
107});
8a67e140Artem Egorov8 years ago108}
109
110public enableJSDebuggingMode(): Q.Promise<void> {
111// Configure the app for debugging
112if (this.targetType === IOSPlatform.deviceString) {
113// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago114this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
8a67e140Artem Egorov8 years ago115return Q.resolve<void>(void 0);
116}
117
118// Wait until the configuration file exists, and check to see if debugging is enabled
119return Q.all<boolean | string>([
db6fd42aRuslan Bikkinin7 years ago120this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
8a67e140Artem Egorov8 years ago121this.getBundleId(),
122])
123.spread((debugModeEnabled: boolean, bundleId: string) => {
124if (debugModeEnabled) {
125return Q.resolve(void 0);
126}
127
128// Debugging must still be enabled
129// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
130// but that file is written to by the app on occasion. To avoid races, we shut the app
131// down before writing to the file.
132const childProcess = new ChildProcess();
133
134return childProcess.execToString("xcrun simctl spawn booted launchctl list")
135.then((output: string) => {
136// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
137const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
138const match = regex.exec(output);
139
140// If we don't find a match, the app must not be running and so we do not need to close it
141return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
142})
143.then(() => {
144// Write to the settings file while the app is not running to avoid races
db6fd42aRuslan Bikkinin7 years ago145return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
8a67e140Artem Egorov8 years ago146})
147.then(() => {
148// Relaunch the app
149return this.runApp();
150});
151});
152}
153
0a68f8dbArtem Egorov8 years ago154public disableJSDebuggingMode(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago155return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
0a68f8dbArtem Egorov8 years ago156}
157
8a67e140Artem Egorov8 years ago158public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago159return this.packager.prewarmBundleCache("ios");
8a67e140Artem Egorov8 years ago160}
161
cbc7ac5bArtem Egorov7 years ago162public getRunArguments(): string[] {
8a67e140Artem Egorov8 years ago163let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago164
165if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago166runArguments = this.runOptions.runArguments;
116c3cb0Ruslan Bikkinin7 years ago167if (this.runOptions.scheme) {
168const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false);
169if (!schemeFromArgs) {
170runArguments.push("--scheme", this.runOptions.scheme);
171} else {
172this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted"));
173}
174}
b57ea017Artem Egorov8 years ago175} else {
176if (this.runOptions.target) {
177if (this.runOptions.target === IOSPlatform.deviceString ||
178this.runOptions.target === IOSPlatform.simulatorString) {
179
180runArguments.push(`--${this.runOptions.target}`);
181} else {
182runArguments.push("--simulator", `${this.runOptions.target}`);
183}
184}
8abbd163Artem Egorov8 years ago185
b57ea017Artem Egorov8 years ago186if (this.runOptions.iosRelativeProjectPath) {
187runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago188}
8a67e140Artem Egorov8 years ago189
b57ea017Artem Egorov8 years ago190// provide any defined scheme
191if (this.runOptions.scheme) {
192runArguments.push("--scheme", this.runOptions.scheme);
193}
8a67e140Artem Egorov8 years ago194}
195
196return runArguments;
197}
198
199private generateSuccessPatterns(): Q.Promise<string[]> {
ed8367fdVladimir Kotikov8 years ago200return this.targetType === IOSPlatform.deviceString ?
201Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
202this.getBundleId()
203.then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
204.concat([`Launching ${bundleId}\n${bundleId}: `]));
8a67e140Artem Egorov8 years ago205}
206
db6fd42aRuslan Bikkinin7 years ago207private getConfiguration(): string {
116c3cb0Ruslan Bikkinin7 years ago208return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
db6fd42aRuslan Bikkinin7 years ago209}
210
8a67e140Artem Egorov8 years ago211private getBundleId(): Q.Promise<string> {
116c3cb0Ruslan Bikkinin7 years ago212let scheme = this.runOptions.scheme;
213if (!scheme) {
214const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
215if (schemeFromArgs) {
216scheme = schemeFromArgs;
217}
218}
219return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName, scheme);
8a67e140Artem Egorov8 years ago220}
7daed3fcArtem Egorov8 years ago221
4edcda70Artem Egorov8 years ago222private static remote(fsPath: string): RemoteExtension {
7daed3fcArtem Egorov8 years ago223if (this.remoteExtension) {
224return this.remoteExtension;
225} else {
4edcda70Artem Egorov8 years ago226return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago227}
228}
8a67e140Artem Egorov8 years ago229}