microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.1.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSPlatform.ts

307lines · 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 path from "path";
df8c800dArtem Egorov8 years ago5import * as semver from "semver";
8a67e140Artem Egorov8 years ago6
7import {ChildProcess} from "../../common/node/childProcess";
8import {CommandExecutor} from "../../common/commandExecutor";
0a68f8dbArtem Egorov8 years ago9import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform";
259c018fYuri Skorokhodov5 years ago10import {IIOSRunOptions, PlatformType} from "../launchArgs";
0a68f8dbArtem Egorov8 years ago11import {PlistBuddy} from "./plistBuddy";
12import {IOSDebugModeManager} from "./iOSDebugModeManager";
8a67e140Artem Egorov8 years ago13import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
031832ffArtem Egorov8 years ago14import {TelemetryHelper} from "../../common/telemetryHelper";
fc602bb6Yuri Skorokhodov7 years ago15import { InternalErrorCode } from "../../common/error/internalErrorCode";
d7d405aeYuri Skorokhodov7 years ago16import * as nls from "vscode-nls";
7e74daf7Yuri Skorokhodov6 years ago17import { AppLauncher } from "../appLauncher";
119d7878JiglioNero5 years ago18import { IiOSSimulator, IOSSimulatorManager } from "./iOSSimulatorManager";
2d8af448Yuri Skorokhodov6 years ago19nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
d7d405aeYuri Skorokhodov7 years ago20const localize = nls.loadMessageBundle();
8022afdfVladimir Kotikov8 years ago21
8a67e140Artem Egorov8 years ago22export class IOSPlatform extends GeneralMobilePlatform {
23public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
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;
119d7878JiglioNero5 years ago29private simulatorManager: IOSSimulatorManager;
7daed3fcArtem Egorov8 years ago30
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
ce5e88eeYuri Skorokhodov5 years ago48public showDevMenu(appLauncher: AppLauncher): Promise<void> {
7e74daf7Yuri Skorokhodov6 years ago49const worker = appLauncher.getAppWorker();
50if (worker) {
51worker.showDevMenuCommand();
52}
53
ce5e88eeYuri Skorokhodov5 years ago54return Promise.resolve();
7daed3fcArtem Egorov8 years ago55}
56
ce5e88eeYuri Skorokhodov5 years ago57public reloadApp(appLauncher: AppLauncher): Promise<void> {
7e74daf7Yuri Skorokhodov6 years ago58const worker = appLauncher.getAppWorker();
59if (worker) {
60worker.reloadAppCommand();
61}
ce5e88eeYuri Skorokhodov5 years ago62return Promise.resolve();
7daed3fcArtem Egorov8 years ago63}
64
0a68f8dbArtem Egorov8 years ago65constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
66super(runOptions, platformDeps);
8a67e140Artem Egorov8 years ago67
119d7878JiglioNero5 years ago68this.simulatorManager = new IOSSimulatorManager();
db6fd42aRuslan Bikkinin7 years ago69this.runOptions.configuration = this.getConfiguration();
70
0db0be15Artem Egorov8 years ago71if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
d7d405aeYuri Skorokhodov7 years ago72this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead."));
8a67e140Artem Egorov8 years ago73}
74
764655c9Yuri Skorokhodov6 years ago75const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(this.runArguments, "--project-path", false);
76this.iosProjectRoot = path.join(this.projectPath, iosProjectFolderPath || this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH);
116c3cb0Ruslan Bikkinin7 years ago77const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
764655c9Yuri Skorokhodov6 years ago78this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.projectPath, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme);
8a67e140Artem Egorov8 years ago79
db6fd42aRuslan Bikkinin7 years ago80if (this.runArguments && this.runArguments.length > 0) {
81this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
8022afdfVladimir Kotikov8 years ago82IOSPlatform.deviceString : IOSPlatform.simulatorString;
83return;
84}
0db0be15Artem Egorov8 years ago85
8022afdfVladimir Kotikov8 years ago86if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
87this.runOptions.target !== IOSPlatform.deviceString)) {
0db0be15Artem Egorov8 years ago88
8022afdfVladimir Kotikov8 years ago89this.targetType = IOSPlatform.simulatorString;
90return;
8a67e140Artem Egorov8 years ago91}
8022afdfVladimir Kotikov8 years ago92
93this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
8a67e140Artem Egorov8 years ago94}
95
119d7878JiglioNero5 years ago96public resolveVirtualDevice(target: string): Promise<IiOSSimulator | null> {
97if (target === "simulator") {
98return this.simulatorManager.startSelection()
99.then((simulatorName: string | undefined) => {
100if (simulatorName) {
101const simulator = this.simulatorManager.findSimulator(simulatorName);
102if (simulator) {
103GeneralMobilePlatform.removeRunArgument(this.runArguments, "--simulator", true);
104GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id);
105}
106return simulator;
107}
108else {
109return null;
110}
111});
112}
113else if (!target.includes("device")) {
114return this.simulatorManager.collectSimulators()
115.then((simulators) => {
116let simulator = this.simulatorManager.getSimulatorById(target, simulators);
117if (simulator) {
118GeneralMobilePlatform.removeRunArgument(this.runArguments, "--simulator", false);
119GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id);
120}
3b728847JiglioNero5 years ago121return null;
119d7878JiglioNero5 years ago122});
123}
124else {
125return Promise.resolve(null);
126}
127}
128
ce5e88eeYuri Skorokhodov5 years ago129public runApp(): Promise<void> {
ba953e9fRedMickey6 years ago130let extProps = {
031832ffArtem Egorov8 years ago131platform: {
259c018fYuri Skorokhodov5 years ago132value: PlatformType.iOS,
031832ffArtem Egorov8 years ago133isPii: false,
134},
135};
136
341dba36Yuri Skorokhodov5 years ago137extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(this.runOptions, this.runOptions.reactNativeVersions, extProps);
ba953e9fRedMickey6 years ago138
031832ffArtem Egorov8 years ago139return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => {
140// Compile, deploy, and launch the app on either a simulator or a device
de838bbfJiglioNero6 years ago141const env = GeneralMobilePlatform.getEnvArgument(process.env, this.runOptions.env, this.runOptions.envFile);
031832ffArtem Egorov8 years ago142
7fa90b3bRedMickey6 years ago143if (!semver.valid(this.runOptions.reactNativeVersions.reactNativeVersion) /*Custom RN implementations should support this flag*/ || semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, IOSPlatform.NO_PACKAGER_VERSION)) {
144this.runArguments.push("--no-packager");
145}
146// Since @react-native-community/cli@2.1.0 build output are hidden by default
147// we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns
148if (semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, "0.60.0")) {
149this.runArguments.push("--verbose");
150}
151const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env});
ce5e88eeYuri Skorokhodov5 years ago152return new OutputVerifier(
153() =>
154this.generateSuccessPatterns(this.runOptions.reactNativeVersions.reactNativeVersion),
155() =>
259c018fYuri Skorokhodov5 years ago156Promise.resolve(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), PlatformType.iOS)
7fa90b3bRedMickey6 years ago157.process(runIosSpawn);
031832ffArtem Egorov8 years ago158});
8a67e140Artem Egorov8 years ago159}
160
ce5e88eeYuri Skorokhodov5 years ago161public enableJSDebuggingMode(): Promise<void> {
8a67e140Artem Egorov8 years ago162// Configure the app for debugging
163if (this.targetType === IOSPlatform.deviceString) {
164// Note that currently we cannot automatically switch the device into debug mode.
7daed3fcArtem Egorov8 years ago165this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging.");
ce5e88eeYuri Skorokhodov5 years ago166return Promise.resolve();
8a67e140Artem Egorov8 years ago167}
168
169// Wait until the configuration file exists, and check to see if debugging is enabled
ce5e88eeYuri Skorokhodov5 years ago170return Promise.all<boolean | string>([
db6fd42aRuslan Bikkinin7 years ago171this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName),
8a67e140Artem Egorov8 years ago172this.getBundleId(),
173])
ce5e88eeYuri Skorokhodov5 years ago174.then(([debugModeEnabled, bundleId]) => {
8a67e140Artem Egorov8 years ago175if (debugModeEnabled) {
ce5e88eeYuri Skorokhodov5 years ago176return Promise.resolve();
8a67e140Artem Egorov8 years ago177}
178
179// Debugging must still be enabled
180// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
181// but that file is written to by the app on occasion. To avoid races, we shut the app
182// down before writing to the file.
183const childProcess = new ChildProcess();
184
185return childProcess.execToString("xcrun simctl spawn booted launchctl list")
186.then((output: string) => {
187// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
188const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
189const match = regex.exec(output);
190
191// If we don't find a match, the app must not be running and so we do not need to close it
192return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
193})
194.then(() => {
195// Write to the settings file while the app is not running to avoid races
db6fd42aRuslan Bikkinin7 years ago196return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName);
8a67e140Artem Egorov8 years ago197})
198.then(() => {
199// Relaunch the app
200return this.runApp();
201});
202});
203}
204
ce5e88eeYuri Skorokhodov5 years ago205public disableJSDebuggingMode(): Promise<void> {
c73f53cbJiglioNero5 years ago206if (this.targetType === IOSPlatform.deviceString) {
207return Promise.resolve();
208}
db6fd42aRuslan Bikkinin7 years ago209return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName);
0a68f8dbArtem Egorov8 years ago210}
211
ce5e88eeYuri Skorokhodov5 years ago212public prewarmBundleCache(): Promise<void> {
259c018fYuri Skorokhodov5 years ago213return this.packager.prewarmBundleCache(PlatformType.iOS);
8a67e140Artem Egorov8 years ago214}
215
cbc7ac5bArtem Egorov7 years ago216public getRunArguments(): string[] {
8a67e140Artem Egorov8 years ago217let runArguments: string[] = [];
0db0be15Artem Egorov8 years ago218
219if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
b57ea017Artem Egorov8 years ago220runArguments = this.runOptions.runArguments;
116c3cb0Ruslan Bikkinin7 years ago221if (this.runOptions.scheme) {
222const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false);
223if (!schemeFromArgs) {
224runArguments.push("--scheme", this.runOptions.scheme);
225} else {
226this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted"));
227}
228}
b57ea017Artem Egorov8 years ago229} else {
230if (this.runOptions.target) {
de838bbfJiglioNero6 years ago231runArguments.push(...this.handleTargetArg(this.runOptions.target));
b57ea017Artem Egorov8 years ago232}
8abbd163Artem Egorov8 years ago233
b57ea017Artem Egorov8 years ago234if (this.runOptions.iosRelativeProjectPath) {
235runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
8022afdfVladimir Kotikov8 years ago236}
8a67e140Artem Egorov8 years ago237
b57ea017Artem Egorov8 years ago238// provide any defined scheme
239if (this.runOptions.scheme) {
240runArguments.push("--scheme", this.runOptions.scheme);
241}
8a67e140Artem Egorov8 years ago242}
243
244return runArguments;
245}
246
de838bbfJiglioNero6 years ago247private handleTargetArg(target: string): string[] {
248if (target === IOSPlatform.deviceString ||
249target === IOSPlatform.simulatorString) {
250return [`--${this.runOptions.target}`];
251} else {
252if (target.indexOf(IOSPlatform.deviceString) !== -1) {
253const deviceArgs = target.split("=");
254return deviceArgs[1] ? [`--${IOSPlatform.deviceString}`, deviceArgs[1]] : [`--${IOSPlatform.deviceString}`];
255} else {
256return [`--${IOSPlatform.simulatorString}`, `${this.runOptions.target}`];
257}
258}
259}
260
ce5e88eeYuri Skorokhodov5 years ago261private generateSuccessPatterns(version: string): Promise<string[]> {
3021756bYuri Skorokhodov6 years ago262// Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation
263let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS];
264if (this.targetType === IOSPlatform.deviceString) {
265if (semver.gte(version, "0.60.0")) {
266successPatterns.push("success Installed the app on the device");
267} else {
268successPatterns.push("INSTALLATION SUCCEEDED");
269}
ce5e88eeYuri Skorokhodov5 years ago270return Promise.resolve(successPatterns);
3021756bYuri Skorokhodov6 years ago271} else {
272return this.getBundleId()
273.then(bundleId => {
274if (semver.gte(version, "0.60.0")) {
275successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `);
276} else {
277successPatterns.push(`Launching ${bundleId}\n${bundleId}: `);
278}
279return successPatterns;
280});
281}
282
8a67e140Artem Egorov8 years ago283}
284
db6fd42aRuslan Bikkinin7 years ago285private getConfiguration(): string {
116c3cb0Ruslan Bikkinin7 years ago286return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration;
db6fd42aRuslan Bikkinin7 years ago287}
288
ce5e88eeYuri Skorokhodov5 years ago289private getBundleId(): Promise<string> {
116c3cb0Ruslan Bikkinin7 years ago290let scheme = this.runOptions.scheme;
291if (!scheme) {
292const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false);
293if (schemeFromArgs) {
294scheme = schemeFromArgs;
295}
296}
764655c9Yuri Skorokhodov6 years ago297return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme);
8a67e140Artem Egorov8 years ago298}
7daed3fcArtem Egorov8 years ago299
f872f4d5RedMickey6 years ago300/*private static remote(fsPath: string): RemoteExtension { // TODO replace with a new implementation from appLauncher
7daed3fcArtem Egorov8 years ago301if (this.remoteExtension) {
302return this.remoteExtension;
303} else {
4edcda70Artem Egorov8 years ago304return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath));
7daed3fcArtem Egorov8 years ago305}
f872f4d5RedMickey6 years ago306}*/
8a67e140Artem Egorov8 years ago307}