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