Closed
Description
Refs: #66818
Complexity: 4
With the addition of the CustomExecution task type, extensions can now contribute tasks that are run as callbacks from VS Code, instead of tasks that run as a script or a process. Using the below code as a starting point, create an extension that uses the CustomExecution type to contribute tasks. Things to test:
- Contribute multiple tasks. Test that they all get executed correctly.
- Contribute multiple tasks, and make only some of them have a CustomExecution. The others can have a ShellExecution type. (example:
let execution = new vscode.ShellExecution("echo hello");
). - Test that you can use the TerminalRenderer from within the CustomExecution to send text to the terminal and to read from the terminal using
write
andonDidAcceptInput
. - Configure a task that has a CustomExecution in tasks.json. Try setting various presentation properties and other properties(dependsOn, label, group, problemMatcher, etc.) and test that they all behave correctly. Add other properties to
CustomTestingTaskDefinition
and in the package.json and test that the right task is modified when you configure it in tasks.json. How to refer to the task from tasks.json
{
"type": "customTesting",
"customProp1": "testing task one",
// try using dependsOn, presentation, and other task configuration properties
}
- Try returning different exit codes from the CustomExecution. 0 is success, anything else is not. Test that when the exit code is non-zero and
”presentation:{ “reveal”:”silent”}
that the terminal is shown when the task completes. - Test that the Rerun Last Task command works with CustomExecution tasks.
- Try setting a custom task as the
preLaunchTask
in a launch configuration and test that launch configuration still works. - Test that a
”type”:”shell”
task in tasks.json can depend on a contributed CustomExecution task.
To start with this sample code, copy vscode.proposed.d.ts into your extension and add to your package.json:
"enableProposedApi": true,
"contributes": {
"taskDefinitions": [
{
"type": "customTesting",
"required": [
"customProp1"
],
"properties": {
"customProp1": {
"type": "string",
"description": "The first custom task property"
}
}
}
]
},
Sample code:
let taskProvider: vscode.Disposable | undefined;
const taskType: string = "customTesting";
export function activate(_context: vscode.ExtensionContext): void {
let rakePromise: Thenable<vscode.Task[]> | undefined = undefined;
taskProvider = vscode.tasks.registerTaskProvider(taskType, {
provideTasks: () => {
rakePromise = getSomeTasks();
return rakePromise;
},
resolveTask(_task: vscode.Task): vscode.Task | undefined {
// Don't worry about testing anything with resolve task. It is old API that was never implemented.
return undefined;
}
});
}
export function deactivate(): void {
if (taskProvider) {
taskProvider.dispose();
}
}
interface CustomTestingTaskDefinition extends vscode.TaskDefinition {
/**
* One of the task properties. This can be used to customize the task in the tasks.json
*/
customProp1: string;
}
async function getSomeTasks(): Promise<vscode.Task[]> {
let result: vscode.Task[] = [];
let kind: CustomTestingTaskDefinition = {
type: taskType,
customProp1: "testing task one"
};
let execution = new vscode.CustomExecution((terminalRenderer, cancellationToken, args): Thenable<number> => {
return new Promise<number>(resolve => {
// This is the custom task callback!
resolve(0);
});
});
const taskName = "First custom task";
let task = new vscode.Task2(kind, vscode.TaskScope.Workspace, taskName, taskType, execution);
result.push(task);
return result;
}