-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathdebugView.ts
More file actions
217 lines (190 loc) Β· 8.03 KB
/
debugView.ts
File metadata and controls
217 lines (190 loc) Β· 8.03 KB
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
207
208
209
210
211
212
213
214
215
216
217
import vscode from "vscode";
import { DebugConfiguration, getDebugServiceDetails, SERVICE_CERTIFICATE } from "../../api/configuration/DebugConfiguration";
import { Tools } from "../../api/Tools";
import { checkClientCertificate, remoteCertificatesExists } from "../../debug/certificates";
import { getDebugEngineJobs, isDebugEngineRunning, readActiveJob, readJVMInfo, startServer, startService, stopServer, stopService } from "../../debug/server";
import { instance } from "../../instantiate";
import { VscodeTools } from "../Tools";
import { BrowserItem } from "../types";
const title = "IBM i debugger";
type Certificates = {
remoteCertificate: boolean
remoteCertificatePath?: string
localCertificateIssue?: string
}
type DebugServiceIssue = {
label: string
detail?: string
context: string
}
export function initializeDebugBrowser(context: vscode.ExtensionContext) {
const debugBrowser = new DebugBrowser();
const debugTreeViewer = vscode.window.createTreeView(
`ibmiDebugBrowser`, {
treeDataProvider: debugBrowser,
showCollapseAll: true
});
const updateDebugBrowser = async () => {
const connection = instance.getConnection();
if (connection) {
debugTreeViewer.title = `${title} ${(await getDebugServiceDetails(connection)).version}`
debugTreeViewer.description = await isDebugEngineRunning() ? vscode.l10n.t(`Online`) : vscode.l10n.t(`Offline`);
}
else {
debugTreeViewer.title = title;
debugTreeViewer.description = "";
}
debugBrowser.refresh();
}
instance.subscribe(context, "connected", "Update Debug Browser", updateDebugBrowser);
instance.subscribe(context, "disconnected", "Update Debug Browser", updateDebugBrowser);
context.subscriptions.push(
debugTreeViewer,
vscode.commands.registerCommand("code-for-ibmi.debug.refresh", updateDebugBrowser),
vscode.commands.registerCommand("code-for-ibmi.debug.refresh.item", (item: DebugItem) => debugBrowser.refresh(item)),
vscode.commands.registerCommand("code-for-ibmi.debug.job.start", (item: DebugJobItem) => VscodeTools.withContext(`code-for-ibmi:debugWorking`, () => item.start())),
vscode.commands.registerCommand("code-for-ibmi.debug.job.stop", (item: DebugJobItem) => VscodeTools.withContext(`code-for-ibmi:debugWorking`, () => item.stop())),
vscode.commands.registerCommand("code-for-ibmi.debug.job.restart", async (item: DebugJobItem) => VscodeTools.withContext(`code-for-ibmi:debugWorking`, async () => await item.stop() && item.start())),
);
}
class DebugBrowser implements vscode.TreeDataProvider<BrowserItem> {
private readonly _emitter: vscode.EventEmitter<DebugItem | undefined | null | void> = new vscode.EventEmitter();
readonly onDidChangeTreeData: vscode.Event<DebugItem | undefined | null | void> = this._emitter.event;
refresh(item?: DebugItem) {
this._emitter.fire(item);
}
getTreeItem(element: DebugItem) {
return element;
}
async getChildren(item?: DebugItem) {
return VscodeTools.withContext(`code-for-ibmi:debugWorking`, async () => item?.getChildren?.() || this.getRootItems());
}
private async getRootItems() {
const connection = instance.getConnection();
if (connection) {
const debugConfig = await new DebugConfiguration(connection).load();
const certificates: Certificates = {
remoteCertificate: await remoteCertificatesExists(debugConfig),
remoteCertificatePath: debugConfig.getRemoteServiceCertificatePath()
};
if (certificates.remoteCertificate) {
try {
await checkClientCertificate(connection, debugConfig);
}
catch (error) {
certificates.localCertificateIssue = String(error);
}
}
const debugJobs = await getDebugEngineJobs();
return [
new DebugJobItem("server",
vscode.l10n.t(`Debug Server`),
{
startFunction: startServer,
stopFunction: stopServer,
debugJob: debugJobs.server
}),
new DebugJobItem("service",
vscode.l10n.t(`Debug Service`), {
startFunction: () => startService(connection),
stopFunction: () => stopService(connection),
debugJob: debugJobs.service,
debugConfig,
certificates
})
];
}
else {
return [];
}
}
async resolveTreeItem(item: vscode.TreeItem, element: BrowserItem, token: vscode.CancellationToken) {
const connection = instance.getConnection();
if (connection && element.tooltip === undefined && element instanceof DebugJobItem && element.parameters.debugJob) {
element.tooltip = new vscode.MarkdownString();
const activeJob = await readActiveJob(connection, element.parameters.debugJob);
if (activeJob) {
const jobToMarkDown = (job: Tools.DB2Row | string) => typeof job === "string" ? job : Object.entries(job).filter(([key, value]) => value !== null).map(([key, value]) => `- ${vscode.l10n.t(key)}: ${value}`).join("\n");
element.tooltip.appendMarkdown(jobToMarkDown(activeJob));
if (element.type === "service") {
element.tooltip.appendMarkdown("\n\n");
const jvmJob = await readJVMInfo(connection, element.parameters.debugJob);
if (jvmJob) {
element.tooltip.appendMarkdown(jobToMarkDown(jvmJob));
}
}
}
return element;
}
}
}
class DebugItem extends BrowserItem {
async refresh() {
vscode.commands.executeCommand("code-for-ibmi.debug.refresh.item", this);
}
}
class DebugJobItem extends DebugItem {
private problem: undefined | DebugServiceIssue;
constructor(readonly type: "server" | "service", label: string, readonly parameters: {
startFunction: () => Promise<boolean>,
stopFunction: () => Promise<boolean>,
debugJob?: string,
certificates?: Certificates,
debugConfig?: DebugConfiguration
}) {
let problem: undefined | DebugServiceIssue
let cantRun = false;
const running = !cantRun && parameters.debugJob !== undefined;
if (parameters.certificates && parameters.debugConfig) {
if (!parameters.certificates.remoteCertificate) {
cantRun = true;
problem = {
context: "noremote",
label: vscode.l10n.t(`Remote certificate not found`),
detail: vscode.l10n.t(`{0} not found under {1}`, SERVICE_CERTIFICATE, parameters.certificates.remoteCertificatePath!)
}
}
else if (parameters.certificates.localCertificateIssue) {
problem = {
context: "localissue",
label: parameters.certificates.localCertificateIssue
};
}
}
super(label, {
state: problem ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.None,
icon: problem ? "warning" : (running ? "pass" : "error"),
color: problem ? cantRun ? "testing.iconFailed" : "testing.iconQueued" : (running ? "testing.iconPassed" : "testing.iconFailed")
});
this.contextValue = `debugJob_${type}${cantRun ? '' : `_${running ? "on" : "off"}`}`;
this.problem = problem;
if (running) {
this.description = this.parameters.debugJob;
}
else {
this.description = vscode.l10n.t(`Offline`);
this.tooltip = "";
}
}
getChildren() {
if (this.problem) {
return [new CertificateIssueItem(this.problem)];
}
}
async start() {
const title = this.type === "server" ? vscode.l10n.t("Starting debug server...") : vscode.l10n.t("Starting debug service...");
return vscode.window.withProgress({ title, location: vscode.ProgressLocation.Window }, this.parameters.startFunction);
}
async stop() {
const title = this.type === "server" ? vscode.l10n.t("Stopping debug server...") : vscode.l10n.t("Stopping debug service...");
return vscode.window.withProgress({ title, location: vscode.ProgressLocation.Window }, this.parameters.stopFunction);
}
}
class CertificateIssueItem extends DebugItem {
constructor(issue: DebugServiceIssue) {
super(issue.label)
this.description = issue.detail;
this.tooltip = issue.detail || '';
this.contextValue = `certificateIssue_${issue.context}`;
}
}