microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
172lines · modeblame
2c19da7fRedMickey6 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"; | |
| 5 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; | |
984ca036RedMickey6 years ago | 6 | import { logger } from "vscode-debugadapter"; |
2c19da7fRedMickey6 years ago | 7 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 8 | import { DebugProtocol } from "vscode-debugprotocol"; | |
| 9 | import { DirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/directCDPMessageHandler"; | |
| 10 | import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase"; | |
984ca036RedMickey6 years ago | 11 | import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper"; |
2c19da7fRedMickey6 years ago | 12 | import * as nls from "vscode-nls"; |
| 13 | const localize = nls.loadMessageBundle(); | |
| 14 | | |
| 15 | export class DirectDebugSession extends DebugSessionBase { | |
| 16 | | |
984ca036RedMickey6 years ago | 17 | private debuggerEndpointHelper: DebuggerEndpointHelper; |
ebbd64f1RedMickey6 years ago | 18 | private onDidTerminateDebugSessionHandler: vscode.Disposable; |
984ca036RedMickey6 years ago | 19 | |
2c19da7fRedMickey6 years ago | 20 | constructor(session: vscode.DebugSession) { |
| 21 | super(session); | |
984ca036RedMickey6 years ago | 22 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
ebbd64f1RedMickey6 years ago | 23 | |
| 24 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( | |
| 25 | this.handleTerminateDebugSession.bind(this) | |
| 26 | ); | |
2c19da7fRedMickey6 years ago | 27 | } |
| 28 | | |
984ca036RedMickey6 years ago | 29 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 30 | let extProps = { |
| 31 | platform: { | |
| 32 | value: launchArgs.platform, | |
| 33 | isPii: false, | |
| 34 | }, | |
| 35 | isDirect: { | |
| 36 | value: true, | |
| 37 | isPii: false, | |
| 38 | }, | |
| 39 | }; | |
| 40 | | |
| 41 | return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs) | |
| 42 | .then(() => { | |
| 43 | logger.log("Launching the application"); | |
| 44 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`); | |
| 45 | return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows") | |
| 46 | .then(versions => { | |
| 47 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); | |
| 48 | if (launchArgs.platform === "windows") { | |
| 49 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); | |
| 50 | } | |
| 51 | return TelemetryHelper.generate("launch", extProps, (generator) => { | |
| 52 | return this.appLauncher.launch(launchArgs) | |
| 53 | .then(() => { | |
| 54 | return this.appLauncher.getPackagerPort(launchArgs.cwd); | |
| 55 | }) | |
| 56 | .then((packagerPort: number) => { | |
| 57 | launchArgs.port = launchArgs.port || packagerPort; | |
| 58 | this.attachRequest(response, launchArgs).then(() => { | |
| 59 | resolve(); | |
| 60 | }).catch((e) => reject(e)); | |
| 61 | }).catch((e) => reject(e)); | |
| 62 | }) | |
| 63 | .catch((err) => { | |
| 64 | logger.error("An error occurred while launching the application. " + err.message || err); | |
| 65 | reject(err); | |
| 66 | }); | |
| 67 | }); | |
984ca036RedMickey6 years ago | 68 | })) |
e23d1841RedMickey6 years ago | 69 | .catch(err => this.showError(err, response)); |
2c19da7fRedMickey6 years ago | 70 | } |
| 71 | | |
984ca036RedMickey6 years ago | 72 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 73 | let extProps = { |
| 74 | platform: { | |
| 75 | value: attachArgs.platform, | |
| 76 | isPii: false, | |
| 77 | }, | |
| 78 | isDirect: { | |
| 79 | value: true, | |
| 80 | isPii: false, | |
| 81 | }, | |
| 82 | }; | |
| 83 | | |
| 84 | this.previousAttachArgs = attachArgs; | |
| 85 | | |
| 86 | return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs) | |
| 87 | .then(() => { | |
| 88 | logger.log("Attaching to the application"); | |
| 89 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`); | |
| 90 | return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true) | |
| 91 | .then(versions => { | |
| 92 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); | |
| 93 | if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) { | |
| 94 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); | |
| 95 | } | |
| 96 | return TelemetryHelper.generate("attach", extProps, (generator) => { | |
| 97 | attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); | |
| 98 | logger.log(`Connecting to ${attachArgs.port} port`); | |
984ca036RedMickey6 years ago | 99 | return this.appLauncher.getRnCdpProxy().stopServer() |
| 100 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new DirectCDPMessageHandler(), this.cdpProxyLogLevel)) | |
e23d1841RedMickey6 years ago | 101 | .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint( |
| 102 | `http://localhost:${attachArgs.port}`, | |
| 103 | 90, | |
| 104 | this.cancellationTokenSource.token | |
| 105 | )) | |
984ca036RedMickey6 years ago | 106 | .then((browserInspectUri) => { |
| 107 | this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri); | |
5d47053fRedMickey6 years ago | 108 | this.establishDebugSession(attachArgs, resolve); |
e23d1841RedMickey6 years ago | 109 | }) |
| 110 | .catch(e => reject(e)); | |
2c19da7fRedMickey6 years ago | 111 | }) |
| 112 | .catch((err) => { | |
| 113 | logger.error("An error occurred while attaching to the debugger. " + err.message || err); | |
| 114 | reject(err); | |
| 115 | }); | |
| 116 | }); | |
984ca036RedMickey6 years ago | 117 | })) |
e23d1841RedMickey6 years ago | 118 | .catch(err => this.showError(err, response)); |
2c19da7fRedMickey6 years ago | 119 | } |
| 120 | | |
984ca036RedMickey6 years ago | 121 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
ebbd64f1RedMickey6 years ago | 122 | this.onDidTerminateDebugSessionHandler.dispose(); |
| 123 | | |
2c19da7fRedMickey6 years ago | 124 | super.disconnectRequest(response, args, request); |
| 125 | } | |
| 126 | | |
5d47053fRedMickey6 years ago | 127 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
b7451aefRedMickey6 years ago | 128 | const attachArguments = { |
| 129 | type: "pwa-node", | |
| 130 | request: "attach", | |
| 131 | name: "Attach", | |
| 132 | continueOnAttach: true, | |
| 133 | port: this.appLauncher.getCdpProxyPort(), | |
| 134 | smartStep: false, | |
5d47053fRedMickey6 years ago | 135 | skipFiles: attachArgs.skipFiles || [], |
b7451aefRedMickey6 years ago | 136 | // The unique identifier of the debug session. It is used to distinguish React Native extension's |
| 137 | // debug sessions from other ones. So we can save and process only the extension's debug sessions | |
| 138 | // in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession". | |
| 139 | rnDebugSessionId: this.session.id, | |
| 140 | }; | |
| 141 | | |
| 142 | vscode.debug.startDebugging( | |
| 143 | this.appLauncher.getWorkspaceFolder(), | |
| 144 | attachArguments, | |
ebbd64f1RedMickey6 years ago | 145 | { |
| 146 | parentSession: this.session, | |
| 147 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
| 148 | } | |
b7451aefRedMickey6 years ago | 149 | ) |
| 150 | .then((childDebugSessionStarted: boolean) => { | |
| 151 | if (childDebugSessionStarted) { | |
| 152 | if (resolve) { | |
| 153 | resolve(); | |
| 154 | } | |
| 155 | } else { | |
e23d1841RedMickey6 years ago | 156 | throw new Error(localize("CouldNotStartChildDebugSession", "Couldn't start child debug session")); |
b7451aefRedMickey6 years ago | 157 | } |
| 158 | }, | |
| 159 | err => { | |
| 160 | throw err; | |
| 161 | }); | |
| 162 | } | |
ebbd64f1RedMickey6 years ago | 163 | |
| 164 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { | |
| 165 | if ( | |
| 166 | debugSession.configuration.rnDebugSessionId === this.session.id | |
| 167 | && debugSession.type === this.pwaNodeSessionName | |
| 168 | ) { | |
| 169 | this.session.customRequest(this.disconnectCommand, {forcedStop: true}); | |
| 170 | } | |
| 171 | } | |
2c19da7fRedMickey6 years ago | 172 | } |