microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ff70c05a6c821e99785e3f91dca1daf27094e4fd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

227lines · modeblame

6e1bcd36RedMickey6 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as vscode from "vscode";
5import * as path from "path";
6import * as mkdirp from "mkdirp";
2c19da7fRedMickey6 years ago7import { logger } from "vscode-debugadapter";
6e1bcd36RedMickey6 years ago8import { DebugProtocol } from "vscode-debugprotocol";
9import { ProjectVersionHelper } from "../common/projectVersionHelper";
10import { TelemetryHelper } from "../common/telemetryHelper";
11import { MultipleLifetimesAppWorker } from "./appWorker";
a6562589RedMickey6 years ago12import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler";
2c19da7fRedMickey6 years ago13import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase";
1bdccb66RedMickey6 years ago14import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter";
6e1bcd36RedMickey6 years ago15import * as nls from "vscode-nls";
2d8af448Yuri Skorokhodov6 years ago16nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
6e1bcd36RedMickey6 years ago17const localize = nls.loadMessageBundle();
18
2c19da7fRedMickey6 years ago19export class RNDebugSession extends DebugSessionBase {
6e1bcd36RedMickey6 years ago20
4c757eebRedMickey6 years ago21private readonly terminateCommand: string;
f872f4d5RedMickey6 years ago22
e23d1841RedMickey6 years ago23private appWorker: MultipleLifetimesAppWorker | null;
4c757eebRedMickey6 years ago24private nodeSession: vscode.DebugSession | null;
6e491635RedMickey6 years ago25private onDidStartDebugSessionHandler: vscode.Disposable;
26private onDidTerminateDebugSessionHandler: vscode.Disposable;
6e1bcd36RedMickey6 years ago27
2c19da7fRedMickey6 years ago28constructor(session: vscode.DebugSession) {
29super(session);
4c757eebRedMickey6 years ago30
31// constants definition
32this.terminateCommand = "terminate"; // the "terminate" command is sent from the client to the debug adapter in order to give the debuggee a chance for terminating itself
33
34// variables definition
e23d1841RedMickey6 years ago35this.appWorker = null;
36
6e491635RedMickey6 years ago37this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
4c757eebRedMickey6 years ago38this.handleStartDebugSession.bind(this)
39);
40
6e491635RedMickey6 years ago41this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
4c757eebRedMickey6 years ago42this.handleTerminateDebugSession.bind(this)
43);
6e1bcd36RedMickey6 years ago44}
45
984ca036RedMickey6 years ago46protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago47return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
48.then(() => {
49logger.log("Launching the application");
50logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
51
52this.appLauncher.launch(launchArgs)
53.then(() => {
54return this.appLauncher.getPackagerPort(launchArgs.cwd);
55})
56.then((packagerPort: number) => {
57launchArgs.port = launchArgs.port || packagerPort;
58this.attachRequest(response, launchArgs).then(() => {
59resolve();
60}).catch((e) => reject(e));
61})
62.catch((err) => {
2c19da7fRedMickey6 years ago63logger.error("An error occurred while launching the application. " + err.message || err);
6e1bcd36RedMickey6 years ago64reject(err);
65});
984ca036RedMickey6 years ago66}))
e23d1841RedMickey6 years ago67.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago68}
69
984ca036RedMickey6 years ago70protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago71let extProps = {
72platform: {
73value: attachArgs.platform,
74isPii: false,
75},
76};
77
78this.previousAttachArgs = attachArgs;
79return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
80.then(() => {
81logger.log("Attaching to the application");
82logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
83return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
84.then(versions => {
85extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
86if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
87extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
88}
89return TelemetryHelper.generate("attach", extProps, (generator) => {
a324603aRedMickey6 years ago90attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
984ca036RedMickey6 years ago91return this.appLauncher.getRnCdpProxy().stopServer()
92.then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel))
f872f4d5RedMickey6 years ago93.then(() => {
94logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker."));
95
96const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
97// Create folder if not exist to avoid problems if
98// RN project root is not a ${workspaceFolder}
99mkdirp.sync(sourcesStoragePath);
100
101// If launch is invoked first time, appWorker is undefined, so create it here
102this.appWorker = new MultipleLifetimesAppWorker(
103attachArgs,
104sourcesStoragePath,
105this.projectRootPath,
a324603aRedMickey6 years ago106undefined
107);
7e74daf7Yuri Skorokhodov6 years ago108this.appLauncher.setAppWorker(this.appWorker);
f872f4d5RedMickey6 years ago109
110this.appWorker.on("connected", (port: number) => {
111logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port));
112
a6562589RedMickey6 years ago113this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port);
4c757eebRedMickey6 years ago114
115if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
116return;
117}
d9c9ddcbRedMickey6 years ago118
4c757eebRedMickey6 years ago119if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
120this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
5d47053fRedMickey6 years ago121this.establishDebugSession(attachArgs, resolve);
4c757eebRedMickey6 years ago122} else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) {
123if (this.nodeSession) {
124this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
125this.nodeSession.customRequest(this.terminateCommand);
126}
f872f4d5RedMickey6 years ago127}
128});
129return this.appWorker.start();
6e1bcd36RedMickey6 years ago130});
131})
132.catch((err) => {
133logger.error("An error occurred while attaching to the debugger. " + err.message || err);
134reject(err);
135});
136});
984ca036RedMickey6 years ago137}))
e23d1841RedMickey6 years ago138.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago139}
140
984ca036RedMickey6 years ago141protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago142// The client is about to disconnect so first we need to stop app worker
143if (this.appWorker) {
144this.appWorker.stop();
145}
146
6e491635RedMickey6 years ago147this.onDidStartDebugSessionHandler.dispose();
148this.onDidTerminateDebugSessionHandler.dispose();
149
6e1bcd36RedMickey6 years ago150super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago151}
152
5d47053fRedMickey6 years ago153protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
1bdccb66RedMickey6 years ago154const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN(
155attachArgs,
156this.appLauncher.getCdpProxyPort(),
157this.session.id
158);
a6562589RedMickey6 years ago159
160vscode.debug.startDebugging(
161this.appLauncher.getWorkspaceFolder(),
1bdccb66RedMickey6 years ago162attachConfiguration,
ebbd64f1RedMickey6 years ago163{
164parentSession: this.session,
165consoleMode: vscode.DebugConsoleMode.MergeWithParent,
166}
a6562589RedMickey6 years ago167)
168.then((childDebugSessionStarted: boolean) => {
169if (childDebugSessionStarted) {
170this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
171this.setConnectionAllowedIfPossible();
172if (resolve) {
173this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
174resolve();
4c757eebRedMickey6 years ago175}
a6562589RedMickey6 years ago176} else {
4c757eebRedMickey6 years ago177this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
178this.setConnectionAllowedIfPossible();
179this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago180throw new Error("Cannot start child debug session");
181}
182},
183err => {
184this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
185this.setConnectionAllowedIfPossible();
4c757eebRedMickey6 years ago186this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago187throw err;
188});
4c757eebRedMickey6 years ago189}
6e1bcd36RedMickey6 years ago190
4c757eebRedMickey6 years ago191private handleStartDebugSession(debugSession: vscode.DebugSession) {
192if (
193debugSession.configuration.rnDebugSessionId === this.session.id
194&& debugSession.type === this.pwaNodeSessionName
195) {
196this.nodeSession = debugSession;
197}
198}
199
200private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
201if (
202debugSession.configuration.rnDebugSessionId === this.session.id
203&& debugSession.type === this.pwaNodeSessionName
204) {
ebbd64f1RedMickey6 years ago205if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
5d47053fRedMickey6 years ago206this.establishDebugSession(this.previousAttachArgs);
ebbd64f1RedMickey6 years ago207} else {
208this.session.customRequest(this.disconnectCommand, {forcedStop: true});
209}
4c757eebRedMickey6 years ago210}
211}
212
213private setConnectionAllowedIfPossible(): void {
214if (
215this.debugSessionStatus === DebugSessionStatus.ConnectionDone
216|| this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
217) {
218this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
219}
220}
221
222private resetFirstConnectionStatus(): void {
223if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
224this.debugSessionStatus = DebugSessionStatus.FirstConnection;
225}
226}
6e1bcd36RedMickey6 years ago227}