Skip to content

Commit

Permalink
feat: implements the terramate.createStack command.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
i4ki committed Nov 27, 2023
1 parent 0569b23 commit d9caca5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
24 changes: 20 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"vscode": "^1.52.0"
},
"activationEvents": [
"onLanguage:terramate"
"onLanguage:terramate",
"onCommand:terramate.createStack"
],
"main": "./out/extension",
"contributes": {
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
76 changes: 75 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<any> {
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<any> {
return client.sendRequest(ExecuteCommandRequest.type, params);
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
Expand Down

0 comments on commit d9caca5

Please sign in to comment.