diff --git a/.vscode/tasks.json b/.vscode/tasks.json index de4ed11..e2e2358 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -17,4 +17,4 @@ } } ] -} +} \ No newline at end of file diff --git a/package.json b/package.json index e6188e6..699b672 100644 --- a/package.json +++ b/package.json @@ -368,7 +368,25 @@ } } } - } + }, + "taskDefinitions": [ + { + "type": "go-task", + "required": [ + "name" + ], + "properties": { + "task": { + "type": "string", + "description": "The Taskfile task to customize" + }, + "location": { + "type": "string", + "description": "The Taskfile file that provides the task. Can be omitted." + } + } + } + ] }, "scripts": { "preinstall": "npx only-allow pnpm", diff --git a/src/extension.ts b/src/extension.ts index c89ce33..4bce9c3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,14 +4,14 @@ import { TaskExtension } from './task.js'; export function activate(context: vscode.ExtensionContext) { log.info("Extension activated"); - + // Create a new instance of Tagger let taskExtension: TaskExtension = new TaskExtension(); // Registration taskExtension.registerCommands(context); taskExtension.registerListeners(context); - + taskExtension.registerTaskProvider(context); // Refresh the tasks list taskExtension.refresh(); } diff --git a/src/services/taskfile.ts b/src/services/taskfile.ts index cc2faf1..b6f116b 100644 --- a/src/services/taskfile.ts +++ b/src/services/taskfile.ts @@ -51,7 +51,7 @@ class TaskfileService { return this._instance ?? (this._instance = new this()); } - private command(command?: string, cliArgs?: string): string { + public command(command?: string, cliArgs?: string): string { if (command === undefined) { return settings.path; } diff --git a/src/task.ts b/src/task.ts index 5a7fb51..487f9ad 100644 --- a/src/task.ts +++ b/src/task.ts @@ -7,7 +7,28 @@ import { taskfileSvc } from './services/taskfile.js'; import { log } from './utils/log.js'; import { configKey, oldConfigKey, settings, UpdateOn } from './utils/settings.js'; -export class TaskExtension { +function resolveTaskForNamespace(ns: Namespace) : vscode.Task[] { + const result: vscode.Task[] = []; + if (ns.tasks.length > 0) { + ns.tasks.forEach(task => { + const definition = {"type":TaskExtension.TaskExtensionTaskType, "name":task.name, "location":task.location} + const execution = new vscode.ShellExecution(`${taskfileSvc.command()} ${task.name}`) + const vTask = new vscode.Task(definition, vscode.TaskScope.Workspace, task.name, TaskExtension.TaskExtensionTaskType, execution); + result.push(vTask); + }); + } + if (ns.namespaces === undefined) { + return result + } + const nss = new Map(Object.entries(ns.namespaces)) + nss.forEach((obj) => { + result.push(...resolveTaskForNamespace(obj)) + }); + return result +} + +export class TaskExtension implements vscode.TaskProvider { + public static TaskExtensionTaskType = 'go-task'; private _taskfiles: Namespace[] = []; private _activityBar: ActivityBar; private _watcher: vscode.FileSystemWatcher; @@ -22,6 +43,17 @@ export class TaskExtension { this._status = settings.tree.status; vscode.commands.executeCommand('setContext', 'vscode-task:treeNesting', this._nesting); } + + + public async provideTasks(token: vscode.CancellationToken): Promise { + await this.refresh(); + return this.getVscodeTask(); + } + public resolveTask(_task: vscode.Task, token: vscode.CancellationToken): vscode.ProviderResult { + const definition = {"type":TaskExtension.TaskExtensionTaskType, "name":_task.name} + const execution = new vscode.ShellExecution(`${taskfileSvc.command()} ${definition.name}`) + return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.name, 'go-task', execution); + } public async update(checkForUpdates?: boolean): Promise { // Do version checks @@ -257,6 +289,17 @@ export class TaskExtension { vscode.workspace.onDidChangeConfiguration(event => { this._onDidChangeConfiguration(event); }); } + public registerTaskProvider(context: vscode.ExtensionContext): void { + vscode.tasks.registerTaskProvider(TaskExtension.TaskExtensionTaskType, this); + } + private async getVscodeTask(): Promise { + const result: vscode.Task[] = []; + this._taskfiles.forEach(taskfile => { + result.push(...resolveTaskForNamespace(taskfile)) + }); + return result; + } + private _loadTasksFromTaskfile() { let items: vscode.QuickPickItem[] = []; this._taskfiles.forEach(taskfile => { @@ -264,6 +307,7 @@ export class TaskExtension { items = items.concat(new QuickPickTaskSeparator(taskfile)); taskfile.tasks.forEach(task => { items = items.concat(new QuickPickTaskItem(taskfile, task)); + }); } });