microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e23d18413023bf3cf53b60f216b32f9b33263a28

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

174lines · modecode

1// 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");
9import { LoggingDebugSession, Logger, logger, ErrorDestination } from "vscode-debugadapter";
10import { 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";
15import { InternalError, NestedError } from "../common/error/internalError";
16import { ILaunchArgs } from "../extension/launchArgs";
17import { AppLauncher } from "../extension/appLauncher";
18import { LogLevel } from "../extension/log/LogHelper";
19import * as nls from "vscode-nls";
20const localize = nls.loadMessageBundle();
21
22/**
23 * Enum of possible statuses of debug session
24 */
25export enum DebugSessionStatus {
26 /** A session has been just created */
27 FirstConnection,
28 /** This status is required in order to exclude the possible creation of several debug sessions at the first start */
29 FirstConnectionPending,
30 /** This status means that an application can be reloaded */
31 ConnectionAllowed,
32 /** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
33 ConnectionPending,
34 /** A debuggee connected successfully */
35 ConnectionDone,
36 /** A debuggee failed to connect */
37 ConnectionFailed,
38}
39
40export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments, ILaunchArgs {
41 cwd: string; /* Automatically set by VS Code to the currently opened folder */
42 port: number;
43 url?: string;
44 address?: string;
45 trace?: string;
46}
47
48export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IAttachRequestArgs { }
49
50export abstract class DebugSessionBase extends LoggingDebugSession {
51
52 protected appLauncher: AppLauncher;
53 protected projectRootPath: string;
54 protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
55 protected previousAttachArgs: IAttachRequestArgs;
56 protected cdpProxyLogLevel: LogLevel;
57 protected debugSessionStatus: DebugSessionStatus;
58 protected session: vscode.DebugSession;
59 protected cancellationTokenSource: vscode.CancellationTokenSource;
60
61 constructor(session: vscode.DebugSession) {
62 super();
63
64 this.session = session;
65 this.isSettingsInitialized = false;
66 this.debugSessionStatus = DebugSessionStatus.FirstConnection;
67 this.cancellationTokenSource = new vscode.CancellationTokenSource();
68 }
69
70 protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
71 response.body = response.body || {};
72
73 response.body.supportsConfigurationDoneRequest = true;
74 response.body.supportsEvaluateForHovers = true;
75 response.body.supportTerminateDebuggee = true;
76 response.body.supportsTerminateRequest = true;
77 response.body.supportsCancelRequest = true;
78
79 this.sendResponse(response);
80 }
81
82 protected abstract establishDebugSession(resolve?: (value?: void | PromiseLike<void> | undefined) => void): void;
83
84 protected initializeSettings(args: any): Q.Promise<any> {
85 if (!this.isSettingsInitialized) {
86 let chromeDebugCoreLogs = getLoggingDirectory();
87 if (chromeDebugCoreLogs) {
88 chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
89 }
90 let logLevel: string = args.trace;
91 if (logLevel) {
92 logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
93 logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
94 this.cdpProxyLogLevel = LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
95 } else {
96 logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
97 this.cdpProxyLogLevel = LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
98 }
99
100 if (!args.sourceMaps) {
101 args.sourceMaps = true;
102 }
103
104 const projectRootPath = getProjectRoot(args);
105 return ReactNativeProjectHelper.isReactNativeProject(projectRootPath)
106 .then((result) => {
107 if (!result) {
108 throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
109 }
110 this.projectRootPath = projectRootPath;
111 this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath);
112 this.isSettingsInitialized = true;
113
114 return void 0;
115 });
116 } else {
117 return Q.resolve<void>(void 0);
118 }
119 }
120
121 protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
122 await this.appLauncher.getRnCdpProxy().stopServer();
123
124 this.cancellationTokenSource.cancel();
125 this.cancellationTokenSource.dispose();
126
127 // Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
128 if (this.previousAttachArgs.platform === "android") {
129 try {
130 this.appLauncher.stopMonitoringLogCat();
131 } catch (err) {
132 logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", err.message || err));
133 }
134 }
135
136 super.disconnectRequest(response, args, request);
137 }
138
139 protected showError(error: Error, response: DebugProtocol.Response): void {
140
141 // We can't print error messages after the debugging session is stopped. This could break the extension work.
142 if ((error instanceof InternalError || error instanceof NestedError)
143 && error.errorCode === InternalErrorCode.CancellationTokenTriggered
144 ) {
145 return;
146 }
147
148 this.sendErrorResponse(
149 response,
150 { format: error.message, id: 1 },
151 undefined,
152 undefined,
153 ErrorDestination.User
154 );
155 }
156}
157
158/**
159 * Parses settings.json file for workspace root property
160 */
161export function getProjectRoot(args: any): string {
162 const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
163 const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
164 try {
165 let settingsContent = fs.readFileSync(settingsPath, "utf8");
166 settingsContent = stripJsonComments(settingsContent);
167 let parsedSettings = JSON.parse(settingsContent);
168 let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot;
169 return path.resolve(vsCodeRoot, projectRootPath);
170 } catch (e) {
171 logger.verbose(`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`);
172 return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
173 }
174}
175