|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { AtelierAPI } from "../api"; |
| 3 | + |
| 4 | +export async function addServerNamespaceToWorkspace(): Promise<void> { |
| 5 | + const serverManagerApi = await getServerManagerApi(); |
| 6 | + if (!serverManagerApi) { |
| 7 | + vscode.window.showErrorMessage( |
| 8 | + "This command requires the [InterSystems Server Manager](https://marketplace.visualstudio.com/items?itemName=intersystems-community.servermanager) extension to be installed and enabled." |
| 9 | + ); |
| 10 | + return; |
| 11 | + } |
| 12 | + // Get user's choice of server |
| 13 | + const options: vscode.QuickPickOptions = { ignoreFocusOut: true }; |
| 14 | + const serverName: string = await serverManagerApi.pickServer(undefined, options); |
| 15 | + if (!serverName) { |
| 16 | + return; |
| 17 | + } |
| 18 | + // Get its namespace list |
| 19 | + let uri = vscode.Uri.parse(`isfs://${serverName}/?ns=%SYS`); |
| 20 | + const api = new AtelierAPI(uri); |
| 21 | + const allNamespaces: string[] = await api |
| 22 | + .serverInfo() |
| 23 | + .then((data) => data.result.content.namespaces |
| 24 | + ); |
| 25 | + // Prepare a displayable form of its connection spec as a hint to the user |
| 26 | + const connSpec = await serverManagerApi.getServerSpec(serverName); |
| 27 | + const connDisplayString = `${connSpec.webServer.scheme}://${connSpec.webServer.host}:${connSpec.webServer.port}/${connSpec.webServer.pathPrefix}`; |
| 28 | + // Get user's choice of namespace |
| 29 | + const namespace = await vscode.window.showQuickPick( |
| 30 | + allNamespaces, |
| 31 | + { |
| 32 | + placeHolder: `Namespace on server '${serverName}' (${connDisplayString})` |
| 33 | + } |
| 34 | + ); |
| 35 | + if (!namespace) { |
| 36 | + return; |
| 37 | + } |
| 38 | + // Pick between isfs and isfs-readonly |
| 39 | + const editable = await vscode.window.showQuickPick( |
| 40 | + [ |
| 41 | + { value: true, label: "Editable", detail: "Documents opened from this folder will be editable directly on the server." }, |
| 42 | + { value: false, label: "Read-only", detail: "Documents opened from this folder will be read-only." } |
| 43 | + ], |
| 44 | + { placeHolder: "Choose the mode of access" } |
| 45 | + ); |
| 46 | + // Prepare the folder parameters |
| 47 | + const label = editable.value ? `${serverName}:${namespace}` : `${serverName}:${namespace} (read-only)`; |
| 48 | + uri = uri.with({ scheme: editable.value ? "isfs" : "isfs-readonly", query: `ns=${namespace}` }); |
| 49 | + // Append it to the workspace |
| 50 | + const added = vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, 0, { uri, name: label }); |
| 51 | + // Switch to Explorer view so user sees the outcome |
| 52 | + vscode.commands.executeCommand('workbench.view.explorer'); |
| 53 | + // Handle failure |
| 54 | + if (!added) { |
| 55 | + vscode.window.showErrorMessage( |
| 56 | + "Folder not added. Maybe it already exists on the workspace.", |
| 57 | + "Retry", |
| 58 | + "Close" |
| 59 | + ) |
| 60 | + .then((value) => { |
| 61 | + if (value === "Retry") { |
| 62 | + vscode.commands.executeCommand('vscode-objectscript.addServerNamespaceToWorkspace'); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +async function getServerManagerApi(): Promise<any> { |
| 70 | + const targetExtension = vscode.extensions.getExtension("intersystems-community.servermanager"); |
| 71 | + if (!targetExtension) { |
| 72 | + return undefined; |
| 73 | + } |
| 74 | + if (!targetExtension.isActive) { |
| 75 | + await targetExtension.activate(); |
| 76 | + } |
| 77 | + const api = targetExtension.exports; |
| 78 | + |
| 79 | + if (!api) { |
| 80 | + return undefined; |
| 81 | + } |
| 82 | + return api; |
| 83 | +} |
0 commit comments