mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Document CargoTaskDefinition and factor out converting TaskDefinition to Execution
This commit is contained in:
parent
e8d6a5ec0b
commit
a758e349bc
2 changed files with 63 additions and 49 deletions
|
@ -2,17 +2,26 @@ import * as vscode from "vscode";
|
|||
import * as toolchain from "./toolchain";
|
||||
import type { Config } from "./config";
|
||||
import { log } from "./util";
|
||||
import { unwrapUndefinable } from "./undefinable";
|
||||
|
||||
// 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.
|
||||
export const TASK_TYPE = "cargo";
|
||||
|
||||
export const TASK_SOURCE = "rust";
|
||||
|
||||
export interface CargoTaskDefinition extends vscode.TaskDefinition {
|
||||
program: string;
|
||||
args: string[];
|
||||
// The cargo command, such as "run" or "check".
|
||||
command: string;
|
||||
// Additional arguments passed to the cargo command.
|
||||
args?: string[];
|
||||
// The working directory to run the cargo command in.
|
||||
cwd?: string;
|
||||
// The shell environment.
|
||||
env?: { [key: string]: string };
|
||||
// Override the cargo executable name, such as
|
||||
// "my_custom_cargo_bin".
|
||||
overrideCargo?: string;
|
||||
}
|
||||
|
||||
class RustTaskProvider implements vscode.TaskProvider {
|
||||
|
@ -37,14 +46,12 @@ class RustTaskProvider implements vscode.TaskProvider {
|
|||
{ command: "run", group: undefined },
|
||||
];
|
||||
|
||||
const cargoPath = await toolchain.cargoPath();
|
||||
|
||||
const tasks: vscode.Task[] = [];
|
||||
for (const workspaceTarget of vscode.workspace.workspaceFolders || []) {
|
||||
for (const def of defs) {
|
||||
const vscodeTask = await buildRustTask(
|
||||
workspaceTarget,
|
||||
{ type: TASK_TYPE, program: cargoPath, args: [def.command] },
|
||||
{ type: TASK_TYPE, command: def.command },
|
||||
`cargo ${def.command}`,
|
||||
this.config.problemMatcher,
|
||||
this.config.cargoRunner,
|
||||
|
@ -86,36 +93,7 @@ export async function buildRustTask(
|
|||
customRunner?: string,
|
||||
throwOnError: boolean = false,
|
||||
): Promise<vscode.Task> {
|
||||
let exec: vscode.ProcessExecution | vscode.ShellExecution | undefined = undefined;
|
||||
|
||||
if (customRunner) {
|
||||
const runnerCommand = `${customRunner}.buildShellExecution`;
|
||||
try {
|
||||
const runnerArgs = {
|
||||
kind: TASK_TYPE,
|
||||
args: definition.args,
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
};
|
||||
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
||||
if (customExec) {
|
||||
if (customExec instanceof vscode.ShellExecution) {
|
||||
exec = customExec;
|
||||
} else {
|
||||
log.debug("Invalid cargo ShellExecution", customExec);
|
||||
throw "Invalid cargo ShellExecution.";
|
||||
}
|
||||
}
|
||||
// fallback to default processing
|
||||
} catch (e) {
|
||||
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
|
||||
// fallback to default processing
|
||||
}
|
||||
}
|
||||
|
||||
if (!exec) {
|
||||
exec = new vscode.ProcessExecution(definition.program, definition.args, definition);
|
||||
}
|
||||
const exec = await cargoToExecution(definition, customRunner, throwOnError);
|
||||
|
||||
return new vscode.Task(
|
||||
definition,
|
||||
|
@ -129,6 +107,53 @@ export async function buildRustTask(
|
|||
);
|
||||
}
|
||||
|
||||
async function cargoToExecution(
|
||||
definition: CargoTaskDefinition,
|
||||
customRunner: string | undefined,
|
||||
throwOnError: boolean,
|
||||
): Promise<vscode.ProcessExecution | vscode.ShellExecution> {
|
||||
if (customRunner) {
|
||||
const runnerCommand = `${customRunner}.buildShellExecution`;
|
||||
|
||||
try {
|
||||
const runnerArgs = {
|
||||
kind: TASK_TYPE,
|
||||
args: definition.args,
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
};
|
||||
const customExec = await vscode.commands.executeCommand(runnerCommand, runnerArgs);
|
||||
if (customExec) {
|
||||
if (customExec instanceof vscode.ShellExecution) {
|
||||
return customExec;
|
||||
} else {
|
||||
log.debug("Invalid cargo ShellExecution", customExec);
|
||||
throw "Invalid cargo ShellExecution.";
|
||||
}
|
||||
}
|
||||
// fallback to default processing
|
||||
} catch (e) {
|
||||
if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
|
||||
// fallback to default processing
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether we must use a user-defined substitute for cargo.
|
||||
// Split on spaces to allow overrides like "wrapper cargo".
|
||||
const cargoPath = await toolchain.cargoPath();
|
||||
const cargoCommand = definition.overrideCargo?.split(" ") ?? [cargoPath];
|
||||
|
||||
const args = [definition.command].concat(definition.args ?? []);
|
||||
const fullCommand = [...cargoCommand, ...args];
|
||||
|
||||
const processName = unwrapUndefinable(fullCommand[0]);
|
||||
|
||||
return new vscode.ProcessExecution(processName, fullCommand.slice(1), {
|
||||
cwd: definition.cwd,
|
||||
env: definition.env,
|
||||
});
|
||||
}
|
||||
|
||||
export function activateTaskProvider(config: Config): vscode.Disposable {
|
||||
const provider = new RustTaskProvider(config);
|
||||
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue