Skip to content

Commit 98f0ba2

Browse files
committed
feat: add ability to initialize a taskfile
1 parent acc5797 commit 98f0ba2

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This extension integrates your Taskfile into Visual Studio Code.
88
- Run tasks from the activity bar and command palette
99
- Go to definition
1010
- Multi-root workspace support
11+
- Ability to initialize a Taskfile in the current workspace
12+
- If no Taskfile is detected a button will appear in the activity bar
1113

1214
## Roadmap
1315

@@ -20,4 +22,3 @@ This extension integrates your Taskfile into Visual Studio Code.
2022
- Support global tasks
2123
- Install Task via command palette
2224
- Prompt if a Taskfile is detected and Task is not installed
23-
- If no Taskfile is detected, add a button to initialise one

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
"main": "./out/extension.js",
3737
"contributes": {
3838
"commands": [
39+
{
40+
"command": "vscode-task.init",
41+
"title": "Initialize Taskfile",
42+
"category": "Task",
43+
"icon": "$(plus)"
44+
},
3945
{
4046
"command": "vscode-task.refresh",
4147
"title": "Refresh Tasks",
@@ -84,6 +90,12 @@
8490
}
8591
]
8692
},
93+
"viewsWelcome": [
94+
{
95+
"view": "vscode-task.tasks",
96+
"contents": "No Taskfile found in this workspace. Click the button below to get started and initialize a new Taskfile.\n[Initialize Taskfile](command:vscode-task.init)\nTo learn more about how to use Task [read our docs](https://taskfile.dev)."
97+
}
98+
],
8799
"menus": {
88100
"commandPalette": [
89101
{

src/services/taskfile.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as cp from 'child_process';
22
import * as vscode from 'vscode';
33
import * as models from '../models';
4+
import * as path from 'path';
5+
import * as fs from 'fs';
46

57
class TaskfileService {
68
private static _instance: TaskfileService;
@@ -14,13 +16,39 @@ class TaskfileService {
1416
return this._instance ?? (this._instance = new this());
1517
}
1618

19+
public async init(dir: string): Promise<void> {
20+
return await new Promise((resolve) => {
21+
let command = 'task --init';
22+
cp.exec(command, { cwd: dir }, (_, stdout: string, stderr: string) => {
23+
if (stderr) {
24+
vscode.window.showErrorMessage(stderr);
25+
}
26+
this.open(dir).then(() => {
27+
return resolve();
28+
});
29+
});
30+
});
31+
}
32+
33+
public async open(dir: string): Promise<void> {
34+
let filenames = ['Taskfile.yml', 'Taskfile.yaml', 'taskfile.yml', 'taskfile.yaml'];
35+
for (let i = 0; i < filenames.length; i++) {
36+
let filename = path.join(dir, filenames[i]);
37+
if (fs.existsSync(filename)) {
38+
console.log(filename);
39+
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(filename), { preview: false });
40+
return;
41+
}
42+
}
43+
}
44+
1745
public async read(dir: string): Promise<models.Taskfile> {
1846
return await new Promise((resolve) => {
1947
let command = 'task --list-all --json';
2048
cp.exec(command, { cwd: dir }, (_, stdout: string) => {
2149
var taskfile: models.Taskfile = JSON.parse(stdout);
2250
taskfile.workspace = dir;
23-
resolve(taskfile);
51+
return resolve(taskfile);
2452
});
2553
});
2654
}
@@ -33,12 +61,12 @@ class TaskfileService {
3361
TaskfileService.outputChannel.append(stdout);
3462
TaskfileService.outputChannel.append("-----\n");
3563
TaskfileService.outputChannel.show();
36-
resolve();
64+
return resolve();
3765
});
3866
});
3967
}
4068

41-
public goToDefinition(task: models.Task, preview: boolean = false): void {
69+
public async goToDefinition(task: models.Task, preview: boolean = false): Promise<void> {
4270
if (task.location === undefined) {
4371
vscode.window.showErrorMessage(`Go to definition requires Task v3.23.0 or higher.`);
4472
return;
@@ -58,7 +86,7 @@ class TaskfileService {
5886

5987
// Run the vscode open command with the range options
6088
try {
61-
vscode.commands.executeCommand('vscode.open', file, options);
89+
await vscode.commands.executeCommand('vscode.open', file, options);
6290
} catch (err) {
6391
console.error(err);
6492
}

src/task.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ export class TaskExtension {
4040

4141
public registerCommands(context: vscode.ExtensionContext): void {
4242

43+
// Initialise Taskfile
44+
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.init', () => {
45+
if (vscode.workspace.workspaceFolders?.length === 1) {
46+
services.taskfile.init(vscode.workspace.workspaceFolders[0].uri.fsPath);
47+
return;
48+
}
49+
let items: vscode.QuickPickItem[] = [];
50+
vscode.workspace.workspaceFolders?.forEach((folder) => {
51+
items = items.concat({
52+
label: folder.name,
53+
description: folder.uri.fsPath
54+
});
55+
});
56+
vscode.window.showQuickPick(items).then((item) => {
57+
if (item) {
58+
services.taskfile.init(item.description || "");
59+
}
60+
});
61+
}));
62+
4363
// Refresh tasks
4464
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.refresh', () => {
4565
this.updateAndRefresh();

0 commit comments

Comments
 (0)