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