microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cc70057d4beada1315f86cbbf3246d5c065b9eda

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

130lines · modecode

1// 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";
6
7import {Log} from "../../common/log/log";
8import {ChildProcess} from "../../common/node/childProcess";
9import {CommandExecutor} from "../../common/commandExecutor";
10import {GeneralMobilePlatform} from "../../common/generalMobilePlatform";
11import {Compiler} from "./compiler";
12import {DeviceDeployer} from "./deviceDeployer";
13import {DeviceRunner} from "./deviceRunner";
14import {IRunOptions} from "../../common/launchArgs";
15import {PlistBuddy} from "../../common/ios/plistBuddy";
16import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
17import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
18
19export class IOSPlatform extends GeneralMobilePlatform {
20 public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
21
22 private static deviceString = "device";
23 private static simulatorString = "simulator";
24
25 private plistBuddy = new PlistBuddy();
26
27 private simulatorTarget: string;
28 private isSimulator: boolean;
29 private iosProjectPath: string;
30
31 // We should add the common iOS build/run erros we find to this list
32 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
33 pattern: "No devices are booted",
34 message: "Unable to launch iOS simulator. Try specifying a different target.",
35 }, {
36 pattern: "FBSOpenApplicationErrorDomain",
37 message: "Unable to launch iOS simulator. Try specifying a different target.",
38 }];
39
40 private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
41
42 // We set remoteExtension = null so that if there is an instance of iOSPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests.
43 constructor(runOptions: IRunOptions, { remoteExtension = null } = {}) {
44 super(runOptions, { remoteExtension: remoteExtension });
45 this.simulatorTarget = this.runOptions.target || IOSPlatform.simulatorString;
46 this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
47 this.iosProjectPath = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath);
48 }
49
50 public runApp(): Q.Promise<void> {
51 // Compile, deploy, and launch the app on either a simulator or a device
52 if (this.isSimulator) {
53 // React native supports running on the iOS simulator from the command line
54 let runArguments: string[] = [];
55 if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
56 runArguments.push("--simulator", this.simulatorTarget);
57 }
58
59 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
60
61 const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
62 return new OutputVerifier(
63 () =>
64 this.generateSuccessPatterns(),
65 () =>
66 Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
67 }
68
69 return new Compiler(this.iosProjectPath).compile().then(() => {
70 return new DeviceDeployer(this.iosProjectPath).deploy();
71 }).then(() => {
72 return new DeviceRunner(this.iosProjectPath).run();
73 });
74 }
75
76 public enableJSDebuggingMode(): Q.Promise<void> {
77 // Configure the app for debugging
78 if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
79 // Note that currently we cannot automatically switch the device into debug mode.
80 Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
81 return Q.resolve<void>(void 0);
82 }
83
84 const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectPath);
85
86 // Wait until the configuration file exists, and check to see if debugging is enabled
87 return Q.all([
88 iosDebugModeManager.getSimulatorJSDebuggingModeSetting(),
89 this.getBundleId(),
90 ]).spread((debugModeSetting: string, bundleId: string) => {
91 if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
92 // Debugging must still be enabled
93 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
94 // but that file is written to by the app on occasion. To avoid races, we shut the app
95 // down before writing to the file.
96 const childProcess = new ChildProcess();
97
98 return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => {
99 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
100 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
101 const match = regex.exec(output);
102
103 // If we don't find a match, the app must not be running and so we do not need to close it
104 if (match) {
105 return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`);
106 }
107 }).then(() => {
108 // Write to the settings file while the app is not running to avoid races
109 return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
110 }).then(() => {
111 // Relaunch the app
112 return this.runApp();
113 });
114 }
115 });
116 }
117
118 public prewarmBundleCache(): Q.Promise<void> {
119 return this.remoteExtension.prewarmBundleCache(this.platformName);
120 }
121
122 private generateSuccessPatterns(): Q.Promise<string[]> {
123 return this.getBundleId().then(bundleId =>
124 IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `]));
125 }
126
127 private getBundleId(): Q.Promise<string> {
128 return this.plistBuddy.getBundleId(this.iosProjectPath);
129 }
130}
131