microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debugConfigurationProvider.ts
253lines · 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 | import { PlatformType } from "./launchArgs"; |
| 9 | import { IWDPHelper } from "../debugger/direct/IWDPHelper"; |
| 10 | nls.config({ |
| 11 | messageFormat: nls.MessageFormat.bundle, |
| 12 | bundleFormat: nls.BundleFormat.standalone, |
| 13 | })(); |
| 14 | const localize = nls.loadMessageBundle(); |
| 15 | |
| 16 | export const DEBUG_TYPES = { |
| 17 | REACT_NATIVE: "reactnative", |
| 18 | REACT_NATIVE_DIRECT: "reactnativedirect", |
| 19 | }; |
| 20 | |
| 21 | export class ReactNativeDebugConfigProvider implements vscode.DebugConfigurationProvider { |
| 22 | private debugConfigurations = { |
| 23 | "Debug Android": { |
| 24 | name: "Debug Android", |
| 25 | cwd: "${workspaceFolder}", |
| 26 | type: DEBUG_TYPES.REACT_NATIVE, |
| 27 | request: "launch", |
| 28 | platform: PlatformType.Android, |
| 29 | }, |
| 30 | "Run Android": { |
| 31 | name: "Run Android", |
| 32 | cwd: "${workspaceFolder}", |
| 33 | type: DEBUG_TYPES.REACT_NATIVE, |
| 34 | request: "launch", |
| 35 | platform: PlatformType.Android, |
| 36 | enableDebug: false, |
| 37 | }, |
| 38 | "Debug iOS": { |
| 39 | name: "Debug iOS", |
| 40 | cwd: "${workspaceFolder}", |
| 41 | type: DEBUG_TYPES.REACT_NATIVE, |
| 42 | request: "launch", |
| 43 | platform: PlatformType.iOS, |
| 44 | }, |
| 45 | "Run iOS": { |
| 46 | name: "Run iOS", |
| 47 | cwd: "${workspaceFolder}", |
| 48 | type: DEBUG_TYPES.REACT_NATIVE, |
| 49 | request: "launch", |
| 50 | platform: PlatformType.iOS, |
| 51 | enableDebug: false, |
| 52 | }, |
| 53 | "Debug Windows": { |
| 54 | name: "Debug Windows", |
| 55 | cwd: "${workspaceFolder}", |
| 56 | type: DEBUG_TYPES.REACT_NATIVE, |
| 57 | request: "launch", |
| 58 | platform: PlatformType.Windows, |
| 59 | }, |
| 60 | "Debug macOS": { |
| 61 | name: "Debug macOS", |
| 62 | cwd: "${workspaceFolder}", |
| 63 | type: DEBUG_TYPES.REACT_NATIVE, |
| 64 | request: "launch", |
| 65 | platform: PlatformType.macOS, |
| 66 | }, |
| 67 | "Attach to packager": { |
| 68 | name: "Attach to packager", |
| 69 | cwd: "${workspaceFolder}", |
| 70 | type: DEBUG_TYPES.REACT_NATIVE, |
| 71 | request: "attach", |
| 72 | }, |
| 73 | "Debug in Exponent": { |
| 74 | name: "Debug in Exponent", |
| 75 | cwd: "${workspaceFolder}", |
| 76 | type: DEBUG_TYPES.REACT_NATIVE, |
| 77 | request: "launch", |
| 78 | platform: PlatformType.Exponent, |
| 79 | }, |
| 80 | "Debug Android Hermes - Experimental": { |
| 81 | name: "Debug Android Hermes - Experimental", |
| 82 | cwd: "${workspaceFolder}", |
| 83 | type: DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 84 | request: "launch", |
| 85 | platform: PlatformType.Android, |
| 86 | }, |
| 87 | "Run Android Hermes - Experimental": { |
| 88 | name: "Run Android Hermes - Experimental", |
| 89 | cwd: "${workspaceFolder}", |
| 90 | type: DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 91 | request: "launch", |
| 92 | platform: PlatformType.Android, |
| 93 | enableDebug: false, |
| 94 | }, |
| 95 | "Attach to the React Native Hermes - Experimental": { |
| 96 | name: "Attach to the React Native Hermes - Experimental", |
| 97 | cwd: "${workspaceFolder}", |
| 98 | type: DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 99 | request: "attach", |
| 100 | }, |
| 101 | "Attach to the React Native iOS - Experimental": { |
| 102 | name: "Attach to the React Native iOS - Experimental", |
| 103 | cwd: "${workspaceFolder}", |
| 104 | type: DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 105 | request: "attach", |
| 106 | platform: PlatformType.iOS, |
| 107 | port: IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT, // 9221 |
| 108 | }, |
| 109 | "Debug Direct iOS - Experimental": { |
| 110 | name: "Debug Direct iOS - Experimental", |
| 111 | cwd: "${workspaceFolder}", |
| 112 | type: DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 113 | request: "launch", |
| 114 | platform: PlatformType.iOS, |
| 115 | port: IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT, // 9221 |
| 116 | }, |
| 117 | "Run Direct iOS - Experimental": { |
| 118 | name: "Run Direct iOS - Experimental", |
| 119 | cwd: "${workspaceFolder}", |
| 120 | type: DEBUG_TYPES.REACT_NATIVE_DIRECT, |
| 121 | request: "launch", |
| 122 | platform: PlatformType.iOS, |
| 123 | enableDebug: false, |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | private pickConfig: ReadonlyArray<vscode.QuickPickItem> = [ |
| 128 | { |
| 129 | label: "Debug Android", |
| 130 | description: localize("DebugAndroidConfigDesc", "Run and debug Android application"), |
| 131 | }, |
| 132 | { |
| 133 | label: "Run Android", |
| 134 | description: localize("RunAndroidConfigDesc", "Run Android application"), |
| 135 | }, |
| 136 | { |
| 137 | label: "Debug iOS", |
| 138 | description: localize("DebugiOSConfigDesc", "Run and debug iOS application"), |
| 139 | }, |
| 140 | { |
| 141 | label: "Run iOS", |
| 142 | description: localize("RuniOSConfigDesc", "Run iOS application"), |
| 143 | }, |
| 144 | { |
| 145 | label: "Debug Windows", |
| 146 | description: localize("DebugWindowsConfigDesc", "Run and debug Windows application"), |
| 147 | }, |
| 148 | { |
| 149 | label: "Debug macOS", |
| 150 | description: localize("DebugmacOSConfigDesc", "Run and debug macOS application"), |
| 151 | }, |
| 152 | { |
| 153 | label: "Attach to packager", |
| 154 | description: localize( |
| 155 | "AttachToPackagerConfigDesc", |
| 156 | "Attach to already working application packager", |
| 157 | ), |
| 158 | }, |
| 159 | { |
| 160 | label: "Debug in Exponent", |
| 161 | description: localize( |
| 162 | "DebugExpoConfigDesc", |
| 163 | "Debug Expo application or React Native application in Expo", |
| 164 | ), |
| 165 | }, |
| 166 | { |
| 167 | label: "Debug Android Hermes - Experimental", |
| 168 | description: localize( |
| 169 | "DebugAndroidHermesConfigDesc", |
| 170 | "Run and debug Android Hermes application", |
| 171 | ), |
| 172 | }, |
| 173 | { |
| 174 | label: "Attach to the React Native Hermes - Experimental", |
| 175 | description: localize( |
| 176 | "AttachToPackagerHermesConfigDesc", |
| 177 | "Attach to already working React Native Hermes application on Android directly", |
| 178 | ), |
| 179 | }, |
| 180 | { |
| 181 | label: "Attach to the React Native iOS - Experimental", |
| 182 | description: localize( |
| 183 | "AttachToPackageriOSConfigDesc", |
| 184 | "Attach to already working React Native iOS application directly", |
| 185 | ), |
| 186 | }, |
| 187 | { |
| 188 | label: "Debug Direct iOS - Experimental", |
| 189 | description: localize( |
| 190 | "DebugDirectiOSConfigDesc", |
| 191 | "Run and debug iOS application directly", |
| 192 | ), |
| 193 | }, |
| 194 | { |
| 195 | label: "Run Direct iOS - Experimental", |
| 196 | description: localize( |
| 197 | "RunDirectiOSConfigDesc", |
| 198 | "Run iOS application with direct debugging support", |
| 199 | ), |
| 200 | }, |
| 201 | ]; |
| 202 | |
| 203 | public async provideDebugConfigurations( |
| 204 | folder: vscode.WorkspaceFolder | undefined, // eslint-disable-line @typescript-eslint/no-unused-vars |
| 205 | token?: vscode.CancellationToken, // eslint-disable-line @typescript-eslint/no-unused-vars |
| 206 | ): Promise<vscode.DebugConfiguration[]> { |
| 207 | return new Promise<vscode.DebugConfiguration[]>(resolve => { |
| 208 | const configPicker = this.prepareDebugConfigPicker(); |
| 209 | const disposables: vscode.Disposable[] = []; |
| 210 | const pickHandler = () => { |
| 211 | let chosenConfigsEvent = TelemetryHelper.createTelemetryEvent( |
| 212 | "chosenDebugConfigurations", |
| 213 | ); |
| 214 | let selected: string[] = configPicker.selectedItems.map(element => element.label); |
| 215 | chosenConfigsEvent.properties["selectedItems"] = selected; |
| 216 | Telemetry.send(chosenConfigsEvent); |
| 217 | const launchConfig = this.gatherDebugScenarios(selected); |
| 218 | disposables.forEach(d => d.dispose()); |
| 219 | resolve(launchConfig); |
| 220 | }; |
| 221 | |
| 222 | disposables.push( |
| 223 | configPicker.onDidAccept(pickHandler), |
| 224 | configPicker.onDidHide(pickHandler), |
| 225 | configPicker, |
| 226 | ); |
| 227 | |
| 228 | configPicker.show(); |
| 229 | }); |
| 230 | } |
| 231 | |
| 232 | private gatherDebugScenarios(selectedItems: string[]): vscode.DebugConfiguration[] { |
| 233 | let launchConfig: vscode.DebugConfiguration[] = selectedItems.map( |
| 234 | element => this.debugConfigurations[element], |
| 235 | ); |
| 236 | return launchConfig; |
| 237 | } |
| 238 | |
| 239 | private prepareDebugConfigPicker(): vscode.QuickPick<vscode.QuickPickItem> { |
| 240 | const debugConfigPicker = vscode.window.createQuickPick(); |
| 241 | debugConfigPicker.canSelectMany = true; |
| 242 | debugConfigPicker.ignoreFocusOut = true; |
| 243 | debugConfigPicker.title = localize( |
| 244 | "DebugConfigQuickPickLabel", |
| 245 | "Pick debug configurations", |
| 246 | ); |
| 247 | debugConfigPicker.items = this.pickConfig; |
| 248 | // QuickPickItem property `picked` doesn't work, so this line will check first item in the list |
| 249 | // which is supposed to be Debug Android |
| 250 | debugConfigPicker.selectedItems = [this.pickConfig[0]]; |
| 251 | return debugConfigPicker; |
| 252 | } |
| 253 | } |
| 254 | |