microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
204lines · modeblame
e45838cbVladimir Kotikov9 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 Q from "q"; | |
| 5 | import * as path from "path"; | |
| 6 | import * as fs from "fs"; | |
| 7 | import stripJsonComments = require("strip-json-comments"); | |
| 8 | | |
b8999098Dmitry Zinovyev9 years ago | 9 | import { Telemetry } from "../common/telemetry"; |
| 10 | import { TelemetryHelper } from "../common/telemetryHelper"; | |
| 11 | import { RemoteExtension } from "../common/remoteExtension"; | |
| 12 | import { ExtensionTelemetryReporter, ReassignableTelemetryReporter } from "../common/telemetryReporters"; | |
0a68f8dbArtem Egorov8 years ago | 13 | import { ChromeDebugSession, IChromeDebugSessionOpts, ChromeDebugAdapter, logger } from "vscode-chrome-debug-core"; |
| 14 | import { ContinuedEvent, TerminatedEvent, Logger } from "vscode-debugadapter"; | |
| 15 | import { DebugProtocol } from "vscode-debugprotocol"; | |
e45838cbVladimir Kotikov9 years ago | 16 | |
| 17 | import { MultipleLifetimesAppWorker } from "./appWorker"; | |
| 18 | | |
0a68f8dbArtem Egorov8 years ago | 19 | |
b8999098Dmitry Zinovyev9 years ago | 20 | export function makeSession( |
0a68f8dbArtem Egorov8 years ago | 21 | debugSessionClass: typeof ChromeDebugSession, |
| 22 | debugSessionOpts: IChromeDebugSessionOpts, | |
b8999098Dmitry Zinovyev9 years ago | 23 | telemetryReporter: ReassignableTelemetryReporter, |
0a68f8dbArtem Egorov8 years ago | 24 | appName: string, version: string): typeof ChromeDebugSession { |
e45838cbVladimir Kotikov9 years ago | 25 | |
| 26 | return class extends debugSessionClass { | |
| 27 | | |
| 28 | private projectRootPath: string; | |
| 29 | private remoteExtension: RemoteExtension; | |
5c8365a6Artem Egorov8 years ago | 30 | private appWorker: MultipleLifetimesAppWorker | null = null; |
e45838cbVladimir Kotikov9 years ago | 31 | |
| 32 | constructor(debuggerLinesAndColumnsStartAt1?: boolean, isServer?: boolean) { | |
| 33 | super(debuggerLinesAndColumnsStartAt1, isServer, debugSessionOpts); | |
| 34 | } | |
| 35 | | |
| 36 | // Override ChromeDebugSession's sendEvent to control what we will send to client | |
0a68f8dbArtem Egorov8 years ago | 37 | public sendEvent(event: DebugProtocol.Event): void { |
e45838cbVladimir Kotikov9 years ago | 38 | // Do not send "terminated" events signaling about session's restart to client as it would cause it |
| 39 | // to restart adapter's process, while we want to stay alive and don't want to interrupt connection | |
6c75098eVladimir9 years ago | 40 | // to packager. |
b8999098Dmitry Zinovyev9 years ago | 41 | |
720e992dArtem Egorov8 years ago | 42 | if (event.event === "terminated" && event.body && event.body.restart) { |
b8999098Dmitry Zinovyev9 years ago | 43 | |
| 44 | // Worker has been reloaded and switched to "continue" state | |
| 45 | // So we have to send "continued" event to client instead of "terminated" | |
| 46 | // Otherwise client might mistakenly show "stopped" state | |
0a68f8dbArtem Egorov8 years ago | 47 | let continuedEvent: ContinuedEvent = { |
b8999098Dmitry Zinovyev9 years ago | 48 | event: "continued", |
| 49 | type: "event", | |
| 50 | seq: event["seq"], // tslint:disable-line | |
| 51 | body: { threadId: event.body.threadId }, | |
| 52 | }; | |
| 53 | | |
| 54 | super.sendEvent(continuedEvent); | |
e45838cbVladimir Kotikov9 years ago | 55 | return; |
| 56 | } | |
| 57 | | |
| 58 | super.sendEvent(event); | |
| 59 | } | |
| 60 | | |
0a68f8dbArtem Egorov8 years ago | 61 | protected dispatchRequest(request: DebugProtocol.Request): void { |
e45838cbVladimir Kotikov9 years ago | 62 | if (request.command === "disconnect") |
| 63 | return this.disconnect(request); | |
| 64 | | |
| 65 | if (request.command === "attach") | |
| 66 | return this.attach(request); | |
| 67 | | |
| 68 | if (request.command === "launch") | |
| 69 | return this.launch(request); | |
| 70 | | |
| 71 | return super.dispatchRequest(request); | |
| 72 | } | |
| 73 | | |
0a68f8dbArtem Egorov8 years ago | 74 | private launch(request: DebugProtocol.Request): void { |
| 75 | this.requestSetup(request.arguments); | |
| 76 | this.remoteExtension.launch(request) | |
cbb0e869Artem Egorov8 years ago | 77 | .then(() => { |
2e432a9eArtem Egorov8 years ago | 78 | return this.remoteExtension.getPackagerPort(request.arguments.program); |
0a68f8dbArtem Egorov8 years ago | 79 | }) |
| 80 | .then((packagerPort: number) => { | |
| 81 | this.attachRequest(request, packagerPort); | |
| 82 | }) | |
| 83 | .catch(error => { | |
748105d9Artem Egorov8 years ago | 84 | this.bailOut(error.data || error.message); |
cbb0e869Artem Egorov8 years ago | 85 | }); |
e45838cbVladimir Kotikov9 years ago | 86 | } |
| 87 | | |
0a68f8dbArtem Egorov8 years ago | 88 | private attach(request: DebugProtocol.Request): void { |
| 89 | this.requestSetup(request.arguments); | |
2e432a9eArtem Egorov8 years ago | 90 | this.remoteExtension.getPackagerPort(request.arguments.program) |
0a68f8dbArtem Egorov8 years ago | 91 | .then((packagerPort: number) => { |
| 92 | this.attachRequest(request, packagerPort); | |
cbb0e869Artem Egorov8 years ago | 93 | }); |
e45838cbVladimir Kotikov9 years ago | 94 | } |
| 95 | | |
0a68f8dbArtem Egorov8 years ago | 96 | private disconnect(request: DebugProtocol.Request): void { |
e45838cbVladimir Kotikov9 years ago | 97 | // The client is about to disconnect so first we need to stop app worker |
f920e582Vladimir Kotikov9 years ago | 98 | if (this.appWorker) { |
| 99 | this.appWorker.stop(); | |
| 100 | } | |
e45838cbVladimir Kotikov9 years ago | 101 | |
| 102 | // Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session | |
0a68f8dbArtem Egorov8 years ago | 103 | if (request.arguments.platform === "android") { |
e45838cbVladimir Kotikov9 years ago | 104 | this.remoteExtension.stopMonitoringLogcat() |
0a68f8dbArtem Egorov8 years ago | 105 | .catch(reason => logger.warn(`Couldn't stop monitoring logcat: ${reason.message || reason}`)) |
b8999098Dmitry Zinovyev9 years ago | 106 | .finally(() => super.dispatchRequest(request)); |
e45838cbVladimir Kotikov9 years ago | 107 | } else { |
| 108 | super.dispatchRequest(request); | |
| 109 | } | |
| 110 | } | |
| 111 | | |
0a68f8dbArtem Egorov8 years ago | 112 | private requestSetup(args: any): void { |
| 113 | let logLevel: string = args.trace; | |
| 114 | if (logLevel) { | |
| 115 | logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase()); | |
| 116 | logger.setup(Logger.LogLevel[logLevel], false); | |
| 117 | } else { | |
| 118 | logger.setup(Logger.LogLevel.Log, false); | |
| 119 | } | |
| 120 | | |
e45838cbVladimir Kotikov9 years ago | 121 | this.projectRootPath = getProjectRoot(args); |
| 122 | this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath); | |
| 123 | | |
| 124 | // Start to send telemetry | |
| 125 | telemetryReporter.reassignTo(new ExtensionTelemetryReporter( | |
| 126 | appName, version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, this.projectRootPath)); | |
| 127 | } | |
| 128 | | |
| 129 | /** | |
| 130 | * Runs logic needed to attach. | |
| 131 | * Attach should: | |
| 132 | * - Enable js debugging | |
| 133 | */ | |
0a68f8dbArtem Egorov8 years ago | 134 | // tslint:disable-next-line:member-ordering |
| 135 | protected attachRequest( | |
| 136 | request: DebugProtocol.Request, | |
| 137 | packagerPort: number): Q.Promise<void> { | |
e45838cbVladimir Kotikov9 years ago | 138 | return TelemetryHelper.generate("attach", (generator) => { |
| 139 | return Q({}) | |
| 140 | .then(() => { | |
0a68f8dbArtem Egorov8 years ago | 141 | logger.log("Starting debugger app worker."); |
e45838cbVladimir Kotikov9 years ago | 142 | // TODO: remove dependency on args.program - "program" property is technically |
| 143 | // no more required in launch configuration and could be removed | |
| 144 | const workspaceRootPath = path.resolve(path.dirname(request.arguments.program), ".."); | |
| 145 | const sourcesStoragePath = path.join(workspaceRootPath, ".vscode", ".react"); | |
| 146 | | |
| 147 | // If launch is invoked first time, appWorker is undefined, so create it here | |
7daed3fcArtem Egorov8 years ago | 148 | this.appWorker = new MultipleLifetimesAppWorker(packagerPort, sourcesStoragePath, this.projectRootPath); |
e45838cbVladimir Kotikov9 years ago | 149 | this.appWorker.on("connected", (port: number) => { |
0a68f8dbArtem Egorov8 years ago | 150 | logger.log("Debugger worker loaded runtime on port " + port); |
e45838cbVladimir Kotikov9 years ago | 151 | // Don't mutate original request to avoid side effects |
| 152 | let attachArguments = Object.assign({}, request.arguments, { port, restart: true, request: "attach" }); | |
| 153 | let attachRequest = Object.assign({}, request, { command: "attach", arguments: attachArguments }); | |
| 154 | | |
6c75098eVladimir9 years ago | 155 | // Reinstantiate debug adapter, as the current implementation of ChromeDebugAdapter |
| 156 | // doesn't allow us to reattach to another debug target easily. As of now it's easier | |
| 157 | // to throw previous instance out and create a new one. | |
0a68f8dbArtem Egorov8 years ago | 158 | (this as any)._debugAdapter = new (<any>debugSessionOpts.adapter)(debugSessionOpts, this); |
e45838cbVladimir Kotikov9 years ago | 159 | super.dispatchRequest(attachRequest); |
| 160 | }); | |
| 161 | | |
| 162 | return this.appWorker.start(); | |
| 163 | }) | |
| 164 | .catch(error => this.bailOut(error.message)); | |
| 165 | }); | |
| 166 | } | |
| 167 | | |
| 168 | /** | |
| 169 | * Logs error to user and finishes the debugging process. | |
| 170 | */ | |
| 171 | private bailOut(message: string): void { | |
0a68f8dbArtem Egorov8 years ago | 172 | logger.error(`Could not debug. ${message}`); |
| 173 | this.sendEvent(new TerminatedEvent()); | |
27710197Vladimir Kotikov8 years ago | 174 | } |
e45838cbVladimir Kotikov9 years ago | 175 | }; |
| 176 | } | |
| 177 | | |
0a68f8dbArtem Egorov8 years ago | 178 | export function makeAdapter(debugAdapterClass: typeof ChromeDebugAdapter): typeof ChromeDebugAdapter { |
e45838cbVladimir Kotikov9 years ago | 179 | return class extends debugAdapterClass { |
| 180 | public doAttach(port: number, targetUrl?: string, address?: string, timeout?: number): Promise<void> { | |
4b86d595Vladimir Kotikov9 years ago | 181 | // We need to overwrite ChromeDebug's _attachMode to let Node2 adapter |
e45838cbVladimir Kotikov9 years ago | 182 | // to set up breakpoints on initial pause event |
0a68f8dbArtem Egorov8 years ago | 183 | (this as any)._attachMode = false; |
e45838cbVladimir Kotikov9 years ago | 184 | return super.doAttach(port, targetUrl, address, timeout); |
| 185 | } | |
| 186 | }; | |
| 187 | } | |
| 188 | | |
| 189 | /** | |
| 190 | * Parses settings.json file for workspace root property | |
| 191 | */ | |
| 192 | function getProjectRoot(args: any): string { | |
| 193 | try { | |
| 194 | let vsCodeRoot = path.resolve(args.program, "../.."); | |
| 195 | let settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json"); | |
| 196 | let settingsContent = fs.readFileSync(settingsPath, "utf8"); | |
| 197 | settingsContent = stripJsonComments(settingsContent); | |
| 198 | let parsedSettings = JSON.parse(settingsContent); | |
| 199 | let projectRootPath = parsedSettings["react-native-tools"].projectRoot; | |
| 200 | return path.resolve(vsCodeRoot, projectRootPath); | |
| 201 | } catch (e) { | |
| 202 | return path.resolve(args.program, "../.."); | |
| 203 | } | |
| 204 | } |