Skip to content

Commit d9caca5

Browse files
committed
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 <[email protected]>
1 parent 0569b23 commit d9caca5

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

package.json

+20-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"vscode": "^1.52.0"
2828
},
2929
"activationEvents": [
30-
"onLanguage:terramate"
30+
"onLanguage:terramate",
31+
"onCommand:terramate.createStack"
3132
],
3233
"main": "./out/extension",
3334
"contributes": {
@@ -87,7 +88,22 @@
8788
}
8889
}
8990
}
90-
]
91+
],
92+
"commands": [
93+
{
94+
"command": "terramate.createStack",
95+
"title": "Create Terramate Stack",
96+
"category": "Terramate"
97+
}
98+
],
99+
"menus": {
100+
"explorer/context": [
101+
{
102+
"command": "terramate.createStack",
103+
"group": "terramate"
104+
}
105+
]
106+
}
91107
},
92108
"scripts": {
93109
"vscode:prepublish": "npm run compile",
@@ -108,8 +124,8 @@
108124
"eslint": "^8.12.0",
109125
"mocha": "^9.2.1",
110126
"ovsx": "^0.5.0",
127+
"semver": "^7.3.7",
111128
"typescript": "^4.6.2",
112-
"vsce": "^2.9.1",
113-
"semver": "^7.3.7"
129+
"vsce": "^2.15.0"
114130
}
115131
}

src/extension.ts

+75-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17+
import path = require('path');
1718
import {
1819
workspace,
1920
ExtensionContext,
2021
ConfigurationScope,
21-
WorkspaceConfiguration
22+
WorkspaceConfiguration,
23+
commands,
24+
window,
25+
Uri,
2226
} from 'vscode';
2327

28+
import * as vscode from 'vscode';
29+
2430
import {
31+
ExecuteCommandParams,
32+
ExecuteCommandRequest,
2533
LanguageClient,
2634
LanguageClientOptions,
2735
} from 'vscode-languageclient/node';
@@ -50,10 +58,76 @@ export function activate(ctx: ExtensionContext) {
5058

5159
console.log("terramate-ls path: "+getServerPath(ctx));
5260

61+
const terramateStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0);
62+
63+
ctx.subscriptions.push(vscode.commands.registerCommand('terramate.createStack', async (dir:vscode.Uri) => {
64+
terramateStatus.text = "Creating stack";
65+
terramateStatus.show();
66+
if (dir === undefined) {
67+
const selected = await vscode.window.showOpenDialog({
68+
title: "Select the directory for the stack creation",
69+
canSelectFiles: false,
70+
canSelectFolders: true,
71+
canSelectMany: false,
72+
defaultUri: vscode.workspace.workspaceFolders[0].uri,
73+
openLabel: "Create Stack"
74+
});
75+
if (selected) {
76+
dir = selected[0];
77+
} else {
78+
vscode.window.showErrorMessage("no file selected");
79+
terramateStatus.hide();
80+
return;
81+
}
82+
}
83+
const name = await vscode.window.showInputBox({
84+
title: "Name of the stack",
85+
});
86+
const desc = await vscode.window.showInputBox({
87+
title: "Description of the stack",
88+
});
89+
const val = await vscode.window.showQuickPick(["yes", "no"], {
90+
title: "Generate the stack.id with an UUID v4?",
91+
});
92+
try {
93+
const res = await createStackCommand(client, dir, name, desc, val == "yes");
94+
console.log(res);
95+
} catch (err) {
96+
await vscode.window.showErrorMessage(err.toString());
97+
}
98+
terramateStatus.hide();
99+
}));
100+
53101
// Start the client. This will also launch the server
54102
ctx.subscriptions.push(client.start());
55103
}
56104

105+
async function createStackCommand(client: LanguageClient, moduleUri: vscode.Uri, name: string, desc: string, genid = true): Promise<any> {
106+
const args = [
107+
`uri=${moduleUri.toString()}`,
108+
];
109+
if (name !== '') {
110+
args.push(`name=${name}`);
111+
} else {
112+
args.push(`name=${path.basename(moduleUri.path)}`);
113+
}
114+
if (desc !== '') {
115+
args.push(`description=${desc}`);
116+
}
117+
if (genid) {
118+
args.push(`genid=true`);
119+
}
120+
const requestParams: ExecuteCommandParams = {
121+
command: `terramate.createStack`,
122+
arguments: args,
123+
};
124+
return execWorkspaceCommand(client, requestParams);
125+
}
126+
127+
function execWorkspaceCommand(client: LanguageClient, params: ExecuteCommandParams): Promise<any> {
128+
return client.sendRequest(ExecuteCommandRequest.type, params);
129+
}
130+
57131
export function deactivate(): Thenable<void> | undefined {
58132
if (!client) {
59133
return undefined;

0 commit comments

Comments
 (0)