import { TreeDataProvider } from "vscode" ; import * as vscode from "vscode" ; import { Utils } from "../utils/Utils" ; import { Dependency } from "./model/Dependency" ; import { ITreeItem } from "./model/ITreeItem" ; import { MavenProject } from "./model/MavenProject" ; import { WorkspaceFolder } from "./model/WorkspaceFolder" ; class MavenExplorerProvider implements TreeDataProvider < ITreeItem > { public readonly onDidChangeTreeData : vscode . Event < ITreeItem | undefined > ; private _onDidChangeTreeData : vscode . EventEmitter < ITreeItem | undefined > ; private _projectMap : Map < string , MavenProject > = new Map ( ) ; constructor ( ) { this . _onDidChangeTreeData = new vscode . EventEmitter < ITreeItem > ( ) ; this . onDidChangeTreeData = this . _onDidChangeTreeData . event ; this . refresh ( ) ; } public get mavenProjectNodes ( ) : MavenProject [ ] { return Array . from ( this . _projectMap . values ( ) ) ; } public updateProjects ( ... items ) { for ( const item of items ) { this . _projectMap . set ( item . pomPath , item ) ; } } public addProject ( pomPath ) { this . _projectMap . set ( pomPath , new MavenProject ( pomPath ) ) ; this . refresh ( ) ; } public removeProject ( pomPath ) { if ( this . _projectMap . has ( pomPath ) ) { this . _projectMap . delete ( pomPath ) ; this . refresh ( ) ; } } public async addWorkspaceFolder ( folder ) { await this . loadProjects ( folder ) ; this . refresh ( ) ; } public async removeWorkspaceFolder ( folder ) { const pomPaths = await Utils . getAllPomPaths ( folder ) ; for ( const pomPath of pomPaths ) { this . _projectMap . delete ( pomPath ) ; } this . refresh ( ) ; } public getMavenProject ( pomPath ) { return this . _projectMap . get ( pomPath ) ; } public getTreeItem ( element ) { return Promise . resolve ( element . getTreeItem ( ) ) . then ( item => { item . contextValue = element . getContextValue ( ) ; return item ; } ) ; } public async getChildren ( element ? ) { if ( ! vscode . workspace . isTrusted ) { return undefined ; } if ( element === undefined ) { if ( ! vscode . workspace . workspaceFolders ) { return undefined ; } if ( vscode . workspace . workspaceFolders . length === 1 ) { return await new WorkspaceFolder ( vscode . workspace . workspaceFolders [ 0 ] ) . getChildren ( ) ; } return vscode . workspace . workspaceFolders . map ( workspaceFolder => new WorkspaceFolder ( workspaceFolder ) ) ; } else { return element . getChildren ? element . getChildren ( ) : undefined ; } } public async getParent ( element ) { if ( element instanceof Dependency ) { return element . parent ; } else { return undefined ; } } public refresh ( item ? ) { this . _onDidChangeTreeData . fire ( item ) ; } public async loadProjects ( workspaceFolder ? ) { const newProjects = [ ] ; const allProjects = [ ] ; const pomPaths = await Utils . getAllPomPaths ( workspaceFolder ) ; for ( const pomPath of pomPaths ) { let currentProject = mavenExplorerProvider . getMavenProject ( pomPath ) ; if ( ! currentProject ) { currentProject = new MavenProject ( pomPath ) ; newProjects . push ( currentProject ) ; } allProjects . push ( currentProject ) ; } await Promise . all ( newProjects . map ( async ( elem ) => elem . parsePom ( ) ) ) ; mavenExplorerProvider . updateProjects ( ... newProjects ) ; newProjects . forEach ( p => { p . modules . forEach ( m => { const moduleNode = mavenExplorerProvider . getMavenProject ( m ) ; if ( moduleNode && moduleNode . parent === undefined ) { moduleNode . parent = p ; } } ) ; } ) ; return allProjects ; } } export const mavenExplorerProvider = new MavenExplorerProvider ( ) ;