microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c036b0d343245c82886cd5906e37ff50b8b30d34

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

206lines · 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";
2d8af448Yuri Skorokhodov6 years ago20nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
e23d1841RedMickey6 years ago21const localize = nls.loadMessageBundle();
2c19da7fRedMickey6 years ago22
23/**
24* Enum of possible statuses of debug session
25*/
26export enum DebugSessionStatus {
27/** A session has been just created */
28FirstConnection,
29/** This status is required in order to exclude the possible creation of several debug sessions at the first start */
30FirstConnectionPending,
31/** This status means that an application can be reloaded */
32ConnectionAllowed,
33/** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
34ConnectionPending,
35/** A debuggee connected successfully */
36ConnectionDone,
37/** A debuggee failed to connect */
38ConnectionFailed,
39}
40
ebbd64f1RedMickey6 years ago41export interface TerminateEventArgs {
42debugSession: vscode.DebugSession;
43args: any;
44}
45
2c19da7fRedMickey6 years ago46export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments, ILaunchArgs {
47cwd: string; /* Automatically set by VS Code to the currently opened folder */
48port: number;
49url?: string;
50address?: string;
51trace?: string;
5d47053fRedMickey6 years ago52skipFiles?: [];
1bdccb66RedMickey6 years ago53sourceMaps?: boolean;
54sourceMapPathOverrides?: { [key: string]: string };
2c19da7fRedMickey6 years ago55}
56
57export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IAttachRequestArgs { }
58
59export abstract class DebugSessionBase extends LoggingDebugSession {
60
ebbd64f1RedMickey6 years ago61protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
62public static readonly onDidTerminateRootDebugSession = DebugSessionBase.rootSessionTerminatedEventEmitter.event;
63
64protected readonly disconnectCommand: string;
65protected readonly pwaNodeSessionName: string;
66
2c19da7fRedMickey6 years ago67protected appLauncher: AppLauncher;
68protected projectRootPath: string;
69protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
70protected previousAttachArgs: IAttachRequestArgs;
71protected cdpProxyLogLevel: LogLevel;
72protected debugSessionStatus: DebugSessionStatus;
73protected session: vscode.DebugSession;
e23d1841RedMickey6 years ago74protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago75
76constructor(session: vscode.DebugSession) {
77super();
78
ebbd64f1RedMickey6 years ago79// constants definition
80this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
81this.disconnectCommand = "disconnect";
82
83// variables definition
2c19da7fRedMickey6 years ago84this.session = session;
85this.isSettingsInitialized = false;
86this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago87this.cancellationTokenSource = new vscode.CancellationTokenSource();
88}
89
90protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
91response.body = response.body || {};
92
93response.body.supportsConfigurationDoneRequest = true;
94response.body.supportsEvaluateForHovers = true;
95response.body.supportTerminateDebuggee = true;
96response.body.supportsCancelRequest = true;
97
98this.sendResponse(response);
2c19da7fRedMickey6 years ago99}
100
5d47053fRedMickey6 years ago101protected abstract establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void;
b7451aefRedMickey6 years ago102
2c19da7fRedMickey6 years ago103protected initializeSettings(args: any): Q.Promise<any> {
104if (!this.isSettingsInitialized) {
105let chromeDebugCoreLogs = getLoggingDirectory();
106if (chromeDebugCoreLogs) {
107chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
108}
109let logLevel: string = args.trace;
110if (logLevel) {
111logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
112logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
113this.cdpProxyLogLevel = LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
114} else {
115logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
116this.cdpProxyLogLevel = LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
117}
118
119if (!args.sourceMaps) {
120args.sourceMaps = true;
121}
122
5514e287RedMickey6 years ago123if (typeof args.enableDebug !== "boolean") {
124args.enableDebug = true;
125}
126
2c19da7fRedMickey6 years ago127const projectRootPath = getProjectRoot(args);
128return ReactNativeProjectHelper.isReactNativeProject(projectRootPath)
129.then((result) => {
130if (!result) {
131throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
132}
133this.projectRootPath = projectRootPath;
134this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath);
135this.isSettingsInitialized = true;
136
137return void 0;
138});
139} else {
140return Q.resolve<void>(void 0);
141}
142}
984ca036RedMickey6 years ago143
e23d1841RedMickey6 years ago144protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
145await this.appLauncher.getRnCdpProxy().stopServer();
146
147this.cancellationTokenSource.cancel();
148this.cancellationTokenSource.dispose();
149
150// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
6213678cYuri Skorokhodov6 years ago151if (this.previousAttachArgs && this.previousAttachArgs.platform === "android") {
e23d1841RedMickey6 years ago152try {
153this.appLauncher.stopMonitoringLogCat();
154} catch (err) {
155logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", err.message || err));
156}
157}
158
67ffa5b4RedMickey6 years ago159await logger.dispose();
160
ebbd64f1RedMickey6 years ago161DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
162debugSession: this.session,
163args: {
164forcedStop: (<any>args).forcedStop,
165},
166});
167
168this.sendResponse(response);
e23d1841RedMickey6 years ago169}
170
171protected showError(error: Error, response: DebugProtocol.Response): void {
172
173// We can't print error messages after the debugging session is stopped. This could break the extension work.
174if ((error instanceof InternalError || error instanceof NestedError)
175&& error.errorCode === InternalErrorCode.CancellationTokenTriggered
176) {
177return;
178}
179
984ca036RedMickey6 years ago180this.sendErrorResponse(
181response,
e23d1841RedMickey6 years ago182{ format: error.message, id: 1 },
984ca036RedMickey6 years ago183undefined,
184undefined,
185ErrorDestination.User
186);
187}
2c19da7fRedMickey6 years ago188}
189
190/**
191* Parses settings.json file for workspace root property
192*/
193export function getProjectRoot(args: any): string {
194const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
195const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
196try {
197let settingsContent = fs.readFileSync(settingsPath, "utf8");
198settingsContent = stripJsonComments(settingsContent);
199let parsedSettings = JSON.parse(settingsContent);
200let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot;
201return path.resolve(vsCodeRoot, projectRootPath);
202} catch (e) {
203logger.verbose(`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`);
204return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
205}
206}