9

I've built an extension for a programming language that I use and I've created hotkey shortcuts for calling the compiler executable with the currently open document's URI. I want to convert that to a build task in my extension. I have made a tasks.json file with a build task that works and catches errors and such, but it only works if I put it in the current workspace.

There are absolutely no examples of adding a build task anywhere and the API documentation for task providers is specifically for Ruby Rakefiles or something. I'm just wanting to make a shell executable build task with a problem matcher. Can anyone give me an example of that?

Gama11
  • 31,714
  • 9
  • 78
  • 100
sentry07
  • 111
  • 1
  • 5

1 Answers1

15

Here's a minimal TaskProvider implementation that simply runs echo "Hello World" in the shell:

'use strict';
import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    var type = "exampleProvider";
    vscode.tasks.registerTaskProvider(type, {
        provideTasks(token?: vscode.CancellationToken) {
            var execution = new vscode.ShellExecution("echo \"Hello World\"");
            var problemMatchers = ["$myProblemMatcher"];
            return [
                new vscode.Task({type: type}, vscode.TaskScope.Workspace,
                    "Build", "myExtension", execution, problemMatchers)
            ];
        },
        resolveTask(task: vscode.Task, token?: vscode.CancellationToken) {
            return task;
        }
    });
}

The task definition (first argument for new Task()) needs to be contributed via package.json and can have additional properties if needed:

"contributes": {
    "taskDefinitions": [
        {
            "type": "exampleProvider"
        }
    ]
}

Extensions with a task provider should activate when the Tasks: Run Task command is executed:

"activationEvents": [
    "onCommand:workbench.action.tasks.runTask"
]

And finally, the problem matcher(s) you want to reference need to be contributed in the package.json's contributes.problemMatchers section.

Gama11
  • 31,714
  • 9
  • 78
  • 100