microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7be1388c3442500874ff28fc32e6f332ad9724ef

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

121lines · modeblame

488f1908Jimmy Thomson10 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";
5
190e393cMeena Kunnathur Balakrishnan10 years ago6import {Log} from "../../common/log/log";
dbe130e4Jimmy Thomson10 years ago7import {ChildProcess} from "../../common/node/childProcess";
b0061ac6Meena Kunnathur Balakrishnan10 years ago8import {CommandExecutor} from "../../common/commandExecutor";
f8d32439dlebu10 years ago9import {IAppPlatform} from "../platformResolver";
488f1908Jimmy Thomson10 years ago10import {Compiler} from "./compiler";
11import {DeviceDeployer} from "./deviceDeployer";
12import {DeviceRunner} from "./deviceRunner";
acf08bc2dlebu10 years ago13import {IRunOptions} from "../../common/launchArgs";
a9d96b7cdigeff10 years ago14import {PlistBuddy} from "../../common/ios/plistBuddy";
1ed272e1digeff10 years ago15import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
f8b7022ddigeff10 years ago16import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
488f1908Jimmy Thomson10 years ago17
f8d32439dlebu10 years ago18export class IOSPlatform implements IAppPlatform {
bc7a32ceJimmy Thomson10 years ago19private static deviceString = "device";
20private static simulatorString = "simulator";
21
a9d96b7cdigeff10 years ago22private plistBuddy = new PlistBuddy();
23
110558c0Jimmy Thomson10 years ago24private projectPath: string;
25private simulatorTarget: string;
26private isSimulator: boolean;
27
7cc67271digeff10 years ago28// We should add the common iOS build/run erros we find to this list
29private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure = {
30"No devices are booted": "Unable to launch iOS simulator. Try specifying a different target.",
cdf34447digeff10 years ago31"FBSOpenApplicationErrorDomain": "Unable to launch iOS simulator. Try specifying a different target.",
32};
7cc67271digeff10 years ago33
34private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
35
7be1388cdigeff10 years ago36constructor(private runOptions: IRunOptions) {
37this.consumeArguments();
38}
488f1908Jimmy Thomson10 years ago39
7be1388cdigeff10 years ago40public runApp(): Q.Promise<void> {
41// Compile, deploy, and launch the app on either a simulator or a device
110558c0Jimmy Thomson10 years ago42if (this.isSimulator) {
488f1908Jimmy Thomson10 years ago43// React native supports running on the iOS simulator from the command line
f8d32439dlebu10 years ago44let runArguments: string[] = [];
110558c0Jimmy Thomson10 years ago45if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
488f1908Jimmy Thomson10 years ago46runArguments.push("--simulator");
110558c0Jimmy Thomson10 years ago47runArguments.push(this.simulatorTarget);
488f1908Jimmy Thomson10 years ago48}
49
10873e11digeff10 years ago50const runIosSpawn = new CommandExecutor(this.projectPath).spawnChildReactCommandProcess("run-ios", runArguments);
f8b7022ddigeff10 years ago51return new OutputVerifier(
7cc67271digeff10 years ago52() =>
7be1388cdigeff10 years ago53this.generateSuccessPatterns(),
7cc67271digeff10 years ago54() =>
55Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
488f1908Jimmy Thomson10 years ago56}
57
bc96b26bJimmy Thomson10 years ago58return new Compiler(this.projectPath).compile().then(() => {
110558c0Jimmy Thomson10 years ago59return new DeviceDeployer(this.projectPath).deploy();
488f1908Jimmy Thomson10 years ago60}).then(() => {
110558c0Jimmy Thomson10 years ago61return new DeviceRunner(this.projectPath).run();
488f1908Jimmy Thomson10 years ago62});
63}
64
7be1388cdigeff10 years ago65public enableJSDebuggingMode(): Q.Promise<void> {
488f1908Jimmy Thomson10 years ago66// Configure the app for debugging
110558c0Jimmy Thomson10 years ago67if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
488f1908Jimmy Thomson10 years ago68// Note that currently we cannot automatically switch the device into debug mode.
b044f0b9Jimmy Thomson10 years ago69Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
488f1908Jimmy Thomson10 years ago70return Q.resolve<void>(void 0);
71}
72
dbe130e4Jimmy Thomson10 years ago73const iosDebugModeManager = new IOSDebugModeManager(this.projectPath);
74
75// Wait until the configuration file exists, and check to see if debugging is enabled
76return Q.all([
77iosDebugModeManager.getSimulatorJSDebuggingModeSetting(),
7be1388cdigeff10 years ago78this.getBundleId(),
dbe130e4Jimmy Thomson10 years ago79]).spread((debugModeSetting: string, bundleId: string) => {
80if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
81// Debugging must still be enabled
771dc596Jimmy Thomson10 years ago82// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
83// but that file is written to by the app on occasion. To avoid races, we shut the app
84// down before writing to the file.
dbe130e4Jimmy Thomson10 years ago85const childProcess = new ChildProcess();
b3db6f6bJimmy Thomson10 years ago86
8bff2a55Jimmy Thomson10 years ago87return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => {
b3db6f6bJimmy Thomson10 years ago88// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
89const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
8bff2a55Jimmy Thomson10 years ago90const match = regex.exec(output);
b3db6f6bJimmy Thomson10 years ago91
92// If we don't find a match, the app must not be running and so we do not need to close it
93if (match) {
94return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`);
95}
dbe130e4Jimmy Thomson10 years ago96}).then(() => {
97// Write to the settings file while the app is not running to avoid races
98return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
99}).then(() => {
100// Relaunch the app
7be1388cdigeff10 years ago101return this.runApp();
dbe130e4Jimmy Thomson10 years ago102});
103}
104});
488f1908Jimmy Thomson10 years ago105}
110558c0Jimmy Thomson10 years ago106
7be1388cdigeff10 years ago107private consumeArguments(): void {
108this.projectPath = this.runOptions.projectRoot;
109this.simulatorTarget = this.runOptions.target || IOSPlatform.simulatorString;
110558c0Jimmy Thomson10 years ago110this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
111}
7cc67271digeff10 years ago112
7be1388cdigeff10 years ago113private generateSuccessPatterns(): Q.Promise<string[]> {
114return this.getBundleId().then(bundleId =>
7cc67271digeff10 years ago115IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `]));
116}
7be1388cdigeff10 years ago117
118private getBundleId(): Q.Promise<string> {
119return this.plistBuddy.getBundleId(this.projectPath);
120}
a9d96b7cdigeff10 years ago121}