microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4adeb45d4a2f292842d38deb2126dd90bb71bd20

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

155lines · 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 { ProjectVersionHelper } from "../../common/projectVersionHelper";
6import {logger } from "vscode-debugadapter";
7import { TelemetryHelper } from "../../common/telemetryHelper";
8import { DebugProtocol } from "vscode-debugprotocol";
9import { DirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/directCDPMessageHandler";
10import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase";
11import * as nls from "vscode-nls";
12const localize = nls.loadMessageBundle();
13
14export class DirectDebugSession extends DebugSessionBase {
15
16constructor(session: vscode.DebugSession) {
17super(session);
18}
19
20protected launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
21let extProps = {
22platform: {
23value: launchArgs.platform,
24isPii: false,
25},
26isDirect: {
27value: true,
28isPii: false,
29},
30};
31
32return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
33.then(() => {
34logger.log("Launching the application");
35logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
36return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows")
37.then(versions => {
38extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
39if (launchArgs.platform === "windows") {
40extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
41}
42return TelemetryHelper.generate("launch", extProps, (generator) => {
43return this.appLauncher.launch(launchArgs)
44.then(() => {
45return this.appLauncher.getPackagerPort(launchArgs.cwd);
46})
47.then((packagerPort: number) => {
48launchArgs.port = launchArgs.port || packagerPort;
49this.attachRequest(response, launchArgs).then(() => {
50resolve();
51}).catch((e) => reject(e));
52}).catch((e) => reject(e));
53})
54.catch((err) => {
55logger.error("An error occurred while launching the application. " + err.message || err);
56reject(err);
57});
58});
59}));
60}
61
62protected attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
63let extProps = {
64platform: {
65value: attachArgs.platform,
66isPii: false,
67},
68isDirect: {
69value: true,
70isPii: false,
71},
72};
73
74this.previousAttachArgs = attachArgs;
75
76return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
77.then(() => {
78logger.log("Attaching to the application");
79logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
80return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
81.then(versions => {
82extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
83if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
84extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
85}
86return TelemetryHelper.generate("attach", extProps, (generator) => {
87attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
88this.appLauncher.getRnCdpProxy().stopServer();
89logger.log(`Connecting to ${attachArgs.port} port`);
90return this.appLauncher.getRnCdpProxy().initializeServer(new DirectCDPMessageHandler(), this.cdpProxyLogLevel)
91.then(() => {
b7451aefRedMickey6 years ago92this.appLauncher.getRnCdpProxy().setApplicationTargetPort(attachArgs.port);
93this.establishDebugSession(resolve);
2c19da7fRedMickey6 years ago94});
95})
96.catch((err) => {
97logger.error("An error occurred while attaching to the debugger. " + err.message || err);
98reject(err);
99});
100});
101}));
102}
103
104protected disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): void {
105// The client is about to disconnect so first we need to stop app worker
106if (this.appWorker) {
107this.appWorker.stop();
108}
109
110this.appLauncher.getRnCdpProxy().stopServer();
111
112if (this.previousAttachArgs.platform === "android") {
113try {
114this.appLauncher.stopMonitoringLogCat();
115} catch (err) {
116logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", err.message || err));
117}
118}
119
120super.disconnectRequest(response, args, request);
121}
122
b7451aefRedMickey6 years ago123protected establishDebugSession(resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
124const attachArguments = {
125type: "pwa-node",
126request: "attach",
127name: "Attach",
128continueOnAttach: true,
129port: this.appLauncher.getCdpProxyPort(),
130smartStep: false,
131// The unique identifier of the debug session. It is used to distinguish React Native extension's
132// debug sessions from other ones. So we can save and process only the extension's debug sessions
133// in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession".
134rnDebugSessionId: this.session.id,
135};
136
137vscode.debug.startDebugging(
138this.appLauncher.getWorkspaceFolder(),
139attachArguments,
140this.session
141)
142.then((childDebugSessionStarted: boolean) => {
143if (childDebugSessionStarted) {
144if (resolve) {
145resolve();
146}
147} else {
148throw new Error("Cannot start child debug session");
149}
150},
151err => {
152throw err;
153});
154}
2c19da7fRedMickey6 years ago155}