Skip to content

Commit 34a8505

Browse files
committed
feat: refresh when taskfile is changed
1 parent 60767c6 commit 34a8505

File tree

5 files changed

+84
-12
lines changed

5 files changed

+84
-12
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ This extension integrates your Taskfile into Visual Studio Code.
1111

1212
- Run last task command
1313
- Switch between nested/flat task view
14-
- Refresh tasks when Taskfile is changed
1514
- Refresh up-to-date status when a task's sources change
1615
- Support global tasks
1716
- Support workspaces

package.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,27 @@
125125
"highContrastLight": "#AAAAAA"
126126
}
127127
}
128-
]
128+
],
129+
"configuration": {
130+
"title": "Task configuration",
131+
"properties": {
132+
"task": {
133+
"type": "object",
134+
"description": "Task configuration options.",
135+
"properties": {
136+
"updateOn": {
137+
"type": "string",
138+
"enum": [
139+
"save",
140+
"manual"
141+
],
142+
"default": "save",
143+
"description": "When the list of tasks should be updated."
144+
}
145+
}
146+
}
147+
}
148+
}
129149
},
130150
"scripts": {
131151
"vscode:prepublish": "yarn run compile",

src/extension.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ export function activate(context: vscode.ExtensionContext) {
88

99
// Registration
1010
taskExtension.registerCommands(context);
11+
taskExtension.registerListeners(context);
1112

1213
// Refresh the tasks list
13-
taskExtension.update().then(() => {
14-
taskExtension.refresh();
15-
}).catch((err: string) => {
16-
console.error(err);
17-
});
14+
taskExtension.updateAndRefresh();
1815
}
1916

2017
export function deactivate() { }

src/settings.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as vscode from 'vscode';
2+
3+
4+
export class Settings {
5+
6+
constructor() {
7+
this.update();
8+
}
9+
10+
// Variables
11+
public updateOn!: string;
12+
13+
// Fetches all the settings from the workspace configuration file
14+
public update() {
15+
// Get the workspace config
16+
let config = vscode.workspace.getConfiguration("task");
17+
18+
// Get the configs
19+
let updateOn: string | undefined = config.get("updateOn");
20+
21+
// Set the configs
22+
this.updateOn = updateOn !== undefined ? updateOn : "change";
23+
}
24+
}

src/task.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import * as elements from './elements';
33
import * as services from './services';
44
import * as models from './models';
55
import { TaskTreeItem } from './providers/tasks';
6+
import { Settings } from './settings';
67

78
export class TaskExtension {
89
private _taskfile?: models.Taskfile;
10+
private _settings: Settings;
911
private _activityBar: elements.ActivityBar;
12+
private _watcher: vscode.FileSystemWatcher;
1013

1114
constructor() {
15+
this._settings = new Settings();
1216
this._activityBar = new elements.ActivityBar();
17+
this._watcher = vscode.workspace.createFileSystemWatcher("**/*.{yml,yaml}");
1318
}
1419

1520
public async update(): Promise<void> {
@@ -22,6 +27,14 @@ export class TaskExtension {
2227
this._activityBar.refresh(this._taskfile);
2328
}
2429

30+
public async updateAndRefresh(): Promise<void> {
31+
await this.update().then(() => {
32+
this.refresh();
33+
}).catch((err: string) => {
34+
console.error(err);
35+
});
36+
}
37+
2538
public registerCommands(context: vscode.ExtensionContext): void {
2639

2740
// Run task
@@ -46,11 +59,30 @@ export class TaskExtension {
4659

4760
// Refresh tasks
4861
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.refresh', () => {
49-
this.update().then(() => {
50-
this.refresh();
51-
}).catch((err: string) => {
52-
console.error(err);
53-
});
62+
this.updateAndRefresh();
5463
}));
5564
}
65+
66+
public registerListeners(context: vscode.ExtensionContext): void {
67+
// When a file on the system is changed, created or deleted
68+
this._watcher.onDidChange(async _ => { await this._onDidTaskfileChange(); });
69+
this._watcher.onDidCreate(async _ => { await this._onDidTaskfileChange(); });
70+
this._watcher.onDidDelete(async _ => { await this._onDidTaskfileChange(); });
71+
72+
// Listen for configuration changes
73+
vscode.workspace.onDidChangeConfiguration(event => { this._onDidChangeConfiguration(event); });
74+
}
75+
76+
private async _onDidTaskfileChange() {
77+
// If manual updating is turned off (update on save)
78+
if (this._settings.updateOn !== "manual") {
79+
await this.updateAndRefresh();
80+
}
81+
}
82+
83+
private _onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
84+
if (event.affectsConfiguration("task")) {
85+
this._settings.update();
86+
}
87+
}
5688
}

0 commit comments

Comments
 (0)