microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debuggingConfiguration/configurationProviders/attachConfigProvider.ts
144lines · modeblame
5471436aRedMickey5 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 { 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 | return () => this.configureAddress(input, config); | |
| 78 | } | |
| 79 | | |
| 80 | private async configureAddress( | |
| 81 | input: MultiStepInput<DebugConfigurationState>, | |
| 82 | config: Partial<ILaunchRequestArgs>, | |
| 83 | ): Promise<InputStep<DebugConfigurationState> | void> { | |
| 84 | delete config.address; | |
| 85 | let address = await input.showInputBox({ | |
| 86 | title: localize("AddressInputTitle", "The address of the host"), | |
| 87 | step: config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT ? 3 : 2, | |
| 88 | totalSteps: this.maxStepCount, | |
| 89 | value: this.defaultAddress, | |
| 90 | prompt: localize("AddressInputPrompt", "Enter the address of the host"), | |
| 91 | validate: value => | |
| 92 | Promise.resolve( | |
| 93 | value && value.trim().length > 0 | |
| 94 | ? undefined | |
| 95 | : localize("AddressInputInvalid", "Enter a valid host name or IP address"), | |
| 96 | ), | |
| 97 | }); | |
| 98 | | |
| 99 | if (address && address.trim() !== this.defaultAddress) { | |
| 100 | config.address = address.trim(); | |
| 101 | } | |
| 102 | | |
| 103 | return () => this.configurePort(input, config); | |
| 104 | } | |
| 105 | | |
| 106 | private async configurePort( | |
| 107 | input: MultiStepInput<DebugConfigurationState>, | |
| 108 | config: Partial<ILaunchRequestArgs>, | |
| 109 | ): Promise<InputStep<DebugConfigurationState> | void> { | |
| 110 | delete config.port; | |
| 111 | const defaultPort = String( | |
| 112 | config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT && config.platform === PlatformType.iOS | |
| 113 | ? IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT | |
| 114 | : Packager.DEFAULT_PORT, | |
| 115 | ); | |
| 116 | const portRegex = /^\d+$/; | |
| 117 | | |
| 118 | let portStr = await input.showInputBox({ | |
| 119 | title: localize("PortInputTitle", "The port of the host"), | |
| 120 | step: config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT ? 4 : 3, | |
| 121 | totalSteps: this.maxStepCount, | |
| 122 | value: defaultPort, | |
| 123 | prompt: localize( | |
| 124 | "PortInputPrompt", | |
| 125 | "Enter the port number that the debug server is listening on", | |
| 126 | ), | |
| 127 | validate: value => | |
| 128 | Promise.resolve( | |
| 129 | value && portRegex.test(value.trim()) | |
| 130 | ? undefined | |
| 131 | : localize("PortInputInvalid", "Enter a valid port number"), | |
| 132 | ), | |
| 133 | }); | |
| 134 | | |
| 135 | let portNumber: number | undefined; | |
| 136 | if (portStr && portRegex.test(portStr.trim())) { | |
| 137 | portNumber = parseInt(portStr, 10); | |
| 138 | } | |
| 139 | | |
| 140 | if (portNumber && portNumber !== Packager.DEFAULT_PORT) { | |
| 141 | config.port = portNumber; | |
| 142 | } | |
| 143 | } | |
| 144 | } |