-
Notifications
You must be signed in to change notification settings - Fork 6
/
debugConfigurationProvider.ts
206 lines (175 loc) · 8.58 KB
/
debugConfigurationProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Copyright (c) 2018-2020 Michael Potthoff
*
* This file is part of vscode-android-webview-debug.
*
* vscode-android-webview-debug is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* vscode-android-webview-debug is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with vscode-android-webview-debug. If not, see <http://www.gnu.org/licenses/>.
*/
import * as vscode from "vscode";
import * as vsls from "vsls";
import * as bridge from "./bridge";
import * as tasks from "./tasks";
import * as ui from "./ui";
export class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
public async resolveDebugConfiguration?(folder: vscode.WorkspaceFolder | undefined, debugConfiguration: vscode.DebugConfiguration, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration | null | undefined> {
const vslsApi = await vsls.getApi();
if (vslsApi && vslsApi.session.id && vslsApi.session.role === vsls.Role.Guest) {
// Do not do anything in a VS LiveShare guest instance. Everything
// has to be done in the host instance, otherwise ADB
// would be started in the guest instance.
return debugConfiguration;
}
if (!debugConfiguration.type || !debugConfiguration.request) {
// Empty configurations are unsupported
return null;
}
if (debugConfiguration.request !== "attach") {
// Only attach is supported right now
return null;
}
if (debugConfiguration.preLaunchTask) {
// Workaround for a configured preLaunchTask.
// The debug configuration is resolved before the preLaunchTask gets executed.
// This means the debugging connection would be established before the task gets executed,
// which would prevent the task from deploying the application.
const task = await tasks.findTask(debugConfiguration.preLaunchTask);
if (!task) {
let item;
if (typeof debugConfiguration.preLaunchTask === "string") {
item = await vscode.window.showErrorMessage(`Could not find the task '${debugConfiguration.preLaunchTask}'.`, {
modal: true
}, "Debug Anyway", "Configure Task", "Open launch.json");
} else {
item = await vscode.window.showErrorMessage("Could not find the specified task.", {
modal: true
}, "Debug Anyway", "Configure Task", "Open launch.json");
}
if (item === "Debug Anyway") {
// Continue
} else if (item === "Configure Task") {
vscode.commands.executeCommand("workbench.action.tasks.configureTaskRunner");
return undefined;
} else if (item === "Open launch.json") {
return null;
} else {
return undefined;
}
} else {
const result = await tasks.executeTask(task);
if (!result) {
return undefined;
}
}
delete debugConfiguration.preLaunchTask;
}
// Rewrite type to chrome
debugConfiguration.type = "chrome";
// Test the bridge to ensure that the required executables exist
await bridge.test();
return await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification
}, async (progress) => {
let device: bridge.Device | undefined;
let webView: bridge.WebView | undefined;
progress.report({ message: "Loading devices..." });
// Find the connected devices
const devices = await bridge.findDevices();
if (devices.length < 1) {
vscode.window.showErrorMessage(`No devices found`);
return undefined;
}
if (debugConfiguration.device) {
// Try to find the configured device
const found = devices.find((el) => el.serial === debugConfiguration.device);
if (!found) {
vscode.window.showErrorMessage(`Device '${debugConfiguration.device}' not found`);
return undefined;
}
device = found;
}
if (!device) {
if (debugConfiguration.application) {
progress.report({ message: "Loading WebViews..." });
// Find all devices that have the application running
const promises = devices.map(async (dev) => {
const webViews = await bridge.findWebViews(dev).catch((err): bridge.WebView[] => {
vscode.window.showWarningMessage(err.message);
return [];
});
return webViews.find((el) => el.packageName === debugConfiguration.application);
});
const result = await Promise.all(promises);
const filtered = result.filter((el) => el ? true : false) as bridge.WebView[];
if (filtered.length < 1) {
vscode.window.showErrorMessage(`No WebViews of '${debugConfiguration.application}' found on any device`);
return undefined;
} else if (filtered.length === 1) {
device = filtered[0].device;
webView = filtered[0];
} else {
// Ask the user to select a device
const filteredDevices = filtered.map((el) => el.device);
const pickedDevice = await ui.pickDevice(filteredDevices);
if (!pickedDevice) {
return undefined;
}
const pickedWebView = filtered.find((el) => el.device === pickedDevice);
if (!pickedWebView) {
return undefined;
}
device = pickedWebView.device;
webView = pickedWebView;
}
} else {
// Ask the user to select a connected device
const picked = await ui.pickDevice(devices);
if (!picked) {
return undefined;
}
device = picked;
}
}
if (!webView) {
progress.report({ message: "Loading WebViews..." });
// Find the running applications
const webViews = await bridge.findWebViews(device);
if (webViews.length < 1) {
vscode.window.showErrorMessage(`No WebViews found`);
return undefined;
}
if (debugConfiguration.application) {
// Try to find the configured application
const found = webViews.find((el) => el.packageName === debugConfiguration.application);
if (!found) {
vscode.window.showErrorMessage(`No WebViews of '${debugConfiguration.application}' found`);
return undefined;
}
webView = found;
} else {
// Ask the user to select a webview
const picked = await ui.pickWebView(webViews);
if (!picked) {
return undefined;
}
webView = picked;
}
}
progress.report({ message: "Forwarding debugger..." });
// Forward the debugger to the local port
debugConfiguration.port = await bridge.forwardDebugger(webView, debugConfiguration.port);
vscode.window.showInformationMessage(`Connected to ${webView.packageName} on ${webView.device.serial}`);
return debugConfiguration;
});
}
}