microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9442a7e6d775bd164f0d04d0bc3ff5d8b7775769

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

199lines · modeblame

2c19da7fRedMickey6 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 Q from "q";
6import * as path from "path";
7import * as fs from "fs";
8import stripJsonComments = require("strip-json-comments");
984ca036RedMickey6 years ago9import { LoggingDebugSession, Logger, logger, ErrorDestination } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago10import { DebugProtocol } from "vscode-debugprotocol";
11import { getLoggingDirectory, LogHelper } from "../extension/log/LogHelper";
12import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
13import { ErrorHelper } from "../common/error/errorHelper";
14import { InternalErrorCode } from "../common/error/internalErrorCode";
e23d1841RedMickey6 years ago15import { InternalError, NestedError } from "../common/error/internalError";
2c19da7fRedMickey6 years ago16import { ILaunchArgs } from "../extension/launchArgs";
17import { AppLauncher } from "../extension/appLauncher";
18import { LogLevel } from "../extension/log/LogHelper";
e23d1841RedMickey6 years ago19import * as nls from "vscode-nls";
20const localize = nls.loadMessageBundle();
2c19da7fRedMickey6 years ago21
22/**
23* Enum of possible statuses of debug session
24*/
25export enum DebugSessionStatus {
26/** A session has been just created */
27FirstConnection,
28/** This status is required in order to exclude the possible creation of several debug sessions at the first start */
29FirstConnectionPending,
30/** This status means that an application can be reloaded */
31ConnectionAllowed,
32/** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
33ConnectionPending,
34/** A debuggee connected successfully */
35ConnectionDone,
36/** A debuggee failed to connect */
37ConnectionFailed,
38}
39
ebbd64f1RedMickey6 years ago40export interface TerminateEventArgs {
41debugSession: vscode.DebugSession;
42args: any;
43}
44
2c19da7fRedMickey6 years ago45export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments, ILaunchArgs {
46cwd: string; /* Automatically set by VS Code to the currently opened folder */
47port: number;
48url?: string;
49address?: string;
50trace?: string;
5d47053fRedMickey6 years ago51skipFiles?: [];
1bdccb66RedMickey6 years ago52sourceMaps?: boolean;
53sourceMapPathOverrides?: { [key: string]: string };
2c19da7fRedMickey6 years ago54}
55
56export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IAttachRequestArgs { }
57
58export abstract class DebugSessionBase extends LoggingDebugSession {
59
ebbd64f1RedMickey6 years ago60protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
61public static readonly onDidTerminateRootDebugSession = DebugSessionBase.rootSessionTerminatedEventEmitter.event;
62
63protected readonly disconnectCommand: string;
64protected readonly pwaNodeSessionName: string;
65
2c19da7fRedMickey6 years ago66protected appLauncher: AppLauncher;
67protected projectRootPath: string;
68protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
69protected previousAttachArgs: IAttachRequestArgs;
70protected cdpProxyLogLevel: LogLevel;
71protected debugSessionStatus: DebugSessionStatus;
72protected session: vscode.DebugSession;
e23d1841RedMickey6 years ago73protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago74
75constructor(session: vscode.DebugSession) {
76super();
77
ebbd64f1RedMickey6 years ago78// constants definition
79this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
80this.disconnectCommand = "disconnect";
81
82// variables definition
2c19da7fRedMickey6 years ago83this.session = session;
84this.isSettingsInitialized = false;
85this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago86this.cancellationTokenSource = new vscode.CancellationTokenSource();
87}
88
89protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
90response.body = response.body || {};
91
92response.body.supportsConfigurationDoneRequest = true;
93response.body.supportsEvaluateForHovers = true;
94response.body.supportTerminateDebuggee = true;
95response.body.supportsCancelRequest = true;
96
97this.sendResponse(response);
2c19da7fRedMickey6 years ago98}
99
5d47053fRedMickey6 years ago100protected abstract establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void;
b7451aefRedMickey6 years ago101
2c19da7fRedMickey6 years ago102protected initializeSettings(args: any): Q.Promise<any> {
103if (!this.isSettingsInitialized) {
104let chromeDebugCoreLogs = getLoggingDirectory();
105if (chromeDebugCoreLogs) {
106chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
107}
108let logLevel: string = args.trace;
109if (logLevel) {
110logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
111logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
112this.cdpProxyLogLevel = LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
113} else {
114logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
115this.cdpProxyLogLevel = LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
116}
117
118if (!args.sourceMaps) {
119args.sourceMaps = true;
120}
121
122const projectRootPath = getProjectRoot(args);
123return ReactNativeProjectHelper.isReactNativeProject(projectRootPath)
124.then((result) => {
125if (!result) {
126throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
127}
128this.projectRootPath = projectRootPath;
129this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath);
130this.isSettingsInitialized = true;
131
132return void 0;
133});
134} else {
135return Q.resolve<void>(void 0);
136}
137}
984ca036RedMickey6 years ago138
e23d1841RedMickey6 years ago139protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
140await this.appLauncher.getRnCdpProxy().stopServer();
141
142this.cancellationTokenSource.cancel();
143this.cancellationTokenSource.dispose();
144
145// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
6213678cYuri Skorokhodov6 years ago146if (this.previousAttachArgs && this.previousAttachArgs.platform === "android") {
e23d1841RedMickey6 years ago147try {
148this.appLauncher.stopMonitoringLogCat();
149} catch (err) {
150logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", err.message || err));
151}
152}
153
ebbd64f1RedMickey6 years ago154DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
155debugSession: this.session,
156args: {
157forcedStop: (<any>args).forcedStop,
158},
159});
160
161this.sendResponse(response);
e23d1841RedMickey6 years ago162}
163
164protected showError(error: Error, response: DebugProtocol.Response): void {
165
166// We can't print error messages after the debugging session is stopped. This could break the extension work.
167if ((error instanceof InternalError || error instanceof NestedError)
168&& error.errorCode === InternalErrorCode.CancellationTokenTriggered
169) {
170return;
171}
172
984ca036RedMickey6 years ago173this.sendErrorResponse(
174response,
e23d1841RedMickey6 years ago175{ format: error.message, id: 1 },
984ca036RedMickey6 years ago176undefined,
177undefined,
178ErrorDestination.User
179);
180}
2c19da7fRedMickey6 years ago181}
182
183/**
184* Parses settings.json file for workspace root property
185*/
186export function getProjectRoot(args: any): string {
187const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
188const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
189try {
190let settingsContent = fs.readFileSync(settingsPath, "utf8");
191settingsContent = stripJsonComments(settingsContent);
192let parsedSettings = JSON.parse(settingsContent);
193let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot;
194return path.resolve(vsCodeRoot, projectRootPath);
195} catch (e) {
196logger.verbose(`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`);
197return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
198}
199}