microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debuggingConfiguration/reactNativeDebugConfigProvider.ts
297lines · modeblame
0bfa4e58Yuri Skorokhodov7 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 vscode from "vscode"; | |
09f6024fHeniker4 years ago | 5 | import * as nls from "vscode-nls"; |
5471436aRedMickey5 years ago | 6 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 7 | import { Telemetry } from "../../common/telemetry"; | |
| 8 | import { ILaunchRequestArgs } from "../../debugger/debugSessionBase"; | |
9f8c460dEzio Li2 years ago | 9 | import { PlatformType } from "../launchArgs"; |
5471436aRedMickey5 years ago | 10 | import { |
09f6024fHeniker4 years ago | 11 | debugConfigurations, |
| 12 | DEBUG_CONFIGURATION_NAMES, | |
5471436aRedMickey5 years ago | 13 | DEBUG_TYPES, |
| 14 | DebugScenarioType, | |
| 15 | DebugConfigurationQuickPickItem, | |
| 16 | DebugConfigurationState, | |
| 17 | } from "./debugConfigTypesAndConstants"; | |
09f6024fHeniker4 years ago | 18 | import { DebugScenarioNameGenerator } from "./debugScenarioNameGenerator"; |
| 19 | | |
5471436aRedMickey5 years ago | 20 | import { MultiStepInput, IMultiStepInput, InputStep, IQuickPickParameters } from "./multiStepInput"; |
| 21 | import { ConfigProviderFactory } from "./configurationProviders/configProviderFactory"; | |
09f6024fHeniker4 years ago | 22 | |
34472878RedMickey5 years ago | 23 | nls.config({ |
| 24 | messageFormat: nls.MessageFormat.bundle, | |
| 25 | bundleFormat: nls.BundleFormat.standalone, | |
| 26 | })(); | |
0bfa4e58Yuri Skorokhodov7 years ago | 27 | const localize = nls.loadMessageBundle(); |
| 28 | | |
| 29 | export class ReactNativeDebugConfigProvider implements vscode.DebugConfigurationProvider { | |
5471436aRedMickey5 years ago | 30 | private initialPickConfig: ReadonlyArray<vscode.QuickPickItem> = [ |
0bfa4e58Yuri Skorokhodov7 years ago | 31 | { |
e7a2c40dRedMickey4 years ago | 32 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_ANDROID, |
0bfa4e58Yuri Skorokhodov7 years ago | 33 | description: localize("DebugAndroidConfigDesc", "Run and debug Android application"), |
| 34 | }, | |
5514e287RedMickey6 years ago | 35 | { |
e7a2c40dRedMickey4 years ago | 36 | label: DEBUG_CONFIGURATION_NAMES.RUN_ANDROID, |
5514e287RedMickey6 years ago | 37 | description: localize("RunAndroidConfigDesc", "Run Android application"), |
| 38 | }, | |
0bfa4e58Yuri Skorokhodov7 years ago | 39 | { |
e7a2c40dRedMickey4 years ago | 40 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_IOS, |
0bfa4e58Yuri Skorokhodov7 years ago | 41 | description: localize("DebugiOSConfigDesc", "Run and debug iOS application"), |
| 42 | }, | |
5514e287RedMickey6 years ago | 43 | { |
e7a2c40dRedMickey4 years ago | 44 | label: DEBUG_CONFIGURATION_NAMES.RUN_IOS, |
5514e287RedMickey6 years ago | 45 | description: localize("RuniOSConfigDesc", "Run iOS application"), |
| 46 | }, | |
36e9730fDavid Serafimov6 years ago | 47 | { |
e7a2c40dRedMickey4 years ago | 48 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS, |
36e9730fDavid Serafimov6 years ago | 49 | description: localize("DebugWindowsConfigDesc", "Run and debug Windows application"), |
| 50 | }, | |
341dba36Yuri Skorokhodov5 years ago | 51 | { |
e7a2c40dRedMickey4 years ago | 52 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS, |
341dba36Yuri Skorokhodov5 years ago | 53 | description: localize("DebugmacOSConfigDesc", "Run and debug macOS application"), |
| 54 | }, | |
0bfa4e58Yuri Skorokhodov7 years ago | 55 | { |
e7a2c40dRedMickey4 years ago | 56 | label: DEBUG_CONFIGURATION_NAMES.ATTACH_TO_PACKAGER, |
34472878RedMickey5 years ago | 57 | description: localize( |
| 58 | "AttachToPackagerConfigDesc", | |
| 59 | "Attach to already working application packager", | |
| 60 | ), | |
0bfa4e58Yuri Skorokhodov7 years ago | 61 | }, |
| 62 | { | |
e7a2c40dRedMickey4 years ago | 63 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_IN_EXPONENT, |
34472878RedMickey5 years ago | 64 | description: localize( |
| 65 | "DebugExpoConfigDesc", | |
| 66 | "Debug Expo application or React Native application in Expo", | |
| 67 | ), | |
0bfa4e58Yuri Skorokhodov7 years ago | 68 | }, |
0b86204dEzio Li3 years ago | 69 | { |
| 70 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_IN_EXPONENT_HERMES_EXPERIMENTAL, | |
| 71 | description: localize( | |
| 72 | "DebugHermesExpoConfigDesc", | |
| 73 | "Debug Hermes Expo application or React Native application in Expo", | |
| 74 | ), | |
| 75 | }, | |
9f8c460dEzio Li2 years ago | 76 | { |
| 77 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_IN_EXPONENT_WEB_EXPERIMENTAL, | |
| 78 | description: localize( | |
| 79 | "DebugExpoWebConfigDesc", | |
| 80 | "Debug Hermes Expo application on web browser", | |
| 81 | ), | |
| 82 | }, | |
549baae2RedMickey6 years ago | 83 | { |
c6ca8545Ezio Li3 years ago | 84 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_ANDROID_HERMES, |
34472878RedMickey5 years ago | 85 | description: localize( |
| 86 | "DebugAndroidHermesConfigDesc", | |
| 87 | "Run and debug Android Hermes application", | |
| 88 | ), | |
549baae2RedMickey6 years ago | 89 | }, |
| 90 | { | |
c6ca8545Ezio Li3 years ago | 91 | label: DEBUG_CONFIGURATION_NAMES.RUN_ANDROID_HERMES, |
6f9a0779JiglioNero5 years ago | 92 | description: localize("RunAndroidHermesConfigDesc", "Run Android Hermes application"), |
| 93 | }, | |
| 94 | { | |
c6ca8545Ezio Li3 years ago | 95 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_IOS_HERMES, |
34472878RedMickey5 years ago | 96 | description: localize( |
6f9a0779JiglioNero5 years ago | 97 | "DebugIosHermesConfigDesc", |
| 98 | "Run and debug iOS Hermes application", | |
34472878RedMickey5 years ago | 99 | ), |
259c018fYuri Skorokhodov5 years ago | 100 | }, |
| 101 | { | |
c6ca8545Ezio Li3 years ago | 102 | label: DEBUG_CONFIGURATION_NAMES.RUN_IOS_HERMES, |
6f9a0779JiglioNero5 years ago | 103 | description: localize("RunIosHermesConfigDesc", "Run iOS Hermes application"), |
| 104 | }, | |
5c9b8eb8RedMickey5 years ago | 105 | { |
e7a2c40dRedMickey4 years ago | 106 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS_HERMES_EXPERIMENTAL, |
5c9b8eb8RedMickey5 years ago | 107 | description: localize( |
| 108 | "DebugMacOSHermesConfigDesc", | |
| 109 | "Run and debug macOS Hermes application", | |
| 110 | ), | |
| 111 | }, | |
008d88e5RedMickey4 years ago | 112 | { |
e7a2c40dRedMickey4 years ago | 113 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS_HERMES_EXPERIMENTAL, |
008d88e5RedMickey4 years ago | 114 | description: localize( |
| 115 | "DebugWindowsHermesConfigDesc", | |
| 116 | "Run and debug Windows Hermes application", | |
| 117 | ), | |
| 118 | }, | |
6f9a0779JiglioNero5 years ago | 119 | { |
c6ca8545Ezio Li3 years ago | 120 | label: DEBUG_CONFIGURATION_NAMES.ATTACH_TO_HERMES_APPLICATION, |
34472878RedMickey5 years ago | 121 | description: localize( |
6f9a0779JiglioNero5 years ago | 122 | "AttachToPackagerHermesConfigDesc", |
| 123 | "Attach to already working React Native Hermes application on Android directly", | |
34472878RedMickey5 years ago | 124 | ), |
259c018fYuri Skorokhodov5 years ago | 125 | }, |
| 126 | { | |
e7a2c40dRedMickey4 years ago | 127 | label: DEBUG_CONFIGURATION_NAMES.DEBUG_DIRECT_IOS_EXPERIMENTAL, |
34472878RedMickey5 years ago | 128 | description: localize( |
| 129 | "DebugDirectiOSConfigDesc", | |
| 130 | "Run and debug iOS application directly", | |
| 131 | ), | |
259c018fYuri Skorokhodov5 years ago | 132 | }, |
| 133 | { | |
e7a2c40dRedMickey4 years ago | 134 | label: DEBUG_CONFIGURATION_NAMES.RUN_DIRECT_IOS_EXPERIMENTAL, |
34472878RedMickey5 years ago | 135 | description: localize( |
| 136 | "RunDirectiOSConfigDesc", | |
| 137 | "Run iOS application with direct debugging support", | |
| 138 | ), | |
549baae2RedMickey6 years ago | 139 | }, |
6f9a0779JiglioNero5 years ago | 140 | { |
e7a2c40dRedMickey4 years ago | 141 | label: DEBUG_CONFIGURATION_NAMES.ATTACH_TO_DIRECT_IOS_EXPERIMENTAL, |
6f9a0779JiglioNero5 years ago | 142 | description: localize( |
| 143 | "AttachToPackageriOSConfigDesc", | |
| 144 | "Attach to already working React Native iOS application directly", | |
| 145 | ), | |
| 146 | }, | |
0bfa4e58Yuri Skorokhodov7 years ago | 147 | ]; |
| 148 | | |
5471436aRedMickey5 years ago | 149 | private sequentialPickConfig: ReadonlyArray<DebugConfigurationQuickPickItem> = [ |
| 150 | { | |
| 151 | label: "Run application", | |
| 152 | description: localize( | |
| 153 | "RunApplicationScenario", | |
| 154 | "Run React Native application without debugging", | |
| 155 | ), | |
| 156 | type: DebugScenarioType.RunApp, | |
| 157 | }, | |
| 158 | { | |
| 159 | label: "Debug application", | |
| 160 | description: localize("DebugApplicationScenario", "Debug React Native application"), | |
| 161 | type: DebugScenarioType.DebugApp, | |
| 162 | }, | |
| 163 | { | |
| 164 | label: "Attach to application", | |
| 165 | description: localize( | |
| 166 | "AttachApplicationScenario", | |
| 167 | "Attach to running React Native application", | |
| 168 | ), | |
| 169 | type: DebugScenarioType.AttachApp, | |
| 170 | }, | |
| 171 | ]; | |
| 172 | | |
34472878RedMickey5 years ago | 173 | public async provideDebugConfigurations( |
| 174 | folder: vscode.WorkspaceFolder | undefined, // eslint-disable-line @typescript-eslint/no-unused-vars | |
| 175 | token?: vscode.CancellationToken, // eslint-disable-line @typescript-eslint/no-unused-vars | |
| 176 | ): Promise<vscode.DebugConfiguration[]> { | |
| 177 | return new Promise<vscode.DebugConfiguration[]>(resolve => { | |
0bfa4e58Yuri Skorokhodov7 years ago | 178 | const configPicker = this.prepareDebugConfigPicker(); |
| 179 | const disposables: vscode.Disposable[] = []; | |
| 180 | const pickHandler = () => { | |
09f6024fHeniker4 years ago | 181 | const chosenConfigsEvent = TelemetryHelper.createTelemetryEvent( |
34472878RedMickey5 years ago | 182 | "chosenDebugConfigurations", |
| 183 | ); | |
09f6024fHeniker4 years ago | 184 | const selected: string[] = configPicker.selectedItems.map(element => element.label); |
| 185 | chosenConfigsEvent.properties.selectedItems = selected; | |
0bfa4e58Yuri Skorokhodov7 years ago | 186 | Telemetry.send(chosenConfigsEvent); |
| 187 | const launchConfig = this.gatherDebugScenarios(selected); | |
| 188 | disposables.forEach(d => d.dispose()); | |
| 189 | resolve(launchConfig); | |
| 190 | }; | |
| 191 | | |
| 192 | disposables.push( | |
| 193 | configPicker.onDidAccept(pickHandler), | |
| 194 | configPicker.onDidHide(pickHandler), | |
34472878RedMickey5 years ago | 195 | configPicker, |
0bfa4e58Yuri Skorokhodov7 years ago | 196 | ); |
| 197 | | |
| 198 | configPicker.show(); | |
| 199 | }); | |
| 200 | } | |
| 201 | | |
5471436aRedMickey5 years ago | 202 | public async provideDebugConfigurationSequentially( |
| 203 | folder: vscode.WorkspaceFolder | undefined, | |
| 204 | token?: vscode.CancellationToken, | |
| 205 | ): Promise<vscode.DebugConfiguration | undefined> { | |
| 206 | const config: Partial<ILaunchRequestArgs> = {}; | |
| 207 | const state = { config, scenarioType: DebugScenarioType.DebugApp, folder, token }; | |
| 208 | | |
| 209 | const multiStep = new MultiStepInput<DebugConfigurationState>(); | |
| 210 | await multiStep.run((input, s) => this.pickDebugConfiguration(input, s), state); | |
| 211 | | |
| 212 | if (Object.keys(state.config).length === 0) { | |
| 213 | return; | |
09f6024fHeniker4 years ago | 214 | } |
9f8c460dEzio Li2 years ago | 215 | if (state.config.platform !== PlatformType.ExpoWeb) { |
| 216 | if (state.config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT) { | |
| 217 | if ( | |
| 218 | state.config.platform === "android" || | |
| 219 | (state.config.platform === "ios" && | |
| 220 | state.config.target !== "device" && | |
| 221 | state.config.request !== "attach") | |
| 222 | ) { | |
| 223 | state.config.name = DebugScenarioNameGenerator.createScenarioName( | |
| 224 | state.scenarioType, | |
| 225 | state.config.type, | |
| 226 | state.config.platform, | |
| 227 | state.config.useHermesEngine !== false, | |
| 228 | ); | |
| 229 | } else { | |
| 230 | state.config.name = DebugScenarioNameGenerator.createScenarioName( | |
| 231 | state.scenarioType, | |
| 232 | state.config.type, | |
| 233 | state.config.platform, | |
| 234 | state.config.useHermesEngine !== false, | |
| 235 | true, | |
| 236 | ); | |
| 237 | } | |
c6ca8545Ezio Li3 years ago | 238 | } else { |
| 239 | state.config.name = DebugScenarioNameGenerator.createScenarioName( | |
| 240 | state.scenarioType, | |
9f8c460dEzio Li2 years ago | 241 | state.config.type || DEBUG_TYPES.REACT_NATIVE, |
c6ca8545Ezio Li3 years ago | 242 | state.config.platform, |
| 243 | ); | |
| 244 | } | |
5471436aRedMickey5 years ago | 245 | } else { |
09f6024fHeniker4 years ago | 246 | state.config.name = DebugScenarioNameGenerator.createScenarioName( |
| 247 | state.scenarioType, | |
| 248 | state.config.type || DEBUG_TYPES.REACT_NATIVE, | |
| 249 | state.config.platform, | |
| 250 | ); | |
5471436aRedMickey5 years ago | 251 | } |
9f8c460dEzio Li2 years ago | 252 | |
09f6024fHeniker4 years ago | 253 | return state.config as vscode.DebugConfiguration; |
5471436aRedMickey5 years ago | 254 | } |
| 255 | | |
| 256 | private async pickDebugConfiguration( | |
| 257 | input: IMultiStepInput<DebugConfigurationState>, | |
| 258 | state: DebugConfigurationState, | |
| 259 | ): Promise<InputStep<DebugConfigurationState> | void> { | |
| 260 | state.config = {}; | |
| 261 | const pick = await input.showQuickPick< | |
| 262 | DebugConfigurationQuickPickItem, | |
| 263 | IQuickPickParameters<DebugConfigurationQuickPickItem> | |
| 264 | >({ | |
| 265 | title: localize("DebugConfigQuickPickSequentialLabel", "Select a debug configuration"), | |
| 266 | placeholder: "Debug Configuration", | |
| 267 | activeItem: this.sequentialPickConfig[0], | |
| 268 | items: this.sequentialPickConfig, | |
| 269 | }); | |
| 270 | if (pick) { | |
| 271 | const provider = ConfigProviderFactory.create(pick.type); | |
| 272 | return provider.buildConfiguration.bind(provider); | |
| 273 | } | |
| 274 | } | |
| 275 | | |
0bfa4e58Yuri Skorokhodov7 years ago | 276 | private gatherDebugScenarios(selectedItems: string[]): vscode.DebugConfiguration[] { |
09f6024fHeniker4 years ago | 277 | const launchConfig: vscode.DebugConfiguration[] = selectedItems.map( |
e7a2c40dRedMickey4 years ago | 278 | element => debugConfigurations[element], |
34472878RedMickey5 years ago | 279 | ); |
0bfa4e58Yuri Skorokhodov7 years ago | 280 | return launchConfig; |
| 281 | } | |
| 282 | | |
| 283 | private prepareDebugConfigPicker(): vscode.QuickPick<vscode.QuickPickItem> { | |
| 284 | const debugConfigPicker = vscode.window.createQuickPick(); | |
| 285 | debugConfigPicker.canSelectMany = true; | |
| 286 | debugConfigPicker.ignoreFocusOut = true; | |
34472878RedMickey5 years ago | 287 | debugConfigPicker.title = localize( |
| 288 | "DebugConfigQuickPickLabel", | |
| 289 | "Pick debug configurations", | |
| 290 | ); | |
5471436aRedMickey5 years ago | 291 | debugConfigPicker.items = this.initialPickConfig; |
0bfa4e58Yuri Skorokhodov7 years ago | 292 | // QuickPickItem property `picked` doesn't work, so this line will check first item in the list |
| 293 | // which is supposed to be Debug Android | |
5471436aRedMickey5 years ago | 294 | debugConfigPicker.selectedItems = [this.initialPickConfig[0]]; |
0bfa4e58Yuri Skorokhodov7 years ago | 295 | return debugConfigPicker; |
| 296 | } | |
| 297 | } |