microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debugConfigurationProvider.ts
170lines · 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 vscode from "vscode"; |
| 5 | import { TelemetryHelper } from "../common/telemetryHelper"; |
| 6 | import { Telemetry } from "../common/telemetry"; |
| 7 | import * as nls from "vscode-nls"; |
| 8 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
| 9 | const localize = nls.loadMessageBundle(); |
| 10 | |
| 11 | export const DEBUG_TYPES = { |
| 12 | REACT_NATIVE: "reactnative-preview", |
| 13 | REACT_NATIVE_DIRECT: "reactnativedirect-preview", |
| 14 | }; |
| 15 | |
| 16 | export class ReactNativeDebugConfigProvider implements vscode.DebugConfigurationProvider { |
| 17 | private debugConfigurations = { |
| 18 | "Debug Android (Preview)": { |
| 19 | "name": "Debug Android (Preview)", |
| 20 | "cwd": "${workspaceFolder}", |
| 21 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 22 | "request": "launch", |
| 23 | "platform": "android", |
| 24 | }, |
| 25 | "Run Android (Preview)": { |
| 26 | "name": "Run Android (Preview)", |
| 27 | "cwd": "${workspaceFolder}", |
| 28 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 29 | "request": "launch", |
| 30 | "platform": "android", |
| 31 | "enableDebug": false, |
| 32 | }, |
| 33 | "Debug iOS (Preview)": { |
| 34 | "name": "Debug iOS (Preview)", |
| 35 | "cwd": "${workspaceFolder}", |
| 36 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 37 | "request": "launch", |
| 38 | "platform": "ios", |
| 39 | }, |
| 40 | "Run iOS (Preview)": { |
| 41 | "name": "Run iOS (Preview)", |
| 42 | "cwd": "${workspaceFolder}", |
| 43 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 44 | "request": "launch", |
| 45 | "platform": "ios", |
| 46 | "enableDebug": false, |
| 47 | }, |
| 48 | "Debug Windows (Preview)": { |
| 49 | "name": "Debug Windows (Preview)", |
| 50 | "cwd": "${workspaceFolder}", |
| 51 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 52 | "request": "launch", |
| 53 | "platform": "windows", |
| 54 | }, |
| 55 | "Attach to packager (Preview)": { |
| 56 | "name": "Attach to packager (Preview)", |
| 57 | "cwd": "${workspaceFolder}", |
| 58 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 59 | "request": "attach", |
| 60 | }, |
| 61 | "Debug in Exponent (Preview)": { |
| 62 | "name": "Debug in Exponent (Preview)", |
| 63 | "cwd": "${workspaceFolder}", |
| 64 | "type": DEBUG_TYPES.REACT_NATIVE, |
| 65 | "request": "launch", |
| 66 | "platform": "exponent", |
| 67 | }, |
| 68 | "Debug Android Hermes (Preview) - Experimental": { |
| 69 | "name": "Debug Android Hermes (Preview) - Experimental", |
| 70 | "cwd": "${workspaceFolder}", |
| 71 | "type": DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 72 | "request": "launch", |
| 73 | "platform": "android", |
| 74 | }, |
| 75 | "Run Android Hermes (Preview) - Experimental": { |
| 76 | "name": "Run Android Hermes (Preview) - Experimental", |
| 77 | "cwd": "${workspaceFolder}", |
| 78 | "type": DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 79 | "request": "launch", |
| 80 | "platform": "android", |
| 81 | "enableDebug": false, |
| 82 | }, |
| 83 | "Attach to Hermes application (Preview) - Experimental": { |
| 84 | "name": "Attach to Hermes application (Preview) - Experimental", |
| 85 | "cwd": "${workspaceFolder}", |
| 86 | "type": DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 87 | "request": "attach", |
| 88 | }, |
| 89 | }; |
| 90 | |
| 91 | private pickConfig: ReadonlyArray<vscode.QuickPickItem> = [ |
| 92 | { |
| 93 | label: "Debug Android (Preview)", |
| 94 | description: localize("DebugAndroidConfigDesc", "Run and debug Android application"), |
| 95 | }, |
| 96 | { |
| 97 | label: "Run Android (Preview)", |
| 98 | description: localize("RunAndroidConfigDesc", "Run Android application"), |
| 99 | }, |
| 100 | { |
| 101 | label: "Debug iOS (Preview)", |
| 102 | description: localize("DebugiOSConfigDesc", "Run and debug iOS application"), |
| 103 | }, |
| 104 | { |
| 105 | label: "Run iOS (Preview)", |
| 106 | description: localize("RuniOSConfigDesc", "Run iOS application"), |
| 107 | }, |
| 108 | { |
| 109 | label: "Debug Windows (Preview)", |
| 110 | description: localize("DebugWindowsConfigDesc", "Run and debug Windows application"), |
| 111 | }, |
| 112 | { |
| 113 | label: "Attach to packager (Preview)", |
| 114 | description: localize("AttachToPackagerConfigDesc", "Attach to already working application packager"), |
| 115 | }, |
| 116 | { |
| 117 | label: "Debug in Exponent (Preview)", |
| 118 | description: localize("DebugExpoConfigDesc", "Debug Expo application or React Native application in Expo"), |
| 119 | }, |
| 120 | { |
| 121 | label: "Debug Android Hermes (Preview) - Experimental", |
| 122 | description: localize("DebugAndroidHermesConfigDesc", "Run and debug Android Hermes application"), |
| 123 | }, |
| 124 | { |
| 125 | label: "Attach to Hermes application (Preview) - Experimental", |
| 126 | description: localize("AttachToPackagerHermesConfigDesc", "Attach to already working Android Hermes application packager"), |
| 127 | }, |
| 128 | ]; |
| 129 | |
| 130 | public async provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration[]> { |
| 131 | return new Promise<vscode.DebugConfiguration[]>((resolve) => { |
| 132 | const configPicker = this.prepareDebugConfigPicker(); |
| 133 | const disposables: vscode.Disposable[] = []; |
| 134 | const pickHandler = () => { |
| 135 | let chosenConfigsEvent = TelemetryHelper.createTelemetryEvent("chosenDebugConfigurations"); |
| 136 | let selected: string[] = configPicker.selectedItems.map(element => element.label); |
| 137 | chosenConfigsEvent.properties["selectedItems"] = selected; |
| 138 | Telemetry.send(chosenConfigsEvent); |
| 139 | const launchConfig = this.gatherDebugScenarios(selected); |
| 140 | disposables.forEach(d => d.dispose()); |
| 141 | resolve(launchConfig); |
| 142 | }; |
| 143 | |
| 144 | disposables.push( |
| 145 | configPicker.onDidAccept(pickHandler), |
| 146 | configPicker.onDidHide(pickHandler), |
| 147 | configPicker |
| 148 | ); |
| 149 | |
| 150 | configPicker.show(); |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | private gatherDebugScenarios(selectedItems: string[]): vscode.DebugConfiguration[] { |
| 155 | let launchConfig: vscode.DebugConfiguration[] = selectedItems.map(element => this.debugConfigurations[element]); |
| 156 | return launchConfig; |
| 157 | } |
| 158 | |
| 159 | private prepareDebugConfigPicker(): vscode.QuickPick<vscode.QuickPickItem> { |
| 160 | const debugConfigPicker = vscode.window.createQuickPick(); |
| 161 | debugConfigPicker.canSelectMany = true; |
| 162 | debugConfigPicker.ignoreFocusOut = true; |
| 163 | debugConfigPicker.title = localize("DebugConfigQuickPickLabel", "Pick debug configurations"); |
| 164 | debugConfigPicker.items = this.pickConfig; |
| 165 | // QuickPickItem property `picked` doesn't work, so this line will check first item in the list |
| 166 | // which is supposed to be Debug Android |
| 167 | debugConfigPicker.selectedItems = [this.pickConfig[0]]; |
| 168 | return debugConfigPicker; |
| 169 | } |
| 170 | } |
| 171 | |