microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8df5011e47ada44be3951868ba32e5fae98f48bb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

209lines · 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 path from "path";
6import * as fs from "fs";
7import stripJsonComments = require("strip-json-comments");
984ca036RedMickey6 years ago8import { LoggingDebugSession, Logger, logger, ErrorDestination } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago9import { DebugProtocol } from "vscode-debugprotocol";
10import { getLoggingDirectory, LogHelper } from "../extension/log/LogHelper";
11import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
12import { ErrorHelper } from "../common/error/errorHelper";
13import { InternalErrorCode } from "../common/error/internalErrorCode";
e23d1841RedMickey6 years ago14import { InternalError, NestedError } from "../common/error/internalError";
259c018fYuri Skorokhodov5 years ago15import { ILaunchArgs, PlatformType } from "../extension/launchArgs";
2c19da7fRedMickey6 years ago16import { AppLauncher } from "../extension/appLauncher";
17import { LogLevel } from "../extension/log/LogHelper";
e23d1841RedMickey6 years ago18import * as nls from "vscode-nls";
2d8af448Yuri Skorokhodov6 years ago19nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
e23d1841RedMickey6 years ago20const 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 {
259c018fYuri Skorokhodov5 years ago46webkitRangeMax: number;
47webkitRangeMin: number;
2c19da7fRedMickey6 years ago48cwd: string; /* Automatically set by VS Code to the currently opened folder */
49port: number;
50url?: string;
51address?: string;
52trace?: string;
5d47053fRedMickey6 years ago53skipFiles?: [];
1bdccb66RedMickey6 years ago54sourceMaps?: boolean;
55sourceMapPathOverrides?: { [key: string]: string };
2c19da7fRedMickey6 years ago56}
57
58export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IAttachRequestArgs { }
59
60export abstract class DebugSessionBase extends LoggingDebugSession {
61
ebbd64f1RedMickey6 years ago62protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
63public static readonly onDidTerminateRootDebugSession = DebugSessionBase.rootSessionTerminatedEventEmitter.event;
64
a2ddbba5RedMickey5 years ago65protected readonly stopCommand: string;
ebbd64f1RedMickey6 years ago66protected readonly pwaNodeSessionName: string;
67
2c19da7fRedMickey6 years ago68protected appLauncher: AppLauncher;
69protected projectRootPath: string;
70protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
71protected previousAttachArgs: IAttachRequestArgs;
72protected cdpProxyLogLevel: LogLevel;
73protected debugSessionStatus: DebugSessionStatus;
74protected session: vscode.DebugSession;
e23d1841RedMickey6 years ago75protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago76
77constructor(session: vscode.DebugSession) {
78super();
79
ebbd64f1RedMickey6 years ago80// constants definition
81this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
a2ddbba5RedMickey5 years ago82this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
ebbd64f1RedMickey6 years ago83
84// variables definition
2c19da7fRedMickey6 years ago85this.session = session;
86this.isSettingsInitialized = false;
87this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago88this.cancellationTokenSource = new vscode.CancellationTokenSource();
89}
90
91protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
92response.body = response.body || {};
93
94response.body.supportsConfigurationDoneRequest = true;
95response.body.supportsEvaluateForHovers = true;
96response.body.supportTerminateDebuggee = true;
97response.body.supportsCancelRequest = true;
98
99this.sendResponse(response);
2c19da7fRedMickey6 years ago100}
101
5d47053fRedMickey6 years ago102protected abstract establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void;
b7451aefRedMickey6 years ago103
ce5e88eeYuri Skorokhodov5 years ago104protected initializeSettings(args: any): Promise<any> {
2c19da7fRedMickey6 years ago105if (!this.isSettingsInitialized) {
106let chromeDebugCoreLogs = getLoggingDirectory();
107if (chromeDebugCoreLogs) {
108chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
109}
110let logLevel: string = args.trace;
111if (logLevel) {
112logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
113logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
114this.cdpProxyLogLevel = LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
115} else {
116logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
117this.cdpProxyLogLevel = LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
118}
119
2db9ac85Yuri Skorokhodov5 years ago120if (typeof args.sourceMaps !== "boolean") {
2c19da7fRedMickey6 years ago121args.sourceMaps = true;
122}
123
5514e287RedMickey6 years ago124if (typeof args.enableDebug !== "boolean") {
125args.enableDebug = true;
126}
127
2c19da7fRedMickey6 years ago128const projectRootPath = getProjectRoot(args);
129return ReactNativeProjectHelper.isReactNativeProject(projectRootPath)
130.then((result) => {
131if (!result) {
132throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
133}
134this.projectRootPath = projectRootPath;
135this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath);
136this.isSettingsInitialized = true;
137
138return void 0;
139});
140} else {
ce5e88eeYuri Skorokhodov5 years ago141return Promise.resolve();
2c19da7fRedMickey6 years ago142}
143}
984ca036RedMickey6 years ago144
e23d1841RedMickey6 years ago145protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
a32e1e1fYuri Skorokhodov5 years ago146if (this.appLauncher) {
147await this.appLauncher.getRnCdpProxy().stopServer();
148}
e23d1841RedMickey6 years ago149
150this.cancellationTokenSource.cancel();
151this.cancellationTokenSource.dispose();
152
153// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
259c018fYuri Skorokhodov5 years ago154if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
e23d1841RedMickey6 years ago155try {
8df5011eYuri Skorokhodov5 years ago156this.appLauncher.getMobilePlatform().dispose();
e23d1841RedMickey6 years ago157} catch (err) {
158logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", err.message || err));
159}
160}
161
67ffa5b4RedMickey5 years ago162await logger.dispose();
163
ebbd64f1RedMickey6 years ago164DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
165debugSession: this.session,
166args: {
a2ddbba5RedMickey5 years ago167forcedStop: !!(<any>args).forcedStop,
ebbd64f1RedMickey6 years ago168},
169});
170
171this.sendResponse(response);
e23d1841RedMickey6 years ago172}
173
174protected showError(error: Error, response: DebugProtocol.Response): void {
175
176// We can't print error messages after the debugging session is stopped. This could break the extension work.
177if ((error instanceof InternalError || error instanceof NestedError)
178&& error.errorCode === InternalErrorCode.CancellationTokenTriggered
179) {
180return;
181}
182
984ca036RedMickey6 years ago183this.sendErrorResponse(
184response,
e23d1841RedMickey6 years ago185{ format: error.message, id: 1 },
984ca036RedMickey6 years ago186undefined,
187undefined,
188ErrorDestination.User
189);
190}
2c19da7fRedMickey6 years ago191}
192
193/**
194* Parses settings.json file for workspace root property
195*/
196export function getProjectRoot(args: any): string {
197const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
198const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
199try {
200let settingsContent = fs.readFileSync(settingsPath, "utf8");
201settingsContent = stripJsonComments(settingsContent);
202let parsedSettings = JSON.parse(settingsContent);
203let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot;
204return path.resolve(vsCodeRoot, projectRootPath);
205} catch (e) {
206logger.verbose(`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`);
207return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
208}
209}