Switch to ShellExecution instead of full Task

This commit is contained in:
vsrs 2020-06-19 12:42:26 +03:00
parent a43a9103bc
commit 647b126da5
2 changed files with 46 additions and 42 deletions

View file

@ -116,7 +116,8 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }), env: Object.assign({}, process.env as { [key: string]: string }, { "RUST_BACKTRACE": "short" }),
}; };
const cargoTask = await tasks.buildCargoTask(definition, runnable.label, args, config.cargoRunner); const target = vscode.workspace.workspaceFolders![0]; // safe, see main activate()
const cargoTask = await tasks.buildCargoTask(target, definition, runnable.label, args, config.cargoRunner, true);
cargoTask.presentationOptions.clear = true; cargoTask.presentationOptions.clear = true;
return cargoTask; return cargoTask;

View file

@ -24,43 +24,28 @@ class CargoTaskProvider implements vscode.TaskProvider {
this.config = config; this.config = config;
} }
provideTasks(): vscode.Task[] { async provideTasks(): Promise<vscode.Task[]> {
// Detect Rust tasks. Currently we do not do any actual detection // Detect Rust tasks. Currently we do not do any actual detection
// of tasks (e.g. aliases in .cargo/config) and just return a fixed // 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 // set of tasks that always exist. These tasks cannot be removed in
// tasks.json - only tweaked. // tasks.json - only tweaked.
const cargoPath = toolchain.cargoPath(); const defs = [
return [
{ command: 'build', group: vscode.TaskGroup.Build }, { command: 'build', group: vscode.TaskGroup.Build },
{ command: 'check', group: vscode.TaskGroup.Build }, { command: 'check', group: vscode.TaskGroup.Build },
{ command: 'test', group: vscode.TaskGroup.Test }, { command: 'test', group: vscode.TaskGroup.Test },
{ command: 'clean', group: vscode.TaskGroup.Clean }, { command: 'clean', group: vscode.TaskGroup.Clean },
{ command: 'run', group: undefined }, { command: 'run', group: undefined },
] ];
.map(({ command, group }) => {
const vscodeTask = new vscode.Task( const tasks: vscode.Task[] = [];
// The contents of this object end up in the tasks.json entries. for (const def of defs) {
{ const vscodeTask = await buildCargoTask(this.target, { type: TASK_TYPE, command: def.command }, `cargo ${def.command}`, [def.command], this.config.cargoRunner);
type: TASK_TYPE, vscodeTask.group = def.group;
command, tasks.push(vscodeTask);
}, }
// The scope of the task - workspace or specific folder (global
// is not supported). return tasks;
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(cargoPath, [command]),
// Problem matchers.
['$rustc'],
);
vscodeTask.group = group;
return vscodeTask;
});
} }
async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> { async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> {
@ -73,38 +58,56 @@ class CargoTaskProvider implements vscode.TaskProvider {
if (definition.type === TASK_TYPE && definition.command) { if (definition.type === TASK_TYPE && definition.command) {
const args = [definition.command].concat(definition.args ?? []); const args = [definition.command].concat(definition.args ?? []);
return await buildCargoTask(definition, task.name, args, this.config.cargoRunner); return await buildCargoTask(this.target, definition, task.name, args, this.config.cargoRunner);
} }
return undefined; return undefined;
} }
} }
export async function buildCargoTask(definition: CargoTaskDefinition, name: string, args: string[], customRunner?: string): Promise<vscode.Task> { export async function buildCargoTask(
if (customRunner) { target: vscode.WorkspaceFolder,
const runnerCommand = `${customRunner}.createCargoTask`; definition: CargoTaskDefinition,
try { name: string,
const runnerArgs = { name, args, cwd: definition.cwd, env: definition.env, source: TASK_SOURCE }; args: string[],
const task = await vscode.commands.executeCommand(runnerCommand, runnerArgs); customRunner?: string,
throwOnError: boolean = false
): Promise<vscode.Task> {
if (task instanceof vscode.Task) { let exec: vscode.ShellExecution | undefined = undefined;
return task;
} else if (task) { if (customRunner) {
log.debug("Invalid cargo task", task); const runnerCommand = `${customRunner}.buildShellExecution`;
throw `Invalid task!`; try {
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) {
exec = customExec as vscode.ShellExecution;
} else {
log.debug("Invalid cargo ShellExecution", customExec);
throw "Invalid cargo ShellExecution.";
}
} }
// fallback to default processing // fallback to default processing
} catch (e) { } catch (e) {
throw `Cargo runner '${customRunner}' failed! ${e}`; if (throwOnError) throw `Cargo runner '${customRunner}' failed! ${e}`;
// fallback to default processing
} }
} }
if (!exec) {
exec = new vscode.ShellExecution(toolchain.cargoPath(), args, definition)
}
return new vscode.Task( return new vscode.Task(
definition, definition,
target,
name, name,
TASK_SOURCE, TASK_SOURCE,
new vscode.ShellExecution(toolchain.cargoPath(), args, definition), exec,
['$rustc']
); );
} }