@@ -70,12 +70,27 @@ interface GruntTaskDefinition extends vscode.TaskDefinition {
7070 file ?: string ;
7171}
7272
73+ async function findGruntCommand ( rootPath : string ) : Promise < string > {
74+ let command : string ;
75+ let platform = process . platform ;
76+ if ( platform === 'win32' && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'grunt.cmd' ) ) ) {
77+ command = path . join ( '.' , 'node_modules' , '.bin' , 'grunt.cmd' ) ;
78+ } else if ( ( platform === 'linux' || platform === 'darwin' ) && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'grunt' ) ) ) {
79+ command = path . join ( '.' , 'node_modules' , '.bin' , 'grunt' ) ;
80+ } else {
81+ command = 'grunt' ;
82+ }
83+ return command ;
84+ }
85+
7386class FolderDetector {
7487
7588 private fileWatcher : vscode . FileSystemWatcher | undefined ;
7689 private promise : Thenable < vscode . Task [ ] > | undefined ;
7790
78- constructor ( private _workspaceFolder : vscode . WorkspaceFolder ) {
91+ constructor (
92+ private _workspaceFolder : vscode . WorkspaceFolder ,
93+ private _gruntCommand : Promise < string > ) {
7994 }
8095
8196 public get workspaceFolder ( ) : vscode . WorkspaceFolder {
@@ -95,10 +110,28 @@ class FolderDetector {
95110 }
96111
97112 public async getTasks ( ) : Promise < vscode . Task [ ] > {
98- if ( ! this . promise ) {
99- this . promise = this . computeTasks ( ) ;
113+ if ( this . isEnabled ( ) ) {
114+ if ( ! this . promise ) {
115+ this . promise = this . computeTasks ( ) ;
116+ }
117+ return this . promise ;
118+ } else {
119+ return [ ] ;
100120 }
101- return this . promise ;
121+ }
122+
123+ public async getTask ( _task : vscode . Task ) : Promise < vscode . Task | undefined > {
124+ const gruntTask = ( < any > _task . definition ) . task ;
125+ if ( gruntTask ) {
126+ let kind : GruntTaskDefinition = ( < any > _task . definition ) ;
127+ let options : vscode . ShellExecutionOptions = { cwd : this . workspaceFolder . uri . fsPath } ;
128+ let source = 'grunt' ;
129+ let task = gruntTask . indexOf ( ' ' ) === - 1
130+ ? new vscode . Task ( kind , this . workspaceFolder , gruntTask , source , new vscode . ShellExecution ( `${ await this . _gruntCommand } ${ name } ` , options ) )
131+ : new vscode . Task ( kind , this . workspaceFolder , gruntTask , source , new vscode . ShellExecution ( `${ await this . _gruntCommand } "${ name } "` , options ) ) ;
132+ return task ;
133+ }
134+ return undefined ;
102135 }
103136
104137 private async computeTasks ( ) : Promise < vscode . Task [ ] > {
@@ -111,17 +144,7 @@ class FolderDetector {
111144 return emptyTasks ;
112145 }
113146
114- let command : string ;
115- let platform = process . platform ;
116- if ( platform === 'win32' && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'grunt.cmd' ) ) ) {
117- command = path . join ( '.' , 'node_modules' , '.bin' , 'grunt.cmd' ) ;
118- } else if ( ( platform === 'linux' || platform === 'darwin' ) && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'grunt' ) ) ) {
119- command = path . join ( '.' , 'node_modules' , '.bin' , 'grunt' ) ;
120- } else {
121- command = 'grunt' ;
122- }
123-
124- let commandLine = `${ command } --help --no-color` ;
147+ let commandLine = `${ await this . _gruntCommand } --help --no-color` ;
125148 try {
126149 let { stdout, stderr } = await exec ( commandLine , { cwd : rootPath } ) ;
127150 if ( stderr ) {
@@ -168,8 +191,8 @@ class FolderDetector {
168191 let source = 'grunt' ;
169192 let options : vscode . ShellExecutionOptions = { cwd : this . workspaceFolder . uri . fsPath } ;
170193 let task = name . indexOf ( ' ' ) === - 1
171- ? new vscode . Task ( kind , this . workspaceFolder , name , source , new vscode . ShellExecution ( `${ command } ${ name } ` , options ) )
172- : new vscode . Task ( kind , this . workspaceFolder , name , source , new vscode . ShellExecution ( `${ command } "${ name } "` , options ) ) ;
194+ ? new vscode . Task ( kind , this . workspaceFolder , name , source , new vscode . ShellExecution ( `${ await this . _gruntCommand } ${ name } ` , options ) )
195+ : new vscode . Task ( kind , this . workspaceFolder , name , source , new vscode . ShellExecution ( `${ await this . _gruntCommand } "${ name } "` , options ) ) ;
173196 result . push ( task ) ;
174197 let lowerCaseTaskName = name . toLowerCase ( ) ;
175198 if ( isBuildTask ( lowerCaseTaskName ) ) {
@@ -239,9 +262,9 @@ class TaskDetector {
239262 }
240263 }
241264 for ( let add of added ) {
242- let detector = new FolderDetector ( add ) ;
265+ let detector = new FolderDetector ( add , findGruntCommand ( add . uri . fsPath ) ) ;
266+ this . detectors . set ( add . uri . toString ( ) , detector ) ;
243267 if ( detector . isEnabled ( ) ) {
244- this . detectors . set ( add . uri . toString ( ) , detector ) ;
245268 detector . start ( ) ;
246269 }
247270 }
@@ -250,18 +273,16 @@ class TaskDetector {
250273
251274 private updateConfiguration ( ) : void {
252275 for ( let detector of this . detectors . values ( ) ) {
253- if ( ! detector . isEnabled ( ) ) {
254- detector . dispose ( ) ;
255- this . detectors . delete ( detector . workspaceFolder . uri . toString ( ) ) ;
256- }
276+ detector . dispose ( ) ;
277+ this . detectors . delete ( detector . workspaceFolder . uri . toString ( ) ) ;
257278 }
258279 let folders = vscode . workspace . workspaceFolders ;
259280 if ( folders ) {
260281 for ( let folder of folders ) {
261282 if ( ! this . detectors . has ( folder . uri . toString ( ) ) ) {
262- let detector = new FolderDetector ( folder ) ;
283+ let detector = new FolderDetector ( folder , findGruntCommand ( folder . uri . fsPath ) ) ;
284+ this . detectors . set ( folder . uri . toString ( ) , detector ) ;
263285 if ( detector . isEnabled ( ) ) {
264- this . detectors . set ( folder . uri . toString ( ) , detector ) ;
265286 detector . start ( ) ;
266287 }
267288 }
@@ -272,12 +293,13 @@ class TaskDetector {
272293
273294 private updateProvider ( ) : void {
274295 if ( ! this . taskProvider && this . detectors . size > 0 ) {
296+ const thisCapture = this ;
275297 this . taskProvider = vscode . workspace . registerTaskProvider ( 'grunt' , {
276- provideTasks : ( ) => {
277- return this . getTasks ( ) ;
298+ provideTasks : ( ) : Promise < vscode . Task [ ] > => {
299+ return thisCapture . getTasks ( ) ;
278300 } ,
279- resolveTask ( _task : vscode . Task ) : vscode . Task | undefined {
280- return undefined ;
301+ resolveTask ( _task : vscode . Task ) : Promise < vscode . Task | undefined > {
302+ return thisCapture . getTask ( _task ) ;
281303 }
282304 } ) ;
283305 }
@@ -312,6 +334,24 @@ class TaskDetector {
312334 } ) ;
313335 }
314336 }
337+
338+ public async getTask ( task : vscode . Task ) : Promise < vscode . Task | undefined > {
339+ if ( this . detectors . size === 0 ) {
340+ return undefined ;
341+ } else if ( this . detectors . size === 1 ) {
342+ return this . detectors . values ( ) . next ( ) . value . getTask ( task ) ;
343+ } else {
344+ if ( ( task . scope === vscode . TaskScope . Workspace ) || ( task . scope === vscode . TaskScope . Global ) ) {
345+ return undefined ;
346+ } else if ( task . scope ) {
347+ const detector = this . detectors . get ( task . scope . uri . toString ( ) ) ;
348+ if ( detector ) {
349+ return detector . getTask ( task ) ;
350+ }
351+ }
352+ return undefined ;
353+ }
354+ }
315355}
316356
317357let detector : TaskDetector ;
0 commit comments