2020-03-31 08:01:41 +00:00
|
|
|
import * as vscode from 'vscode';
|
2020-03-30 17:12:22 +00:00
|
|
|
|
|
|
|
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
|
|
|
|
// our configuration should be compatible with it so use the same key.
|
|
|
|
const TASK_TYPE = 'cargo';
|
|
|
|
|
2020-04-23 00:05:04 +00:00
|
|
|
interface CargoTaskDefinition extends vscode.TaskDefinition {
|
|
|
|
command?: string;
|
|
|
|
args?: string[];
|
|
|
|
cwd?: string;
|
|
|
|
env?: { [key: string]: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
class CargoTaskProvider implements vscode.TaskProvider {
|
|
|
|
private readonly target: vscode.WorkspaceFolder;
|
|
|
|
|
|
|
|
constructor(target: vscode.WorkspaceFolder) {
|
|
|
|
this.target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
provideTasks(): vscode.Task[] {
|
2020-03-30 17:12:22 +00:00
|
|
|
// Detect Rust tasks. Currently we do not do any actual detection
|
|
|
|
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
|
|
|
|
// set of tasks that always exist. These tasks cannot be removed in
|
|
|
|
// tasks.json - only tweaked.
|
|
|
|
|
2020-04-23 00:05:04 +00:00
|
|
|
return [
|
|
|
|
{ command: 'build', group: vscode.TaskGroup.Build },
|
|
|
|
{ command: 'check', group: vscode.TaskGroup.Build },
|
|
|
|
{ command: 'test', group: vscode.TaskGroup.Test },
|
|
|
|
{ command: 'clean', group: vscode.TaskGroup.Clean },
|
|
|
|
{ command: 'run', group: undefined },
|
|
|
|
]
|
|
|
|
.map(({ command, group }) => {
|
|
|
|
const vscodeTask = new vscode.Task(
|
|
|
|
// The contents of this object end up in the tasks.json entries.
|
|
|
|
{
|
|
|
|
type: TASK_TYPE,
|
|
|
|
command,
|
|
|
|
},
|
|
|
|
// The scope of the task - workspace or specific folder (global
|
|
|
|
// is not supported).
|
|
|
|
this.target,
|
|
|
|
// The task name, and task source. These are shown in the UI as
|
|
|
|
// `${source}: ${name}`, e.g. `rust: cargo build`.
|
|
|
|
`cargo ${command}`,
|
|
|
|
'rust',
|
|
|
|
// What to do when this command is executed.
|
|
|
|
new vscode.ShellExecution('cargo', [command]),
|
|
|
|
// Problem matchers.
|
|
|
|
['$rustc'],
|
|
|
|
);
|
|
|
|
vscodeTask.group = group;
|
|
|
|
return vscodeTask;
|
|
|
|
});
|
|
|
|
}
|
2020-03-30 17:12:22 +00:00
|
|
|
|
2020-04-23 00:05:04 +00:00
|
|
|
resolveTask(task: vscode.Task): vscode.Task | undefined {
|
|
|
|
// VSCode calls this for every cargo task in the user's tasks.json,
|
|
|
|
// we need to inform VSCode how to execute that command by creating
|
|
|
|
// a ShellExecution for it.
|
2020-03-30 17:12:22 +00:00
|
|
|
|
2020-04-23 00:05:04 +00:00
|
|
|
const definition = task.definition as CargoTaskDefinition;
|
|
|
|
|
|
|
|
if (definition.type === 'cargo' && definition.command) {
|
|
|
|
const args = [definition.command].concat(definition.args ?? []);
|
|
|
|
|
|
|
|
return new vscode.Task(
|
|
|
|
definition,
|
|
|
|
task.name,
|
2020-03-30 17:12:22 +00:00
|
|
|
'rust',
|
2020-04-23 00:05:04 +00:00
|
|
|
new vscode.ShellExecution('cargo', args, definition),
|
2020-03-30 17:12:22 +00:00
|
|
|
);
|
2020-04-23 00:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-03-30 17:12:22 +00:00
|
|
|
}
|
2020-04-23 00:05:04 +00:00
|
|
|
|
|
|
|
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
|
|
|
|
const provider = new CargoTaskProvider(target);
|
|
|
|
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
|
|
|
}
|