From d9caca56e9186f9fc7a8c7c14637765eb39ba770 Mon Sep 17 00:00:00 2001 From: Tiago Natel Date: Sat, 25 Nov 2023 04:32:26 +0000 Subject: [PATCH] feat: implements the terramate.createStack command. A new "createStack" command can be used from the command palette (CMD+Shift+P) or by right-click in a folder and selecting "Create Terramate Stack". Signed-off-by: Tiago Natel --- package.json | 24 ++++++++++++--- src/extension.ts | 76 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 91db559..78350a5 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "vscode": "^1.52.0" }, "activationEvents": [ - "onLanguage:terramate" + "onLanguage:terramate", + "onCommand:terramate.createStack" ], "main": "./out/extension", "contributes": { @@ -87,7 +88,22 @@ } } } - ] + ], + "commands": [ + { + "command": "terramate.createStack", + "title": "Create Terramate Stack", + "category": "Terramate" + } + ], + "menus": { + "explorer/context": [ + { + "command": "terramate.createStack", + "group": "terramate" + } + ] + } }, "scripts": { "vscode:prepublish": "npm run compile", @@ -108,8 +124,8 @@ "eslint": "^8.12.0", "mocha": "^9.2.1", "ovsx": "^0.5.0", + "semver": "^7.3.7", "typescript": "^4.6.2", - "vsce": "^2.9.1", - "semver": "^7.3.7" + "vsce": "^2.15.0" } } diff --git a/src/extension.ts b/src/extension.ts index 9e48ca7..8518f00 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,14 +14,22 @@ * limitations under the License. */ +import path = require('path'); import { workspace, ExtensionContext, ConfigurationScope, - WorkspaceConfiguration + WorkspaceConfiguration, + commands, + window, + Uri, } from 'vscode'; + import * as vscode from 'vscode'; + import { + ExecuteCommandParams, + ExecuteCommandRequest, LanguageClient, LanguageClientOptions, } from 'vscode-languageclient/node'; @@ -50,10 +58,76 @@ export function activate(ctx: ExtensionContext) { console.log("terramate-ls path: "+getServerPath(ctx)); + const terramateStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0); + + ctx.subscriptions.push(vscode.commands.registerCommand('terramate.createStack', async (dir:vscode.Uri) => { + terramateStatus.text = "Creating stack"; + terramateStatus.show(); + if (dir === undefined) { + const selected = await vscode.window.showOpenDialog({ + title: "Select the directory for the stack creation", + canSelectFiles: false, + canSelectFolders: true, + canSelectMany: false, + defaultUri: vscode.workspace.workspaceFolders[0].uri, + openLabel: "Create Stack" + }); + if (selected) { + dir = selected[0]; + } else { + vscode.window.showErrorMessage("no file selected"); + terramateStatus.hide(); + return; + } + } + const name = await vscode.window.showInputBox({ + title: "Name of the stack", + }); + const desc = await vscode.window.showInputBox({ + title: "Description of the stack", + }); + const val = await vscode.window.showQuickPick(["yes", "no"], { + title: "Generate the stack.id with an UUID v4?", + }); + try { + const res = await createStackCommand(client, dir, name, desc, val == "yes"); + console.log(res); + } catch (err) { + await vscode.window.showErrorMessage(err.toString()); + } + terramateStatus.hide(); + })); + // Start the client. This will also launch the server ctx.subscriptions.push(client.start()); } +async function createStackCommand(client: LanguageClient, moduleUri: vscode.Uri, name: string, desc: string, genid = true): Promise { + const args = [ + `uri=${moduleUri.toString()}`, + ]; + if (name !== '') { + args.push(`name=${name}`); + } else { + args.push(`name=${path.basename(moduleUri.path)}`); + } + if (desc !== '') { + args.push(`description=${desc}`); + } + if (genid) { + args.push(`genid=true`); + } + const requestParams: ExecuteCommandParams = { + command: `terramate.createStack`, + arguments: args, + }; + return execWorkspaceCommand(client, requestParams); +} + +function execWorkspaceCommand(client: LanguageClient, params: ExecuteCommandParams): Promise { + return client.sendRequest(ExecuteCommandRequest.type, params); +} + export function deactivate(): Thenable | undefined { if (!client) { return undefined;