@@ -24,43 +24,28 @@ class CargoTaskProvider implements vscode.TaskProvider {
24
24
this . config = config ;
25
25
}
26
26
27
- provideTasks ( ) : vscode . Task [ ] {
27
+ async provideTasks ( ) : Promise < vscode . Task [ ] > {
28
28
// Detect Rust tasks. Currently we do not do any actual detection
29
29
// of tasks (e.g. aliases in .cargo/config) and just return a fixed
30
30
// set of tasks that always exist. These tasks cannot be removed in
31
31
// tasks.json - only tweaked.
32
32
33
- const cargoPath = toolchain . cargoPath ( ) ;
34
-
35
- return [
33
+ const defs = [
36
34
{ command : 'build' , group : vscode . TaskGroup . Build } ,
37
35
{ command : 'check' , group : vscode . TaskGroup . Build } ,
38
36
{ command : 'test' , group : vscode . TaskGroup . Test } ,
39
37
{ command : 'clean' , group : vscode . TaskGroup . Clean } ,
40
38
{ command : 'run' , group : undefined } ,
41
- ]
42
- . map ( ( { command, group } ) => {
43
- const vscodeTask = new vscode . Task (
44
- // The contents of this object end up in the tasks.json entries.
45
- {
46
- type : TASK_TYPE ,
47
- command,
48
- } ,
49
- // The scope of the task - workspace or specific folder (global
50
- // is not supported).
51
- this . target ,
52
- // The task name, and task source. These are shown in the UI as
53
- // `${source}: ${name}`, e.g. `rust: cargo build`.
54
- `cargo ${ command } ` ,
55
- 'rust' ,
56
- // What to do when this command is executed.
57
- new vscode . ShellExecution ( cargoPath , [ command ] ) ,
58
- // Problem matchers.
59
- [ '$rustc' ] ,
60
- ) ;
61
- vscodeTask . group = group ;
62
- return vscodeTask ;
63
- } ) ;
39
+ ] ;
40
+
41
+ const tasks : vscode . Task [ ] = [ ] ;
42
+ for ( const def of defs ) {
43
+ const vscodeTask = await buildCargoTask ( this . target , { type : TASK_TYPE , command : def . command } , `cargo ${ def . command } ` , [ def . command ] , this . config . cargoRunner ) ;
44
+ vscodeTask . group = def . group ;
45
+ tasks . push ( vscodeTask ) ;
46
+ }
47
+
48
+ return tasks ;
64
49
}
65
50
66
51
async resolveTask ( task : vscode . Task ) : Promise < vscode . Task | undefined > {
@@ -73,38 +58,56 @@ class CargoTaskProvider implements vscode.TaskProvider {
73
58
if ( definition . type === TASK_TYPE && definition . command ) {
74
59
const args = [ definition . command ] . concat ( definition . args ?? [ ] ) ;
75
60
76
- return await buildCargoTask ( definition , task . name , args , this . config . cargoRunner ) ;
61
+ return await buildCargoTask ( this . target , definition , task . name , args , this . config . cargoRunner ) ;
77
62
}
78
63
79
64
return undefined ;
80
65
}
81
66
}
82
67
83
- export async function buildCargoTask ( definition : CargoTaskDefinition , name : string , args : string [ ] , customRunner ?: string ) : Promise < vscode . Task > {
68
+ export async function buildCargoTask (
69
+ target : vscode . WorkspaceFolder ,
70
+ definition : CargoTaskDefinition ,
71
+ name : string ,
72
+ args : string [ ] ,
73
+ customRunner ?: string ,
74
+ throwOnError : boolean = false
75
+ ) : Promise < vscode . Task > {
76
+
77
+ let exec : vscode . ShellExecution | undefined = undefined ;
78
+
84
79
if ( customRunner ) {
85
- const runnerCommand = `${ customRunner } .createCargoTask ` ;
80
+ const runnerCommand = `${ customRunner } .buildShellExecution ` ;
86
81
try {
87
- const runnerArgs = { name, args, cwd : definition . cwd , env : definition . env , source : TASK_SOURCE } ;
88
- const task = await vscode . commands . executeCommand ( runnerCommand , runnerArgs ) ;
89
-
90
- if ( task instanceof vscode . Task ) {
91
- return task ;
92
- } else if ( task ) {
93
- log . debug ( "Invalid cargo task" , task ) ;
94
- throw `Invalid task!` ;
82
+ const runnerArgs = { kind : TASK_TYPE , args, cwd : definition . cwd , env : definition . env } ;
83
+ const customExec = await vscode . commands . executeCommand ( runnerCommand , runnerArgs ) ;
84
+ if ( customExec ) {
85
+ if ( customExec instanceof vscode . ShellExecution ) {
86
+ exec = customExec as vscode . ShellExecution ;
87
+ } else {
88
+ log . debug ( "Invalid cargo ShellExecution" , customExec ) ;
89
+ throw "Invalid cargo ShellExecution." ;
90
+ }
95
91
}
96
92
// fallback to default processing
97
93
98
94
} catch ( e ) {
99
- throw `Cargo runner '${ customRunner } ' failed! ${ e } ` ;
95
+ if ( throwOnError ) throw `Cargo runner '${ customRunner } ' failed! ${ e } ` ;
96
+ // fallback to default processing
100
97
}
101
98
}
102
99
100
+ if ( ! exec ) {
101
+ exec = new vscode . ShellExecution ( toolchain . cargoPath ( ) , args , definition )
102
+ }
103
+
103
104
return new vscode . Task (
104
105
definition ,
106
+ target ,
105
107
name ,
106
108
TASK_SOURCE ,
107
- new vscode . ShellExecution ( toolchain . cargoPath ( ) , args , definition ) ,
109
+ exec ,
110
+ [ '$rustc' ]
108
111
) ;
109
112
}
110
113
0 commit comments