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