-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathserverActions.ts
More file actions
281 lines (274 loc) · 9.73 KB
/
Copy pathserverActions.ts
File metadata and controls
281 lines (274 loc) · 9.73 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import * as vscode from "vscode";
import {
config,
workspaceState,
checkConnection,
explorerProvider,
filesystemSchemas,
FILESYSTEM_SCHEMA,
sendStudioAddinTelemetryEvent,
} from "../extension";
import {
connectionTarget,
terminalWithDocker,
shellWithDocker,
currentFile,
uriOfWorkspaceFolder,
notIsfs,
handleError,
} from "../utils";
import { mainCommandMenu, mainSourceControlMenu } from "./studio";
import { AtelierAPI } from "../api";
import { isfsConfig } from "../utils/FileProviderUtil";
type ServerAction = { detail: string; id: string; label: string; rawLink?: string };
export async function serverActions(): Promise<void> {
const { apiTarget, configName: workspaceFolder } = connectionTarget();
const api = new AtelierAPI(apiTarget);
const { active, host = "", ns = "", https, port = 0, pathPrefix, auth, docker } = api.config;
const explorerCount = (await explorerProvider.getChildren()).length;
if (!explorerCount && (!docker || host === "")) {
await vscode.commands.executeCommand("ObjectScriptExplorer.focus");
}
const { links } = config("conn");
const nsEncoded = encodeURIComponent(ns);
const actions: ServerAction[] = [];
const wsUri = uriOfWorkspaceFolder();
if (!api.externalServer) {
actions.push({
detail: (active ? "Disable" : "Enable") + " current connection",
id: "toggleConnection",
label: "Toggle Connection",
});
}
if (active || api.externalServer) {
actions.push({
id: "refreshConnection",
label: "Refresh Connection",
detail: "Force attempt to connect to the server",
});
// Switching namespace only makes sense for client-side folders
if (wsUri && notIsfs(wsUri)) {
actions.push({
id: "switchNamespace",
label: "Switch Namespace",
detail: "Switch to a different namespace in the current server",
});
}
}
const connectionActionsHandler = async (action: ServerAction): Promise<ServerAction> => {
if (!action) {
return;
}
switch (action.id) {
case "toggleConnection": {
const connConfig = config("", workspaceFolder);
const target = connConfig.inspect("conn").workspaceFolderValue
? vscode.ConfigurationTarget.WorkspaceFolder
: vscode.ConfigurationTarget.Workspace;
const targetConfig =
connConfig.inspect("conn").workspaceFolderValue || connConfig.inspect("conn").workspaceValue;
return connConfig.update("conn", { ...targetConfig, active: !active }, target);
}
case "refreshConnection": {
await checkConnection(true, undefined, true);
break;
}
case "switchNamespace": {
// List of all namespaces except the current one as it doesn't make sense to allow switching to the current one
let allNamespaces: string[] | undefined = await api
.serverInfo(false)
.then((data) => data.result.content.namespaces)
.catch((error) => {
handleError(error, "Failed to fetch a list of namespaces.");
return undefined;
});
if (!allNamespaces) {
return;
}
if (!allNamespaces.length) {
vscode.window.showErrorMessage(`You don't have access to any namespaces.`, "Dismiss");
return;
}
// Filter out the current namespace
allNamespaces = allNamespaces.filter((ns) => ns.toLowerCase() != api.config.ns.toLowerCase());
if (!allNamespaces.length) {
vscode.window.showErrorMessage(`You don't have access to any other namespaces.`, "Dismiss");
return;
}
const namespace = await vscode.window.showQuickPick(allNamespaces, {
title: "Pick the namespace to switch to",
});
if (namespace) {
const connConfig = config("", workspaceFolder);
const target = connConfig.inspect("conn").workspaceFolderValue
? vscode.ConfigurationTarget.WorkspaceFolder
: vscode.ConfigurationTarget.Workspace;
const targetConfig =
connConfig.inspect("conn").workspaceFolderValue || connConfig.inspect("conn").workspaceValue;
return connConfig.update("conn", { ...targetConfig, ns: namespace }, target);
}
break;
}
default:
return action;
}
};
if (!active || !host?.length || !port || !ns.length) {
return vscode.window
.showQuickPick(actions)
.then(connectionActionsHandler)
.then(() => {
return;
});
}
const file = currentFile();
const classname = file && file.name.toLowerCase().endsWith(".cls") ? file.name.slice(0, -4) : "";
const classnameEncoded = encodeURIComponent(classname);
const connInfo = `${host}:${port}${pathPrefix}[${nsEncoded.toUpperCase()}]`;
const serverUrl = `${https ? "https" : "http"}://${host}:${port}${pathPrefix}`;
const portalPath = `/csp/sys/UtilHome.csp?$NAMESPACE=${nsEncoded}`;
const classRef = `/csp/documatic/%25CSP.Documatic.cls?LIBRARY=${nsEncoded}${
classname ? "&CLASSNAME=" + classnameEncoded : ""
}`;
const project = wsUri ? isfsConfig(wsUri).project : "";
let extraLinks = 0;
for (const title in links) {
const rawLink = String(links[title]);
// Skip link if it requires a classname and we don't currently have one
if (classname == "" && (rawLink.includes("${classname}") || rawLink.includes("${classnameEncoded}"))) {
continue;
}
const link = rawLink
.replace("${host}", host)
.replace("${port}", port.toString())
.replace("${serverUrl}", serverUrl)
.replace("${serverAuth}", "")
.replace("${ns}", nsEncoded)
.replace("${namespace}", ns == "%SYS" ? "sys" : nsEncoded.toLowerCase())
.replace("${username}", auth.username)
.replace("${classname}", classname)
.replace("${classnameEncoded}", classnameEncoded)
.replace("${project}", project);
actions.push({
id: "extraLink" + extraLinks++,
label: title,
detail: link,
rawLink,
});
}
if (workspaceState.get(workspaceFolder.toLowerCase() + ":docker", false)) {
actions.push({
id: "openDockerTerminal",
label: "Open Terminal in Docker",
detail: "Use Docker Compose to start session inside configured service",
});
}
if (workspaceState.get(workspaceFolder.toLowerCase() + ":docker", false)) {
actions.push({
id: "openDockerShell",
label: "Open Shell in Docker",
detail: "Use Docker Compose to start shell inside configured service",
});
}
actions.push({
id: "openPortal",
label: "Open Management Portal",
detail: serverUrl + portalPath,
});
actions.push({
id: "openClassReference",
label: "Open Class Reference" + (classname ? ` for ${classname}` : ""),
detail: serverUrl + classRef,
});
actions.push({
id: "openStudioAddin",
label: "Open Studio Add-in...",
detail: "Pick a Studio Add-in to open",
});
if (
(!vscode.window.activeTextEditor && wsUri && wsUri.scheme == FILESYSTEM_SCHEMA) ||
vscode.window.activeTextEditor?.document.uri.scheme == FILESYSTEM_SCHEMA
) {
actions.push({
id: "serverSourceControlMenu",
label: "Server Source Control...",
detail: "Pick a server-side source control action to execute",
});
}
if (
(!vscode.window.activeTextEditor && wsUri && filesystemSchemas.includes(wsUri.scheme)) ||
filesystemSchemas.includes(vscode.window.activeTextEditor?.document.uri.scheme)
) {
actions.push({
id: "serverCommandMenu",
label: "Server Command Menu...",
detail: "Pick a server-side command to execute",
});
}
return vscode.window
.showQuickPick(actions, {
title: `Pick action to perform for server ${connInfo}`,
})
.then(connectionActionsHandler)
.then(async (action) => {
if (!action) {
return;
}
switch (action.id) {
case "openPortal": {
vscode.commands.executeCommand("workbench.action.browser.open", `${serverUrl}${portalPath}`);
break;
}
case "openClassReference": {
vscode.commands.executeCommand("workbench.action.browser.open", `${serverUrl}${classRef}`);
break;
}
case "openStudioAddin": {
const addins: ServerAction[] = await api
.actionQuery(
"SELECT Name AS label, Description AS detail, Url AS id FROM %CSP.StudioTemplateMgr_Templates('ADDIN')",
[]
)
.then((data) => data.result.content)
.catch((error) => {
handleError(error, "Failed to fetch list of Studio Add-ins.");
return undefined;
});
if (addins != undefined) {
const addin = await vscode.window.showQuickPick(addins, {
title: `Pick a Studio Add-In to open for server: ${connInfo}`,
});
if (addin) {
sendStudioAddinTelemetryEvent(addin.label);
let params = `Namespace=${nsEncoded}`;
params += `&User=${encodeURIComponent(auth.username)}`;
if (project != "") {
params += `&Project=${encodeURIComponent(project)}`;
}
vscode.commands.executeCommand("workbench.action.browser.open", `${serverUrl}${addin.id}?${params}`);
}
}
break;
}
case "openDockerTerminal": {
terminalWithDocker();
break;
}
case "openDockerShell": {
shellWithDocker();
break;
}
case "serverSourceControlMenu": {
mainSourceControlMenu();
break;
}
case "serverCommandMenu": {
mainCommandMenu();
break;
}
default: {
vscode.commands.executeCommand("workbench.action.browser.open", action.detail);
}
}
});
}