microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
162lines · 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; |
| 18 | | |
2c19da7fRedMickey6 years ago | 19 | constructor(session: vscode.DebugSession) { |
| 20 | super(session); | |
984ca036RedMickey6 years ago | 21 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
2c19da7fRedMickey6 years ago | 22 | } |
| 23 | | |
984ca036RedMickey6 years ago | 24 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 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 | }); | |
984ca036RedMickey6 years ago | 63 | })) |
| 64 | .catch(err => this.showError(err.message, response)); | |
2c19da7fRedMickey6 years ago | 65 | } |
| 66 | | |
984ca036RedMickey6 years ago | 67 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 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`); | |
984ca036RedMickey6 years ago | 94 | return this.appLauncher.getRnCdpProxy().stopServer() |
| 95 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new DirectCDPMessageHandler(), this.cdpProxyLogLevel)) | |
| 96 | .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint(`http://localhost:${attachArgs.port}`, 90)) | |
| 97 | .then((browserInspectUri) => { | |
| 98 | this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri); | |
b7451aefRedMickey6 years ago | 99 | this.establishDebugSession(resolve); |
2c19da7fRedMickey6 years ago | 100 | }); |
| 101 | }) | |
| 102 | .catch((err) => { | |
| 103 | logger.error("An error occurred while attaching to the debugger. " + err.message || err); | |
| 104 | reject(err); | |
| 105 | }); | |
| 106 | }); | |
984ca036RedMickey6 years ago | 107 | })) |
| 108 | .catch(err => this.showError(err.message, response)); | |
2c19da7fRedMickey6 years ago | 109 | } |
| 110 | | |
984ca036RedMickey6 years ago | 111 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
2c19da7fRedMickey6 years ago | 112 | // The client is about to disconnect so first we need to stop app worker |
| 113 | if (this.appWorker) { | |
| 114 | this.appWorker.stop(); | |
| 115 | } | |
| 116 | | |
984ca036RedMickey6 years ago | 117 | await this.appLauncher.getRnCdpProxy().stopServer(); |
2c19da7fRedMickey6 years ago | 118 | |
| 119 | if (this.previousAttachArgs.platform === "android") { | |
| 120 | try { | |
| 121 | this.appLauncher.stopMonitoringLogCat(); | |
| 122 | } catch (err) { | |
| 123 | logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", err.message || err)); | |
| 124 | } | |
| 125 | } | |
| 126 | | |
| 127 | super.disconnectRequest(response, args, request); | |
| 128 | } | |
| 129 | | |
b7451aefRedMickey6 years ago | 130 | protected establishDebugSession(resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
| 131 | const attachArguments = { | |
| 132 | type: "pwa-node", | |
| 133 | request: "attach", | |
| 134 | name: "Attach", | |
| 135 | continueOnAttach: true, | |
| 136 | port: this.appLauncher.getCdpProxyPort(), | |
| 137 | smartStep: false, | |
| 138 | // The unique identifier of the debug session. It is used to distinguish React Native extension's | |
| 139 | // debug sessions from other ones. So we can save and process only the extension's debug sessions | |
| 140 | // in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession". | |
| 141 | rnDebugSessionId: this.session.id, | |
| 142 | }; | |
| 143 | | |
| 144 | vscode.debug.startDebugging( | |
| 145 | this.appLauncher.getWorkspaceFolder(), | |
| 146 | attachArguments, | |
| 147 | this.session | |
| 148 | ) | |
| 149 | .then((childDebugSessionStarted: boolean) => { | |
| 150 | if (childDebugSessionStarted) { | |
| 151 | if (resolve) { | |
| 152 | resolve(); | |
| 153 | } | |
| 154 | } else { | |
| 155 | throw new Error("Cannot start child debug session"); | |
| 156 | } | |
| 157 | }, | |
| 158 | err => { | |
| 159 | throw err; | |
| 160 | }); | |
| 161 | } | |
2c19da7fRedMickey6 years ago | 162 | } |