import { ThemeIcon , TreeItem , TreeItemCollapsibleState , TaskDefinition , ExtensionContext , Uri } from "vscode" ; import * as path from "path" ; import * as util from "../common/utils" ; import { configuration } from "../common/configuration" ; import TaskItem from "./item" ; import TaskFolder from "./folder" ; class export default class TaskFile extends TreeItem { public path : string ; property public folder : TaskFolder ; property public treeNodes : ( TaskItem | TaskFile ) [ ] = [ ] ; property public fileName : string ; property public groupLevel : number ; property public nodePath : string ; property readonly public readonly taskSource : string ; property readonly public readonly isGroup : boolean ; property readonly public readonly isUser : boolean ; constructor param context param folder param taskDef param source param relativePath param groupLevel param group param label param logPad constructor ( context , folder , taskDef , source , relativePath , groupLevel , group ? , label ? , logPad = "" ) { super ( TaskFile . getLabel ( taskDef , label ? label : source , relativePath , group || false ) , TreeItemCollapsibleState . Collapsed ) ; this . folder = folder ; this . path = this . label !== "vscode" ? relativePath : ".vscode" ; this . nodePath = this . label !== "vscode" ? relativePath : "vscode" ; this . taskSource = source ; this . isGroup = ( group === true ) ; this . isUser = false ; this . groupLevel = 0 ; this . path = this . label !== "vscode" ? relativePath : ".vscode" ; if ( group && this . label ) { this . nodePath = path . join ( this . nodePath , this . label . toString ( ) ) ; } if ( ! this . nodePath && this . label === "vscode" ) { this . nodePath = path . join ( ".vscode" , this . label ) ; } if ( ! this . nodePath ) { this . nodePath = "" ; } this . fileName = this . getFileNameFromSource ( source , folder , taskDef , true ) ; if ( folder . resourceUri ) { if ( relativePath && source !== "Workspace" ) { this . resourceUri = Uri . file ( path . join ( folder . resourceUri . fsPath , relativePath , this . fileName ) ) ; } else { this . resourceUri = Uri . file ( path . join ( folder . resourceUri . fsPath , this . fileName ) ) ; } } else if ( configuration . get < boolean > ( "readUserTasks" ) ) { this . resourceUri = Uri . file ( path . join ( util . getUserDataPath ( logPad ) , this . fileName ) ) ; this . isUser = true ; } if ( ! group ) { this . contextValue = "taskFile" + util . properCase ( this . taskSource ) ; } else { this . fileName = "group" ; this . tooltip = util . properCase ( source ) + " Task Files" ; this . contextValue = "taskGroup" + util . properCase ( this . taskSource ) ; this . groupLevel = groupLevel ; } this . iconPath = ThemeIcon . File ; let src = this . taskSource ; if ( src === "npm" ) { src = util . getPackageManager ( ) ; } if ( util . pathExists ( context . asAbsolutePath ( path . join ( "res" , "sources" , src + ".svg" ) ) ) ) { this . iconPath = { light : context . asAbsolutePath ( path . join ( "res" , "sources" , src + ".svg" ) ) , dark : context . asAbsolutePath ( path . join ( "res" , "sources" , src + ".svg" ) ) } ; } else if ( util . pathExists ( context . asAbsolutePath ( path . join ( "res" , "light" , src + ".svg" ) ) ) && util . pathExists ( context . asAbsolutePath ( path . join ( "res" , "dark" , src + ".svg" ) ) ) ) { this . iconPath = { light : context . asAbsolutePath ( path . join ( "res" , "light" , src + ".svg" ) ) , dark : context . asAbsolutePath ( path . join ( "res" , "dark" , src + ".svg" ) ) } ; } } method param treeNode public addTreeNode ( treeNode ) { if ( treeNode ) { treeNode . groupLevel = this . groupLevel ; this . treeNodes . push ( treeNode ) ; } } method param treeNode private static getLabel ( taskDef , source , relativePath , group ) { let label = source ; if ( source === "Workspace" ) { label = "vscode" ; } if ( group !== true ) { if ( source === "ant" ) { if ( taskDef . fileName && ! taskDef . fileName . match ( /build.xml/i ) ) { if ( relativePath . length > 0 && relativePath !== ".vscode" && taskDef . type ) { return label + " (" + relativePath . substring ( 0 , relativePath . length - 1 ) . toLowerCase ( ) + "/" + taskDef . fileName . toLowerCase ( ) + ")" ; } return ( label + " (" + taskDef . fileName . toLowerCase ( ) + ")" ) ; } } if ( source === "app-publisher" ) { const match = taskDef . fileName . match ( /\.publishrc\.(.+)\.(?:js(?:on)?|ya?ml)$/i ) ; if ( match && match . length > 1 && match [ 1 ] ) { return ( label + " (" + match [ 1 ] . toLowerCase ( ) + ")" ) ; } } if ( relativePath . length > 0 && relativePath !== ".vscode" && source !== "Workspace" ) { if ( relativePath . endsWith ( "\\" ) || relativePath . endsWith ( "/" ) ) { return label + " (" + relativePath . substring ( 0 , relativePath . length - 1 ) . toLowerCase ( ) + ")" ; } else { return label + " (" + relativePath . toLowerCase ( ) + ")" ; } } } return label . toLowerCase ( ) ; } method private param treeNode returns private getFileNameFromSource ( source , folder , taskDef , incRelPathForCode ? ) { if ( taskDef . fileName ) { return path . basename ( taskDef . fileName ) ; } let fileName = "package.json" ; if ( source === "Workspace" ) { fileName = "tasks.json" ; if ( incRelPathForCode === true && folder . resourceUri ) { fileName = ".vscode/tasks.json" ; } } else if ( source === "tsc" ) { fileName = "tsconfig.json" ; } return fileName ; } method param treeNode param index public insertTreeNode ( treeItem , index ) { this . treeNodes . splice ( index , 0 , treeItem ) ; } method param treeNode public removeTreeNode ( treeItem ) { let idx = - 1 ; let idx2 = - 1 ; this . treeNodes . forEach ( each => { idx ++ ; if ( treeItem === each ) { idx2 = idx ; } } ) ; if ( idx2 !== - 1 && idx2 < this . treeNodes . length ) { this . treeNodes . splice ( idx2 , 1 ) ; } } }