microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0c20a0a2cd365df3bf6ae3ebec62c7c68ffe61d1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

111lines · 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
b0061ac6Meena Kunnathur Balakrishnan10 years ago6import {Log} from "../../common/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";
488f1908Jimmy Thomson10 years ago16
f8d32439dlebu10 years ago17export class IOSPlatform implements IAppPlatform {
bc7a32ceJimmy Thomson10 years ago18private static deviceString = "device";
19private static simulatorString = "simulator";
20
a9d96b7cdigeff10 years ago21private plistBuddy = new PlistBuddy();
22
110558c0Jimmy Thomson10 years ago23private projectPath: string;
24private simulatorTarget: string;
25private isSimulator: boolean;
26
488f1908Jimmy Thomson10 years ago27public runApp(launchArgs: IRunOptions): Q.Promise<void> {
28// Compile, deploy, and launch the app on either a simulator or a device
110558c0Jimmy Thomson10 years ago29this.consumeArguments(launchArgs);
488f1908Jimmy Thomson10 years ago30
110558c0Jimmy Thomson10 years ago31if (this.isSimulator) {
488f1908Jimmy Thomson10 years ago32// React native supports running on the iOS simulator from the command line
f8d32439dlebu10 years ago33let runArguments: string[] = [];
110558c0Jimmy Thomson10 years ago34if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
488f1908Jimmy Thomson10 years ago35runArguments.push("--simulator");
110558c0Jimmy Thomson10 years ago36runArguments.push(this.simulatorTarget);
488f1908Jimmy Thomson10 years ago37}
38
10873e11digeff10 years ago39const runIosSpawn = new CommandExecutor(this.projectPath).spawnChildReactCommandProcess("run-ios", runArguments);
5b0582f3digeff10 years ago40const deferred = Q.defer<void>();
10873e11digeff10 years ago41runIosSpawn.stderr.on("data", (data: Buffer) => {
5b0582f3digeff10 years ago42const dataString = data.toString();
43if (dataString.indexOf("No devices are booted") !== -1 // No emulators are started
44|| dataString.indexOf("FBSOpenApplicationErrorDomain") !== -1) { // The incorrect emulator is started
45deferred.reject(new Error("Unable to launch iOS simulator. Try specifying a different target."));
46}
47});
10873e11digeff10 years ago48
ce591c62digeff10 years ago49return runIosSpawn.outcome.then(() => {
50deferred.resolve(void 0); // We resolve deferred when the process ends, in case it wasn't already rejected
51return deferred.promise; // We return the promise. If an error was detected on stderr, this will be a rejection
f7208a21Jimmy Thomson10 years ago52});
488f1908Jimmy Thomson10 years ago53}
54
bc96b26bJimmy Thomson10 years ago55return new Compiler(this.projectPath).compile().then(() => {
110558c0Jimmy Thomson10 years ago56return new DeviceDeployer(this.projectPath).deploy();
488f1908Jimmy Thomson10 years ago57}).then(() => {
110558c0Jimmy Thomson10 years ago58return new DeviceRunner(this.projectPath).run();
488f1908Jimmy Thomson10 years ago59});
60}
61
62public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> {
63// Configure the app for debugging
110558c0Jimmy Thomson10 years ago64this.consumeArguments(launchArgs);
488f1908Jimmy Thomson10 years ago65
110558c0Jimmy Thomson10 years ago66if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
488f1908Jimmy Thomson10 years ago67// Note that currently we cannot automatically switch the device into debug mode.
b044f0b9Jimmy Thomson10 years ago68Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
488f1908Jimmy Thomson10 years ago69return Q.resolve<void>(void 0);
70}
71
dbe130e4Jimmy Thomson10 years ago72const iosDebugModeManager = new IOSDebugModeManager(this.projectPath);
73
74// Wait until the configuration file exists, and check to see if debugging is enabled
75return Q.all([
76iosDebugModeManager.getSimulatorJSDebuggingModeSetting(),
77this.plistBuddy.getBundleId(launchArgs.projectRoot)
78]).spread((debugModeSetting: string, bundleId: string) => {
79if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
80// Debugging must still be enabled
771dc596Jimmy Thomson10 years ago81// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
82// but that file is written to by the app on occasion. To avoid races, we shut the app
83// down before writing to the file.
dbe130e4Jimmy Thomson10 years ago84const childProcess = new ChildProcess();
b3db6f6bJimmy Thomson10 years ago85
8bff2a55Jimmy Thomson10 years ago86return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => {
b3db6f6bJimmy Thomson10 years ago87// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
88const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
8bff2a55Jimmy Thomson10 years ago89const match = regex.exec(output);
b3db6f6bJimmy Thomson10 years ago90
91// If we don't find a match, the app must not be running and so we do not need to close it
92if (match) {
93return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`);
94}
dbe130e4Jimmy Thomson10 years ago95}).then(() => {
96// Write to the settings file while the app is not running to avoid races
97return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
98}).then(() => {
99// Relaunch the app
100return this.runApp(launchArgs);
101});
102}
103});
488f1908Jimmy Thomson10 years ago104}
110558c0Jimmy Thomson10 years ago105
106private consumeArguments(launchArgs: IRunOptions): void {
107this.projectPath = launchArgs.projectRoot;
108this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString;
109this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
110}
a9d96b7cdigeff10 years ago111}