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