microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debuggingConfiguration/configurationProviders/attachConfigProvider.ts
153lines · 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 { BaseConfigProvider } from "./baseConfigProvider"; |
| 5 | import { MultiStepInput, InputStep } from "../multiStepInput"; |
| 6 | import { ILaunchRequestArgs } from "../../../debugger/debugSessionBase"; |
| 7 | import { |
| 8 | DebugConfigurationState, |
| 9 | platformTypeDirectPickConfig, |
| 10 | DEBUG_TYPES, |
| 11 | DebugScenarioType, |
| 12 | } from "../debugConfigTypesAndConstants"; |
| 13 | import { PlatformType } from "../../launchArgs"; |
| 14 | import { IWDPHelper } from "../../../debugger/direct/IWDPHelper"; |
| 15 | import { Packager } from "../../../common/packager"; |
| 16 | import * as nls from "vscode-nls"; |
| 17 | nls.config({ |
| 18 | messageFormat: nls.MessageFormat.bundle, |
| 19 | bundleFormat: nls.BundleFormat.standalone, |
| 20 | })(); |
| 21 | const localize = nls.loadMessageBundle(); |
| 22 | |
| 23 | export class AttachConfigProvider extends BaseConfigProvider { |
| 24 | private readonly defaultAddress: string; |
| 25 | |
| 26 | constructor() { |
| 27 | super(); |
| 28 | this.defaultAddress = "localhost"; |
| 29 | this.maxStepCount = 3; |
| 30 | } |
| 31 | |
| 32 | public async buildConfiguration( |
| 33 | input: MultiStepInput<DebugConfigurationState>, |
| 34 | state: DebugConfigurationState, |
| 35 | ): Promise<InputStep<DebugConfigurationState> | void> { |
| 36 | this.maxStepCount = 3; |
| 37 | state.config = {}; |
| 38 | const config: Partial<ILaunchRequestArgs> = { |
| 39 | name: "Attach to application", |
| 40 | request: "attach", |
| 41 | type: DEBUG_TYPES.REACT_NATIVE, |
| 42 | cwd: "${workspaceFolder}", |
| 43 | }; |
| 44 | |
| 45 | state.scenarioType = DebugScenarioType.AttachApp; |
| 46 | |
| 47 | await this.configurationProviderHelper.selectApplicationType( |
| 48 | input, |
| 49 | config, |
| 50 | 1, |
| 51 | this.maxStepCount, |
| 52 | ); |
| 53 | |
| 54 | Object.assign(state.config, config); |
| 55 | |
| 56 | if (state.config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT) { |
| 57 | this.maxStepCount++; |
| 58 | return () => this.configureDirectPlatform(input, state.config); |
| 59 | } else { |
| 60 | return () => this.configureAddress(input, state.config); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private async configureDirectPlatform( |
| 65 | input: MultiStepInput<DebugConfigurationState>, |
| 66 | config: Partial<ILaunchRequestArgs>, |
| 67 | ): Promise<InputStep<DebugConfigurationState> | void> { |
| 68 | delete config.platform; |
| 69 | await this.configurationProviderHelper.selectPlatform( |
| 70 | input, |
| 71 | config, |
| 72 | platformTypeDirectPickConfig, |
| 73 | 2, |
| 74 | this.maxStepCount, |
| 75 | ); |
| 76 | |
| 77 | if (!config.platform) { |
| 78 | delete config.platform; |
| 79 | delete config.useHermesEngine; |
| 80 | } else { |
| 81 | config.useHermesEngine = false; |
| 82 | } |
| 83 | |
| 84 | return () => this.configureAddress(input, config); |
| 85 | } |
| 86 | |
| 87 | private async configureAddress( |
| 88 | input: MultiStepInput<DebugConfigurationState>, |
| 89 | config: Partial<ILaunchRequestArgs>, |
| 90 | ): Promise<InputStep<DebugConfigurationState> | void> { |
| 91 | delete config.address; |
| 92 | let address = await input.showInputBox({ |
| 93 | title: localize("AddressInputTitle", "The address of the host"), |
| 94 | step: config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT ? 3 : 2, |
| 95 | totalSteps: this.maxStepCount, |
| 96 | value: this.defaultAddress, |
| 97 | prompt: localize("AddressInputPrompt", "Enter the address of the host"), |
| 98 | validate: value => |
| 99 | Promise.resolve( |
| 100 | value && value.trim().length > 0 |
| 101 | ? undefined |
| 102 | : localize("AddressInputInvalid", "Enter a valid host name or IP address"), |
| 103 | ), |
| 104 | }); |
| 105 | |
| 106 | if (address && address.trim() !== this.defaultAddress) { |
| 107 | config.address = address.trim(); |
| 108 | } |
| 109 | |
| 110 | return () => this.configurePort(input, config); |
| 111 | } |
| 112 | |
| 113 | private async configurePort( |
| 114 | input: MultiStepInput<DebugConfigurationState>, |
| 115 | config: Partial<ILaunchRequestArgs>, |
| 116 | ): Promise<InputStep<DebugConfigurationState> | void> { |
| 117 | delete config.port; |
| 118 | const defaultPort = String( |
| 119 | config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT && |
| 120 | config.platform === PlatformType.iOS && |
| 121 | !config.useHermesEngine |
| 122 | ? IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT |
| 123 | : Packager.DEFAULT_PORT, |
| 124 | ); |
| 125 | const portRegex = /^\d+$/; |
| 126 | |
| 127 | let portStr = await input.showInputBox({ |
| 128 | title: localize("PortInputTitle", "The port of the host"), |
| 129 | step: config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT ? 4 : 3, |
| 130 | totalSteps: this.maxStepCount, |
| 131 | value: defaultPort, |
| 132 | prompt: localize( |
| 133 | "PortInputPrompt", |
| 134 | "Enter the port number that the debug server is listening on", |
| 135 | ), |
| 136 | validate: value => |
| 137 | Promise.resolve( |
| 138 | value && portRegex.test(value.trim()) |
| 139 | ? undefined |
| 140 | : localize("PortInputInvalid", "Enter a valid port number"), |
| 141 | ), |
| 142 | }); |
| 143 | |
| 144 | let portNumber: number | undefined; |
| 145 | if (portStr && portRegex.test(portStr.trim())) { |
| 146 | portNumber = parseInt(portStr, 10); |
| 147 | } |
| 148 | |
| 149 | if (portNumber && portNumber !== Packager.DEFAULT_PORT) { |
| 150 | config.port = portNumber; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |