@@ -67,6 +67,24 @@ function showError() {
6767 } ) ;
6868}
6969
70+ async function findGulpCommand ( rootPath : string ) : Promise < string > {
71+ let gulpCommand : string ;
72+ let platform = process . platform ;
73+ if ( platform === 'win32' && await exists ( path . join ( rootPath , 'node_modules' , '.bin' , 'gulp.cmd' ) ) ) {
74+ const globalGulp = path . join ( process . env . APPDATA ? process . env . APPDATA : '' , 'npm' , 'gulp.cmd' ) ;
75+ if ( await exists ( globalGulp ) ) {
76+ gulpCommand = '"' + globalGulp + '"' ;
77+ } else {
78+ gulpCommand = path . join ( '.' , 'node_modules' , '.bin' , 'gulp.cmd' ) ;
79+ }
80+ } else if ( ( platform === 'linux' || platform === 'darwin' ) && await exists ( path . join ( rootPath , 'node_modules' , '.bin' , 'gulp' ) ) ) {
81+ gulpCommand = path . join ( '.' , 'node_modules' , '.bin' , 'gulp' ) ;
82+ } else {
83+ gulpCommand = 'gulp' ;
84+ }
85+ return gulpCommand ;
86+ }
87+
7088interface GulpTaskDefinition extends vscode . TaskDefinition {
7189 task : string ;
7290 file ?: string ;
@@ -77,7 +95,9 @@ class FolderDetector {
7795 private fileWatcher : vscode . FileSystemWatcher | undefined ;
7896 private promise : Thenable < vscode . Task [ ] > | undefined ;
7997
80- constructor ( private _workspaceFolder : vscode . WorkspaceFolder ) {
98+ constructor (
99+ private _workspaceFolder : vscode . WorkspaceFolder ,
100+ private _gulpCommand : Promise < string > ) {
81101 }
82102
83103 public get workspaceFolder ( ) : vscode . WorkspaceFolder {
@@ -97,10 +117,28 @@ class FolderDetector {
97117 }
98118
99119 public async getTasks ( ) : Promise < vscode . Task [ ] > {
100- if ( ! this . promise ) {
101- this . promise = this . computeTasks ( ) ;
120+ if ( this . isEnabled ( ) ) {
121+ if ( ! this . promise ) {
122+ this . promise = this . computeTasks ( ) ;
123+ }
124+ return this . promise ;
125+ } else {
126+ return [ ] ;
127+ }
128+ }
129+
130+ public async getTask ( _task : vscode . Task ) : Promise < vscode . Task | undefined > {
131+ const gulpTask = ( < any > _task . definition ) . task ;
132+ if ( gulpTask ) {
133+ let kind : GulpTaskDefinition = {
134+ type : 'gulp' ,
135+ task : gulpTask
136+ } ;
137+ let options : vscode . ShellExecutionOptions = { cwd : this . workspaceFolder . uri . fsPath } ;
138+ let task = new vscode . Task ( kind , this . workspaceFolder , gulpTask , 'gulp' , new vscode . ShellExecution ( await this . _gulpCommand , [ gulpTask ] , options ) ) ;
139+ return task ;
102140 }
103- return this . promise ;
141+ return undefined ;
104142 }
105143
106144 private async computeTasks ( ) : Promise < vscode . Task [ ] > {
@@ -117,22 +155,7 @@ class FolderDetector {
117155 }
118156 }
119157
120- let gulpCommand : string ;
121- let platform = process . platform ;
122- if ( platform === 'win32' && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'gulp.cmd' ) ) ) {
123- const globalGulp = path . join ( process . env . APPDATA ? process . env . APPDATA : '' , 'npm' , 'gulp.cmd' ) ;
124- if ( await exists ( globalGulp ) ) {
125- gulpCommand = '"' + globalGulp + '"' ;
126- } else {
127- gulpCommand = path . join ( '.' , 'node_modules' , '.bin' , 'gulp.cmd' ) ;
128- }
129- } else if ( ( platform === 'linux' || platform === 'darwin' ) && await exists ( path . join ( rootPath ! , 'node_modules' , '.bin' , 'gulp' ) ) ) {
130- gulpCommand = path . join ( '.' , 'node_modules' , '.bin' , 'gulp' ) ;
131- } else {
132- gulpCommand = 'gulp' ;
133- }
134-
135- let commandLine = `${ gulpCommand } --tasks-simple --no-color` ;
158+ let commandLine = `${ await this . _gulpCommand } --tasks-simple --no-color` ;
136159 try {
137160 let { stdout, stderr } = await exec ( commandLine , { cwd : rootPath } ) ;
138161 if ( stderr && stderr . length > 0 ) {
@@ -151,7 +174,7 @@ class FolderDetector {
151174 task : line
152175 } ;
153176 let options : vscode . ShellExecutionOptions = { cwd : this . workspaceFolder . uri . fsPath } ;
154- let task = new vscode . Task ( kind , this . workspaceFolder , line , 'gulp' , new vscode . ShellExecution ( gulpCommand , [ line ] , options ) ) ;
177+ let task = new vscode . Task ( kind , this . workspaceFolder , line , 'gulp' , new vscode . ShellExecution ( await this . _gulpCommand , [ line ] , options ) ) ;
155178 result . push ( task ) ;
156179 let lowerCaseLine = line . toLowerCase ( ) ;
157180 if ( isBuildTask ( lowerCaseLine ) ) {
@@ -218,9 +241,9 @@ class TaskDetector {
218241 }
219242 }
220243 for ( let add of added ) {
221- let detector = new FolderDetector ( add ) ;
244+ let detector = new FolderDetector ( add , findGulpCommand ( add . uri . fsPath ) ) ;
245+ this . detectors . set ( add . uri . toString ( ) , detector ) ;
222246 if ( detector . isEnabled ( ) ) {
223- this . detectors . set ( add . uri . toString ( ) , detector ) ;
224247 detector . start ( ) ;
225248 }
226249 }
@@ -229,18 +252,16 @@ class TaskDetector {
229252
230253 private updateConfiguration ( ) : void {
231254 for ( let detector of this . detectors . values ( ) ) {
232- if ( ! detector . isEnabled ( ) ) {
233- detector . dispose ( ) ;
234- this . detectors . delete ( detector . workspaceFolder . uri . toString ( ) ) ;
235- }
255+ detector . dispose ( ) ;
256+ this . detectors . delete ( detector . workspaceFolder . uri . toString ( ) ) ;
236257 }
237258 let folders = vscode . workspace . workspaceFolders ;
238259 if ( folders ) {
239260 for ( let folder of folders ) {
240261 if ( ! this . detectors . has ( folder . uri . toString ( ) ) ) {
241- let detector = new FolderDetector ( folder ) ;
262+ let detector = new FolderDetector ( folder , findGulpCommand ( folder . uri . fsPath ) ) ;
263+ this . detectors . set ( folder . uri . toString ( ) , detector ) ;
242264 if ( detector . isEnabled ( ) ) {
243- this . detectors . set ( folder . uri . toString ( ) , detector ) ;
244265 detector . start ( ) ;
245266 }
246267 }
@@ -251,12 +272,13 @@ class TaskDetector {
251272
252273 private updateProvider ( ) : void {
253274 if ( ! this . taskProvider && this . detectors . size > 0 ) {
275+ const thisCapture = this ;
254276 this . taskProvider = vscode . workspace . registerTaskProvider ( 'gulp' , {
255- provideTasks : ( ) = > {
256- return this . getTasks ( ) ;
277+ provideTasks ( ) : Promise < vscode . Task [ ] > {
278+ return thisCapture . getTasks ( ) ;
257279 } ,
258- resolveTask ( _task : vscode . Task ) : vscode . Task | undefined {
259- return undefined ;
280+ resolveTask ( _task : vscode . Task ) : Promise < vscode . Task | undefined > {
281+ return thisCapture . getTask ( _task ) ;
260282 }
261283 } ) ;
262284 }
@@ -291,6 +313,25 @@ class TaskDetector {
291313 } ) ;
292314 }
293315 }
316+
317+ public async getTask ( task : vscode . Task ) : Promise < vscode . Task | undefined > {
318+ if ( this . detectors . size === 0 ) {
319+ return undefined ;
320+ } else if ( this . detectors . size === 1 ) {
321+ return this . detectors . values ( ) . next ( ) . value . getTask ( task ) ;
322+ } else {
323+ if ( ( task . scope === vscode . TaskScope . Workspace ) || ( task . scope === vscode . TaskScope . Global ) ) {
324+ // Not supported, we don't have enough info to create the task.
325+ return undefined ;
326+ } else if ( task . scope ) {
327+ const detector = this . detectors . get ( task . scope . uri . toString ( ) ) ;
328+ if ( detector ) {
329+ return detector . getTask ( task ) ;
330+ }
331+ }
332+ return undefined ;
333+ }
334+ }
294335}
295336
296337let detector : TaskDetector ;
0 commit comments