import { Task , TaskExecution , TreeItem , TreeItemCollapsibleState , WorkspaceFolder , ExtensionContext , tasks } from "vscode" ; import * as path from "path" ; import TaskFile from "./file" ; import * as util from "../common/utils" ; class export default class TaskItem extends TreeItem { private readonly context : ExtensionContext ; public static readonly defaultSource = "Workspace" ; public readonly taskSource : string ; public readonly taskGroup : string ; public readonly isUser : boolean ; public task : Task | undefined ; public execution : TaskExecution | undefined ; public paused : boolean ; /**
     * Equivalent to task.definition.path
     */ public nodePath : string ; public groupLevel : number ; taskFile : TaskFile ; constructor ( context , taskFile , task , taskGroup = "" , groupLevel = 0 ) { const getDisplayName = ( taskName , taskGroup ) : string => { let displayName = taskName ; if ( displayName . indexOf ( " - " ) !== - 1 && ( displayName . indexOf ( "/" ) !== - 1 || displayName . indexOf ( "\\" ) !== - 1 || displayName . indexOf ( " - tsconfig.json" ) !== - 1 ) ) { if ( taskGroup ) { displayName = taskName . replace ( taskGroup + util . getGroupSeparator ( ) , "" ) ; } else { displayName = task . name . substring ( 0 , taskName . indexOf ( " - " ) ) ; } } return displayName ; } ; super ( getDisplayName ( task . name , taskGroup ) , TreeItemCollapsibleState . None ) ; this . context = context ; this . taskGroup = taskGroup ; this . isUser = taskFile . isUser ; const fsPath = taskFile . resourceUri ? taskFile . resourceUri . fsPath : "root" ; this . id = fsPath + ":" + task . source + ":" + task . name + ":" + ( taskGroup || "" ) ; this . paused = false ; this . taskFile = taskFile ; this . task = task ; this . groupLevel = groupLevel ; this . command = { title : "Open definition" , command : "taskExplorer.open" , arguments : [ this ] } ; this . taskSource = task . source ; this . task . definition . taskItemId = this . id ; this . nodePath = taskFile . nodePath ; this . tooltip = "Open " + task . name + ( task . detail ? ` | ${ task . detail }` : "" ) ; this . refreshState ( ) ; } getFolder ( ) { return this . taskFile . folder . workspaceFolder ; } isExecuting ( task ? ) { this . task = task ?? this . task ; if ( this . task ) { this . execution = tasks . taskExecutions . find ( e => e . task . name === this . task ?. name && e . task . source === this . task . source && e . task . scope === this . task . scope && e . task . definition . path === this . task . definition . path ) ; return ! ! this . execution ; } return false ; } refreshState ( task ? ) { const isExecuting = this . isExecuting ( task ) ; if ( this . task ) { this . setContextValue ( this . task , isExecuting ) ; this . setIconPath ( this . task , this . context , isExecuting ) ; } } setContextValue ( task , running ) { if ( task . definition . scriptFile || this . taskSource === "gradle" ) { this . contextValue = running ? "scriptRunning" : "scriptFile" ; } else { this . contextValue = running ? "scriptRunning" : "script" ; } } setIconPath ( task , context , running ) { if ( running ) { this . iconPath = { light : context . asAbsolutePath ( path . join ( "res" , "light" , "loading.svg" ) ) , dark : context . asAbsolutePath ( path . join ( "res" , "dark" , "loading.svg" ) ) } ; } else { this . iconPath = { light : context . asAbsolutePath ( path . join ( "res" , "light" , "script.svg" ) ) , dark : context . asAbsolutePath ( path . join ( "res" , "dark" , "script.svg" ) ) } ; } } }