@@ -66,6 +66,19 @@ function showError() {
6666 } ) ;
6767}
6868
69+ async function findJakeCommand ( rootPath : string ) : Promise < string > {
70+ let jakeCommand : string ;
71+ let platform = process . platform ;
72+ if ( platform === 'win32' && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'jake.cmd' ) ) ) {
73+ jakeCommand = path . join ( '.' , 'node_modules' , '.bin' , 'jake.cmd' ) ;
74+ } else if ( ( platform === 'linux' || platform === 'darwin' ) && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'jake' ) ) ) {
75+ jakeCommand = path . join ( '.' , 'node_modules' , '.bin' , 'jake' ) ;
76+ } else {
77+ jakeCommand = 'jake' ;
78+ }
79+ return jakeCommand ;
80+ }
81+
6982interface JakeTaskDefinition extends vscode . TaskDefinition {
7083 task : string ;
7184 file ?: string ;
@@ -76,7 +89,9 @@ class FolderDetector {
7689 private fileWatcher : vscode . FileSystemWatcher | undefined ;
7790 private promise : Thenable < vscode . Task [ ] > | undefined ;
7891
79- constructor ( private _workspaceFolder : vscode . WorkspaceFolder ) {
92+ constructor (
93+ private _workspaceFolder : vscode . WorkspaceFolder ,
94+ private _jakeCommand : Promise < string > ) {
8095 }
8196
8297 public get workspaceFolder ( ) : vscode . WorkspaceFolder {
@@ -96,10 +111,28 @@ class FolderDetector {
96111 }
97112
98113 public async getTasks ( ) : Promise < vscode . Task [ ] > {
99- if ( ! this . promise ) {
100- this . promise = this . computeTasks ( ) ;
114+ if ( this . isEnabled ( ) ) {
115+ if ( ! this . promise ) {
116+ this . promise = this . computeTasks ( ) ;
117+ }
118+ return this . promise ;
119+ } else {
120+ return [ ] ;
101121 }
102- return this . promise ;
122+ }
123+
124+ public async getTask ( _task : vscode . Task ) : Promise < vscode . Task | undefined > {
125+ const jakeTask = ( < any > _task . definition ) . task ;
126+ if ( jakeTask ) {
127+ let kind : JakeTaskDefinition = {
128+ type : 'jake' ,
129+ task : jakeTask
130+ } ;
131+ let options : vscode . ShellExecutionOptions = { cwd : this . workspaceFolder . uri . fsPath } ;
132+ let task = new vscode . Task ( kind , this . workspaceFolder , jakeTask , 'jake' , new vscode . ShellExecution ( await this . _jakeCommand , [ jakeTask ] , options ) ) ;
133+ return task ;
134+ }
135+ return undefined ;
103136 }
104137
105138 private async computeTasks ( ) : Promise < vscode . Task [ ] > {
@@ -116,17 +149,7 @@ class FolderDetector {
116149 }
117150 }
118151
119- let jakeCommand : string ;
120- let platform = process . platform ;
121- if ( platform === 'win32' && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'jake.cmd' ) ) ) {
122- jakeCommand = path . join ( '.' , 'node_modules' , '.bin' , 'jake.cmd' ) ;
123- } else if ( ( platform === 'linux' || platform === 'darwin' ) && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'jake' ) ) ) {
124- jakeCommand = path . join ( '.' , 'node_modules' , '.bin' , 'jake' ) ;
125- } else {
126- jakeCommand = 'jake' ;
127- }
128-
129- let commandLine = `${ jakeCommand } --tasks` ;
152+ let commandLine = `${ await this . _jakeCommand } --tasks` ;
130153 try {
131154 let { stdout, stderr } = await exec ( commandLine , { cwd : rootPath } ) ;
132155 if ( stderr ) {
@@ -149,7 +172,7 @@ class FolderDetector {
149172 task : taskName
150173 } ;
151174 let options : vscode . ShellExecutionOptions = { cwd : this . workspaceFolder . uri . fsPath } ;
152- let task = new vscode . Task ( kind , taskName , 'jake' , new vscode . ShellExecution ( `${ jakeCommand } ${ taskName } ` , options ) ) ;
175+ let task = new vscode . Task ( kind , taskName , 'jake' , new vscode . ShellExecution ( `${ await this . _jakeCommand } ${ taskName } ` , options ) ) ;
153176 result . push ( task ) ;
154177 let lowerCaseLine = line . toLowerCase ( ) ;
155178 if ( isBuildTask ( lowerCaseLine ) ) {
@@ -217,9 +240,9 @@ class TaskDetector {
217240 }
218241 }
219242 for ( let add of added ) {
220- let detector = new FolderDetector ( add ) ;
243+ let detector = new FolderDetector ( add , findJakeCommand ( add . uri . fsPath ) ) ;
244+ this . detectors . set ( add . uri . toString ( ) , detector ) ;
221245 if ( detector . isEnabled ( ) ) {
222- this . detectors . set ( add . uri . toString ( ) , detector ) ;
223246 detector . start ( ) ;
224247 }
225248 }
@@ -228,18 +251,16 @@ class TaskDetector {
228251
229252 private updateConfiguration ( ) : void {
230253 for ( let detector of this . detectors . values ( ) ) {
231- if ( ! detector . isEnabled ( ) ) {
232- detector . dispose ( ) ;
233- this . detectors . delete ( detector . workspaceFolder . uri . toString ( ) ) ;
234- }
254+ detector . dispose ( ) ;
255+ this . detectors . delete ( detector . workspaceFolder . uri . toString ( ) ) ;
235256 }
236257 let folders = vscode . workspace . workspaceFolders ;
237258 if ( folders ) {
238259 for ( let folder of folders ) {
239260 if ( ! this . detectors . has ( folder . uri . toString ( ) ) ) {
240- let detector = new FolderDetector ( folder ) ;
261+ let detector = new FolderDetector ( folder , findJakeCommand ( folder . uri . fsPath ) ) ;
262+ this . detectors . set ( folder . uri . toString ( ) , detector ) ;
241263 if ( detector . isEnabled ( ) ) {
242- this . detectors . set ( folder . uri . toString ( ) , detector ) ;
243264 detector . start ( ) ;
244265 }
245266 }
@@ -250,12 +271,13 @@ class TaskDetector {
250271
251272 private updateProvider ( ) : void {
252273 if ( ! this . taskProvider && this . detectors . size > 0 ) {
274+ const thisCapture = this ;
253275 this . taskProvider = vscode . workspace . registerTaskProvider ( 'gulp' , {
254- provideTasks : ( ) = > {
255- return this . getTasks ( ) ;
276+ provideTasks ( ) : Promise < vscode . Task [ ] > {
277+ return thisCapture . getTasks ( ) ;
256278 } ,
257- resolveTask ( _task : vscode . Task ) : vscode . Task | undefined {
258- return undefined ;
279+ resolveTask ( _task : vscode . Task ) : Promise < vscode . Task | undefined > {
280+ return thisCapture . getTask ( _task ) ;
259281 }
260282 } ) ;
261283 }
@@ -290,6 +312,25 @@ class TaskDetector {
290312 } ) ;
291313 }
292314 }
315+
316+ public async getTask ( task : vscode . Task ) : Promise < vscode . Task | undefined > {
317+ if ( this . detectors . size === 0 ) {
318+ return undefined ;
319+ } else if ( this . detectors . size === 1 ) {
320+ return this . detectors . values ( ) . next ( ) . value . getTask ( task ) ;
321+ } else {
322+ if ( ( task . scope === vscode . TaskScope . Workspace ) || ( task . scope === vscode . TaskScope . Global ) ) {
323+ // Not supported, we don't have enough info to create the task.
324+ return undefined ;
325+ } else if ( task . scope ) {
326+ const detector = this . detectors . get ( task . scope . uri . toString ( ) ) ;
327+ if ( detector ) {
328+ return detector . getTask ( task ) ;
329+ }
330+ }
331+ return undefined ;
332+ }
333+ }
293334}
294335
295336let detector : TaskDetector ;
0 commit comments