microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/ios/iOSPlatform.ts
76lines · 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 | |
| 4 | import * as Q from "q"; |
| 5 | |
| 6 | import {Log} from "../../utils/commands/log"; |
| 7 | import {CommandExecutor} from "../../utils/commands/commandExecutor"; |
| 8 | import {IAppPlatform} from "../platformResolver"; |
| 9 | import {Compiler} from "./compiler"; |
| 10 | import {DeviceDeployer} from "./deviceDeployer"; |
| 11 | import {DeviceRunner} from "./deviceRunner"; |
| 12 | import {IRunOptions} from "../launchArgs"; |
| 13 | import {SimulatorPlist} from "./simulatorPlist"; |
| 14 | import {PlistBuddy} from "./plistBuddy"; |
| 15 | |
| 16 | export class IOSPlatform implements IAppPlatform { |
| 17 | private static deviceString = "device"; |
| 18 | private static simulatorString = "simulator"; |
| 19 | |
| 20 | private projectPath: string; |
| 21 | private simulatorTarget: string; |
| 22 | private isSimulator: boolean; |
| 23 | |
| 24 | public runApp(launchArgs: IRunOptions): Q.Promise<void> { |
| 25 | // Compile, deploy, and launch the app on either a simulator or a device |
| 26 | this.consumeArguments(launchArgs); |
| 27 | |
| 28 | if (this.isSimulator) { |
| 29 | // React native supports running on the iOS simulator from the command line |
| 30 | let runArguments: string[] = []; |
| 31 | if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) { |
| 32 | runArguments.push("--simulator"); |
| 33 | runArguments.push(this.simulatorTarget); |
| 34 | } |
| 35 | |
| 36 | return new CommandExecutor(this.projectPath).spawnAndWaitReactCommand("run-ios", runArguments); |
| 37 | } |
| 38 | |
| 39 | // TODO: This is currently a stub, device debugging is not yet implemented |
| 40 | return new Compiler(this.projectPath, this.isSimulator).compile().then(() => { |
| 41 | return new DeviceDeployer(this.projectPath).deploy(); |
| 42 | }).then(() => { |
| 43 | return new DeviceRunner(this.projectPath).run(); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> { |
| 48 | // Configure the app for debugging |
| 49 | this.consumeArguments(launchArgs); |
| 50 | |
| 51 | if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) { |
| 52 | // Note that currently we cannot automatically switch the device into debug mode. |
| 53 | Log.logMessage("Application is running on a device, please shake device and select 'Debug in Javascript' to enable debugging."); |
| 54 | return Q.resolve<void>(void 0); |
| 55 | } |
| 56 | |
| 57 | const plistBuddy = new PlistBuddy(); |
| 58 | // Find the plistFile with the configuration setting |
| 59 | return new SimulatorPlist(launchArgs.projectRoot).findPlistFile().then((plistFile: string) => { |
| 60 | // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode |
| 61 | // This is approximately equivalent to clicking the "Debug in Chrome" button |
| 62 | return plistBuddy.setPlistProperty(plistFile, ":RCTDevMenu:executorClass", "RCTWebSocketExecutor"); |
| 63 | }).then(() => { |
| 64 | return plistBuddy.getBundleId(launchArgs.projectRoot); |
| 65 | }).then((bundleId: string) => { |
| 66 | // Relaunch the app so the new setting can take effect |
| 67 | return new CommandExecutor().execute(`xcrun simctl launch booted ${bundleId}`); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | private consumeArguments(launchArgs: IRunOptions): void { |
| 72 | this.projectPath = launchArgs.projectRoot; |
| 73 | this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString; |
| 74 | this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString; |
| 75 | } |
| 76 | } |