Han
Han

Reputation: 167

Add tasks to tasks.json for users of a vscode extension

I am writing a vscode extention for a specification language. I would like to provide the users of the plugin with a specific task. The task could be made available using tasks.json. Is there a way to add tasks to the tasks.json of a user that uses that extension?

Upvotes: 6

Views: 1764

Answers (1)

CodeMonkey
CodeMonkey

Reputation: 4738

The documentation didn't help me here either. When you provide tasks through the extension there is the TaskProvider API. The example doesn't go into much detail about how these tasks are created though, compared to the classical tasks.json approach.

In your package.json you need to define the types of tasks this extension contributes. This has nothing to do with the type in tasks.json. It's rather a freeform string. If you need custom problem matchers you also need to define theme here.

"contributes": {
    "taskDefinitions": [
        {
            "type": "mytask"
        }
    ],
    "problemMatchers": [
        {
            "name": "mywarnings",
            "base": "$gcc",
            "fileLocation": [
                "relative",
                "/"
            ]
        }
    ]
},

In extension.ts you need to provide the tasks. Say we have an array of vscode.Task in tasks you can do:

vscode.tasks.registerTaskProvider('mytask', {
    provideTasks: () => {
        return tasks;
    },
    resolveTask(_task: vscode.Task): vscode.Task | undefined {
        // as far as I can see from the documentation this just needs to return undefined.
        return undefined;
    }
});

If you want to create a shell task you need the following:

new vscode.Task (
    {type: 'shell'}, // this is the same type as in tasks.json
    ws, // The workspace folder
    'name', // how you name the task
    'MyTask', // Shows up as MyTask: name 
    new vscode.ShellExecution(command),
    ["mywarnings"] // list of problem matchers (can use $gcc or other pre-built matchers, or the ones defined in package.json)
);

I hope this helps. The bigges issue I see is the overloading of various names (like type), and that the format in tasks.json is completely different to the way tasks are built with TaskProvider API.

Upvotes: 8

Related Questions