From 647b126da52b3dec1268df13b08c8a14744feb06 Mon Sep 17 00:00:00 2001 From: vsrs Date: Fri, 19 Jun 2020 12:42:26 +0300 Subject: Switch to ShellExecution instead of full Task --- editors/code/src/tasks.ts | 83 ++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 40 deletions(-) (limited to 'editors/code/src/tasks.ts') diff --git a/editors/code/src/tasks.ts b/editors/code/src/tasks.ts index e2c43fdd4..20ddb3a5d 100644 --- a/editors/code/src/tasks.ts +++ b/editors/code/src/tasks.ts @@ -24,43 +24,28 @@ class CargoTaskProvider implements vscode.TaskProvider { this.config = config; } - provideTasks(): vscode.Task[] { + async provideTasks(): Promise { // 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. - const cargoPath = toolchain.cargoPath(); - - return [ + const defs = [ { 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(cargoPath, [command]), - // Problem matchers. - ['$rustc'], - ); - vscodeTask.group = group; - return vscodeTask; - }); + ]; + + const tasks: vscode.Task[] = []; + 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); + vscodeTask.group = def.group; + tasks.push(vscodeTask); + } + + return tasks; } async resolveTask(task: vscode.Task): Promise { @@ -73,38 +58,56 @@ class CargoTaskProvider implements vscode.TaskProvider { if (definition.type === TASK_TYPE && definition.command) { 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; } } -export async function buildCargoTask(definition: CargoTaskDefinition, name: string, args: string[], customRunner?: string): Promise { +export async function buildCargoTask( + target: vscode.WorkspaceFolder, + definition: CargoTaskDefinition, + name: string, + args: string[], + customRunner?: string, + throwOnError: boolean = false +): Promise { + + let exec: vscode.ShellExecution | undefined = undefined; + if (customRunner) { - const runnerCommand = `${customRunner}.createCargoTask`; + const runnerCommand = `${customRunner}.buildShellExecution`; try { - const runnerArgs = { name, args, cwd: definition.cwd, env: definition.env, source: TASK_SOURCE }; - const task = await vscode.commands.executeCommand(runnerCommand, runnerArgs); - - if (task instanceof vscode.Task) { - return task; - } else if (task) { - log.debug("Invalid cargo task", task); - throw `Invalid task!`; + 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 } 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( definition, + target, name, TASK_SOURCE, - new vscode.ShellExecution(toolchain.cargoPath(), args, definition), + exec, + ['$rustc'] ); } -- cgit v1.2.3