2020-03-31 09:01:41 +01:00
|
|
|
import * as vscode from "vscode";
|
2020-05-31 05:13:08 +03:00
|
|
|
import * as toolchain from "./toolchain";
|
2023-07-11 06:10:00 +09:00
|
|
|
import type { Config } from "./config";
|
2020-06-18 22:20:13 +03:00
|
|
|
import { log } from "./util";
|
2020-03-30 18:12:22 +01: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.
|
2020-06-18 22:20:13 +03:00
|
|
|
export const TASK_TYPE = "cargo";
|
|
|
|
export const TASK_SOURCE = "rust";
|
2020-03-30 18:12:22 +01:00
|
|
|
|
2024-03-13 15:47:34 -07:00
|
|
|
export interface RustTargetDefinition extends vscode.TaskDefinition {
|
2024-03-13 17:13:26 -07:00
|
|
|
program: string;
|
2024-03-13 16:46:57 -07:00
|
|
|
args: string[];
|
2020-04-22 17:05:04 -07:00
|
|
|
cwd?: string;
|
|
|
|
env?: { [key: string]: string };
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:47:34 -07:00
|
|
|
class RustTaskProvider implements vscode.TaskProvider {
|
2020-06-18 22:20:13 +03:00
|
|
|
private readonly config: Config;
|
2020-04-22 17:05:04 -07:00
|
|
|
|
2021-05-26 01:11:52 +03:00
|
|
|
constructor(config: Config) {
|
2020-06-18 22:20:13 +03:00
|
|
|
this.config = config;
|
2020-04-22 17:05:04 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 12:42:26 +03:00
|
|
|
async provideTasks(): Promise<vscode.Task[]> {
|
2020-03-30 18:12:22 +01: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-06-19 12:42:26 +03:00
|
|
|
const defs = [
|
2020-04-22 17:05:04 -07:00
|
|
|
{ command: "build", group: vscode.TaskGroup.Build },
|
|
|
|
{ command: "check", group: vscode.TaskGroup.Build },
|
2022-05-20 15:25:37 +02:00
|
|
|
{ command: "clippy", group: vscode.TaskGroup.Build },
|
2020-04-22 17:05:04 -07:00
|
|
|
{ command: "test", group: vscode.TaskGroup.Test },
|
|
|
|
{ command: "clean", group: vscode.TaskGroup.Clean },
|
|
|
|
{ command: "run", group: undefined },
|
2020-06-19 12:42:26 +03:00
|
|
|
];
|
|
|
|
|
2024-03-13 17:13:26 -07:00
|
|
|
const cargoPath = await toolchain.cargoPath();
|
|
|
|
|
2020-06-19 12:42:26 +03:00
|
|
|
const tasks: vscode.Task[] = [];
|
2021-05-26 01:11:52 +03:00
|
|
|
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
|
|
|
|
for (const def of defs) {
|
2024-03-13 15:47:34 -07:00
|
|
|
const vscodeTask = await buildRustTask(
|
2021-05-26 01:11:52 +03:00
|
|
|
workspaceTarget,
|
2024-03-13 17:13:26 -07:00
|
|
|
{ type: TASK_TYPE, program: cargoPath, args: [def.command] },
|
2021-05-26 01:11:52 +03:00
|
|
|
`cargo ${def.command}`,
|
2023-06-30 22:18:52 +01:00
|
|
|
this.config.problemMatcher,
|
2023-07-11 22:35:10 +09:00
|
|
|
this.config.cargoRunner,
|
2021-05-26 01:11:52 +03:00
|
|
|
);
|
|
|
|
vscodeTask.group = def.group;
|
|
|
|
tasks.push(vscodeTask);
|
|
|
|
}
|
2020-06-19 12:42:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
2020-04-22 17:05:04 -07:00
|
|
|
}
|
2020-03-30 18:12:22 +01:00
|
|
|
|
2020-06-18 22:20:13 +03:00
|
|
|
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
|
2020-04-22 17:05:04 -07:00
|
|
|
// 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 18:12:22 +01:00
|
|
|
|
2024-03-13 15:47:34 -07:00
|
|
|
const definition = task.definition as RustTargetDefinition;
|
2020-04-22 17:05:04 -07:00
|
|
|
|
2024-03-13 16:46:57 -07:00
|
|
|
if (definition.type === TASK_TYPE) {
|
2024-03-13 15:47:34 -07:00
|
|
|
return await buildRustTask(
|
2021-08-31 20:35:22 +12:00
|
|
|
task.scope,
|
|
|
|
definition,
|
|
|
|
task.name,
|
2023-06-30 22:18:52 +01:00
|
|
|
this.config.problemMatcher,
|
2023-07-11 22:35:10 +09:00
|
|
|
this.config.cargoRunner,
|
2021-08-31 20:35:22 +12:00
|
|
|
);
|
2020-04-22 17:05:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-03-30 18:12:22 +01:00
|
|
|
}
|
2020-04-22 17:05:04 -07:00
|
|
|
|
2024-03-13 15:47:34 -07:00
|
|
|
export async function buildRustTask(
|
2021-08-31 20:35:22 +12:00
|
|
|
scope: vscode.WorkspaceFolder | vscode.TaskScope | undefined,
|
2024-03-13 15:47:34 -07:00
|
|
|
definition: RustTargetDefinition,
|
2020-06-19 12:42:26 +03:00
|
|
|
name: string,
|
2023-06-30 22:18:52 +01:00
|
|
|
problemMatcher: string[],
|
2020-06-19 12:42:26 +03:00
|
|
|
customRunner?: string,
|
2023-07-11 22:35:10 +09:00
|
|
|
throwOnError: boolean = false,
|
2020-06-19 12:42:26 +03:00
|
|
|
): Promise<vscode.Task> {
|
2021-05-31 19:51:19 +03:00
|
|
|
let exec: vscode.ProcessExecution | vscode.ShellExecution | undefined = undefined;
|
2020-06-19 12:42:26 +03:00
|
|
|
|
2020-06-18 22:20:13 +03:00
|
|
|
if (customRunner) {
|
2020-06-19 12:42:26 +03:00
|
|
|
const runnerCommand = `${customRunner}.buildShellExecution`;
|
2020-06-18 22:20:13 +03:00
|
|
|
try {
|
2024-03-13 16:46:57 -07:00
|
|
|
const runnerArgs = {
|
|
|
|
kind: TASK_TYPE,
|
|
|
|
args: definition.args,
|
|
|
|
cwd: definition.cwd,
|
|
|
|
env: definition.env,
|
|
|
|
};
|
2020-06-19 12:42:26 +03:00
|
|
|
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
|
|
|
if (customExec) {
|
|
|
|
if (customExec instanceof vscode.ShellExecution) {
|
2020-06-24 11:50:14 +03:00
|
|
|
exec = customExec;
|
2020-06-19 12:42:26 +03:00
|
|
|
} else {
|
|
|
|
log.debug("Invalid cargo ShellExecution", customExec);
|
|
|
|
throw "Invalid cargo ShellExecution.";
|
|
|
|
}
|
2020-06-18 22:20:13 +03:00
|
|
|
}
|
|
|
|
// fallback to default processing
|
|
|
|
} catch (e) {
|
2020-06-19 12:42:26 +03:00
|
|
|
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
|
|
|
|
// fallback to default processing
|
2020-06-18 22:20:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:42:26 +03:00
|
|
|
if (!exec) {
|
2024-03-13 17:13:26 -07:00
|
|
|
exec = new vscode.ProcessExecution(definition.program, definition.args, definition);
|
2020-06-19 12:42:26 +03:00
|
|
|
}
|
|
|
|
|
2021-09-08 10:46:31 +12:00
|
|
|
return new vscode.Task(
|
|
|
|
definition,
|
2021-09-09 08:50:24 +12:00
|
|
|
// scope can sometimes be undefined. in these situations we default to the workspace taskscope as
|
|
|
|
// recommended by the official docs: https://code.visualstudio.com/api/extension-guides/task-provider#task-provider)
|
2021-09-08 10:46:31 +12:00
|
|
|
scope ?? vscode.TaskScope.Workspace,
|
|
|
|
name,
|
|
|
|
TASK_SOURCE,
|
|
|
|
exec,
|
2023-07-11 22:35:10 +09:00
|
|
|
problemMatcher,
|
2021-09-08 10:46:31 +12:00
|
|
|
);
|
2020-06-18 22:20:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:11:52 +03:00
|
|
|
export function activateTaskProvider(config: Config): vscode.Disposable {
|
2024-03-13 15:47:34 -07:00
|
|
|
const provider = new RustTaskProvider(config);
|
2020-04-22 17:05:04 -07:00
|
|
|
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
2020-05-23 04:58:22 +03:00
|
|
|
}
|