import { Uri , Task , WorkspaceFolder , workspace , TaskProvider } from "vscode" ; import { TaskExplorerDefinition } from "../taskDefinition" ; import { configuration } from "../common/configuration" ; import * as util from "../common/utils" ; import * as log from "../common/log" ; import TaskItem from "../tree/item" ; export abstract class TaskExplorerProvider implements TaskProvider { abstract getDefaultDefinition ( target , folder , uri ) ; abstract createTask ( target , cmd , folder , uri , xArgs ? , logPad ? ) ; abstract getDocumentPosition ( taskName , documentText ) ; abstract readTasks ( logPad ? ) ; abstract readUriTasks ( uri , wsFolder ? , logPad ? ) ; public cachedTasks : Task [ ] | undefined ; public invalidating = false ; public providerName = "***" ; private queue : Uri [ ] ; constructor ( name ) { this . providerName = name ; this . queue = [ ] ; } getDocumentPositionLine ( lineName , scriptName , documentText , advance = 0 , start = 0 , skipQuotes = false ) { if ( ! scriptName || ! documentText ) { return 0 ; } let idx = documentText . indexOf ( lineName + ( ! skipQuotes ? "\"" : "" ) + scriptName + ( ! skipQuotes ? "\"" : "" ) , start ) ; if ( idx === - 1 ) { idx = documentText . indexOf ( lineName + ( ! skipQuotes ? "'" : "" ) + scriptName + ( ! skipQuotes ? "'" : "" ) , start ) ; } if ( advance !== 0 && idx !== - 1 ) { idx += advance ; } return idx ; } public async provideTasks ( ) { log . methodStart ( `provide ${ this . providerName } tasks` , 1 , "" , true ) ; if ( ! this . cachedTasks ) { this . cachedTasks = await this . readTasks ( "   " ) ; } return this . cachedTasks ; } public resolveTask ( _task ) { return undefined ; } public async invalidateTasksCache ( uri ? , logPad = "" ) { log . methodStart ( `invalidate ${ this . providerName } tasks cache` , 1 , logPad , true , [ [ "uri" , uri ?. path ] , [ "has cached tasks" , ! ! this . cachedTasks ] ] ) ; if ( uri && this . invalidating ) { this . queue . push ( uri ) ; return ; } this . invalidating = true ; if ( uri && this . cachedTasks ) { const rmvTasks = [ ] ; const folder = workspace . getWorkspaceFolder ( uri ) ; for ( const each of this . cachedTasks ) { const cstDef = each . definition ; if ( cstDef . uri && ( cstDef . uri . fsPath === uri . fsPath || ! util . pathExists ( cstDef . uri . fsPath ) ) ) { rmvTasks . push ( each ) ; } } if ( this . cachedTasks ) { for ( const each of rmvTasks ) { log . write ( "   removing old task " + each . name , 2 , logPad ) ; util . removeFromArray ( this . cachedTasks , each ) ; } if ( util . pathExists ( uri . fsPath ) && util . existsInArray ( configuration . get ( "exclude" ) || [ ] , uri . path ) === false ) { const tasks = await this . readUriTasks ( uri , folder , logPad + "   " ) ; this . cachedTasks ?. push ( ... tasks ) ; } if ( this . cachedTasks ?. length > 0 ) { await this . processQueue ( ) ; return ; } } } this . cachedTasks = undefined ; log . methodDone ( `invalidate ${ this . providerName } tasks cache` , 1 , logPad ) ; await this . processQueue ( ) ; } private async processQueue ( ) { this . invalidating = false ; if ( this . queue . length > 0 ) { await this . invalidateTasksCache ( this . queue . shift ( ) ) ; } } }