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