2020-03-31 08:01:41 +00:00
|
|
|
import * as vscode from 'vscode';
|
2020-05-31 02:13:08 +00:00
|
|
|
import * as toolchain from "./toolchain";
|
2020-06-18 19:20:13 +00:00
|
|
|
import { Config } from './config';
|
|
|
|
import { log } from './util';
|
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.
|
2020-06-18 19:20:13 +00:00
|
|
|
export const TASK_TYPE = 'cargo';
|
|
|
|
export const TASK_SOURCE = 'rust';
|
2020-03-30 17:12:22 +00:00
|
|
|
|
2020-06-18 19:20:13 +00:00
|
|
|
export interface CargoTaskDefinition extends vscode.TaskDefinition {
|
2020-04-23 00:05:04 +00:00
|
|
|
command?: string;
|
|
|
|
args?: string[];
|
|
|
|
cwd?: string;
|
|
|
|
env?: { [key: string]: string };
|
2020-09-05 13:21:14 +00:00
|
|
|
overrideCargo?: string;
|
2020-04-23 00:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class CargoTaskProvider implements vscode.TaskProvider {
|
2020-06-18 19:20:13 +00:00
|
|
|
private readonly config: Config;
|
2020-04-23 00:05:04 +00:00
|
|
|
|
2021-05-25 22:11:52 +00:00
|
|
|
constructor(config: Config) {
|
2020-06-18 19:20:13 +00:00
|
|
|
this.config = config;
|
2020-04-23 00:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 09:42:26 +00:00
|
|
|
async provideTasks(): Promise<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-06-19 09:42:26 +00:00
|
|
|
const defs = [
|
2020-04-23 00:05:04 +00:00
|
|
|
{ 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 },
|
2020-06-19 09:42:26 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const tasks: vscode.Task[] = [];
|
2021-05-25 22:11:52 +00:00
|
|
|
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
|
|
|
|
for (const def of defs) {
|
|
|
|
const vscodeTask = await buildCargoTask(workspaceTarget, { type: TASK_TYPE, command: def.command }, `cargo ${def.command}`, [def.command], this.config.cargoRunner);
|
|
|
|
vscodeTask.group = def.group;
|
|
|
|
tasks.push(vscodeTask);
|
|
|
|
}
|
2020-06-19 09:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
2020-04-23 00:05:04 +00:00
|
|
|
}
|
2020-03-30 17:12:22 +00:00
|
|
|
|
2020-06-18 19:20:13 +00:00
|
|
|
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
|
2020-04-23 00:05:04 +00: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 17:12:22 +00:00
|
|
|
|
2020-04-23 00:05:04 +00:00
|
|
|
const definition = task.definition as CargoTaskDefinition;
|
|
|
|
|
2020-06-18 19:20:13 +00:00
|
|
|
if (definition.type === TASK_TYPE && definition.command) {
|
2020-04-23 00:05:04 +00:00
|
|
|
const args = [definition.command].concat(definition.args ?? []);
|
2021-05-25 22:11:52 +00:00
|
|
|
if (isWorkspaceFolder(task.scope)) {
|
|
|
|
return await buildCargoTask(task.scope, definition, task.name, args, this.config.cargoRunner);
|
|
|
|
}
|
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
|
|
|
|
2021-05-25 22:11:52 +00:00
|
|
|
function isWorkspaceFolder(scope?: any): scope is vscode.WorkspaceFolder {
|
|
|
|
return (scope as vscode.WorkspaceFolder).name !== undefined;
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:42:26 +00:00
|
|
|
export async function buildCargoTask(
|
|
|
|
target: vscode.WorkspaceFolder,
|
|
|
|
definition: CargoTaskDefinition,
|
|
|
|
name: string,
|
|
|
|
args: string[],
|
|
|
|
customRunner?: string,
|
|
|
|
throwOnError: boolean = false
|
|
|
|
): Promise<vscode.Task> {
|
|
|
|
|
2021-05-31 16:51:19 +00:00
|
|
|
let exec: vscode.ProcessExecution | vscode.ShellExecution | undefined = undefined;
|
2020-06-19 09:42:26 +00:00
|
|
|
|
2020-06-18 19:20:13 +00:00
|
|
|
if (customRunner) {
|
2020-06-19 09:42:26 +00:00
|
|
|
const runnerCommand = `${customRunner}.buildShellExecution`;
|
2020-06-18 19:20:13 +00:00
|
|
|
try {
|
2020-06-19 09:42:26 +00:00
|
|
|
const runnerArgs = { kind: TASK_TYPE, args, cwd: definition.cwd, env: definition.env };
|
|
|
|
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
|
|
|
if (customExec) {
|
|
|
|
if (customExec instanceof vscode.ShellExecution) {
|
2020-06-24 08:50:14 +00:00
|
|
|
exec = customExec;
|
2020-06-19 09:42:26 +00:00
|
|
|
} else {
|
|
|
|
log.debug("Invalid cargo ShellExecution", customExec);
|
|
|
|
throw "Invalid cargo ShellExecution.";
|
|
|
|
}
|
2020-06-18 19:20:13 +00:00
|
|
|
}
|
|
|
|
// fallback to default processing
|
|
|
|
|
|
|
|
} catch (e) {
|
2020-06-19 09:42:26 +00:00
|
|
|
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
|
|
|
|
// fallback to default processing
|
2020-06-18 19:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:42:26 +00:00
|
|
|
if (!exec) {
|
2020-09-05 13:21:14 +00:00
|
|
|
// Check whether we must use a user-defined substitute for cargo.
|
2021-05-31 16:51:19 +00:00
|
|
|
// Split on spaces to allow overrides like "wrapper cargo".
|
|
|
|
const overrideCargo = definition.overrideCargo ?? definition.overrideCargo;
|
2021-08-15 16:19:45 +00:00
|
|
|
const cargoPath = await toolchain.cargoPath();
|
|
|
|
const cargoCommand = overrideCargo?.split(" ") ?? [cargoPath];
|
2020-09-05 13:21:14 +00:00
|
|
|
|
2021-05-31 16:51:19 +00:00
|
|
|
const fullCommand = [...cargoCommand, ...args];
|
2020-09-05 13:21:14 +00:00
|
|
|
|
2021-05-31 16:51:19 +00:00
|
|
|
exec = new vscode.ProcessExecution(fullCommand[0], fullCommand.slice(1), definition);
|
2020-06-19 09:42:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 19:20:13 +00:00
|
|
|
return new vscode.Task(
|
|
|
|
definition,
|
2020-06-19 09:42:26 +00:00
|
|
|
target,
|
2020-06-18 19:20:13 +00:00
|
|
|
name,
|
|
|
|
TASK_SOURCE,
|
2020-06-19 09:42:26 +00:00
|
|
|
exec,
|
|
|
|
['$rustc']
|
2020-06-18 19:20:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:11:52 +00:00
|
|
|
export function activateTaskProvider(config: Config): vscode.Disposable {
|
|
|
|
const provider = new CargoTaskProvider(config);
|
2020-04-23 00:05:04 +00:00
|
|
|
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
2020-05-23 01:58:22 +00:00
|
|
|
}
|