import * as Collections from 'typescript-collections' ; import * as vscode from 'vscode' ; import { ScanCacheManager } from '../scanCache/scanCacheManager' ; import { BuildGeneralInfo , Status } from '../types/buildGeneralinfo' ; import { ILicenseCacheObject } from '../types/licenseCacheObject' ; import { ILicenseKey } from '../types/licenseKey' ; import { Consts } from '../utils/consts' ; import { IconsPaths } from '../utils/iconsPaths' ; import { BuildsNode } from './dependenciesTree/ciNodes/buildsTree' ; import { CiTitleNode } from './dependenciesTree/ciNodes/ciTitleNode' ; import { DependenciesTreeNode } from './dependenciesTree/dependenciesTreeNode' ; import { TreeDataHolder } from './utils/treeDataHolder' ; /**
 * The component details tree.
 */ export class ComponentDetailsDataProvider implements vscode . TreeDataProvider < any > { private _onDidChangeTreeData : vscode . EventEmitter < any > = new vscode . EventEmitter < any > ( ) ; readonly onDidChangeTreeData : vscode . Event < any > = this . _onDidChangeTreeData . event ; private _selectedNode : DependenciesTreeNode | undefined ; constructor ( private _scanCacheManager ) { } refresh ( ) { this . _onDidChangeTreeData . fire ( ) ; } getTreeItem ( element ) { if ( element instanceof LicensesNode ) { return element ; } let holder = < TreeDataHolder > element ; let treeItem = new vscode . TreeItem ( holder . key ) ; treeItem . description = holder . value ; if ( holder . link ) { treeItem . command = { command : 'vscode.open' , arguments : [ vscode . Uri . parse ( holder . link ) ] } as vscode . Command ; } if ( holder . icon ) { treeItem . iconPath = holder . icon ; } return treeItem ; } getChildren ( element ? ) { if ( ! this . _selectedNode ) { return Promise . resolve ( [ ] ) ; } if ( element instanceof LicensesNode ) { return Promise . resolve ( element . getChildren ( ) ) ; } if ( this . _selectedNode instanceof BuildsNode ) { return Promise . resolve ( this . getBuildChildren ( ) ) ; } let children = [ new TreeDataHolder ( 'Artifact' , this . _selectedNode . generalInfo . artifactId ) ] ; if ( ! ( this . _selectedNode instanceof CiTitleNode && ! this . _selectedNode . generalInfo . version ) ) { children . push ( new TreeDataHolder ( 'Version' , this . _selectedNode . generalInfo . version ) ) ; } children . push ( new TreeDataHolder ( 'Type' , this . _selectedNode . generalInfo . pkgType ) ) ; const scopes = this . _selectedNode . generalInfo . scopes ; if ( scopes . length > 0 && ! this . isNoneScope ( scopes ) ) { children . push ( new TreeDataHolder ( 'Scopes' , this . _selectedNode . generalInfo . scopes . join ( ',' ) ) ) ; } children . push ( new TreeDataHolder ( 'Issues count' , String ( this . _selectedNode . issues . size ( ) ) ) ) ; let path = this . _selectedNode . generalInfo . path ; if ( path ) { children . push ( new TreeDataHolder ( 'Path' , path ) ) ; } if ( ! ( this . _selectedNode instanceof CiTitleNode ) ) { children . push ( new LicensesNode ( this . _scanCacheManager , this . _selectedNode . licenses ) ) ; } return Promise . resolve ( children ) ; } private isNoneScope ( scopes ) { return scopes . length === 1 && scopes [ 0 ] === 'None' ; } getBuildChildren ( ) { if ( ! this . _selectedNode ) { return Promise . resolve ( [ ] ) ; } const bgi = < BuildGeneralInfo > this . _selectedNode . generalInfo ; const status = Status [ bgi . status ] ; let children = [ new TreeDataHolder ( 'Status' , status ) , new TreeDataHolder ( 'Date' , bgi . startedReadable ) , new TreeDataHolder ( 'Branch' , bgi . vcs . branch ) , new TreeDataHolder ( 'Commit Message' , bgi . vcs . message ) ] ; let link = bgi . path ; if ( link ) { children . push ( new TreeDataHolder ( 'Build Log' , link , link ) ) ; } return Promise . resolve ( children ) ; } public selectNode ( selectedNode ) { this . _selectedNode = selectedNode ; this . refresh ( ) ; } } export class LicensesNode extends vscode . TreeItem { constructor ( private _scanCacheManager , private _licenses ) { super ( 'Licenses' , vscode . TreeItemCollapsibleState . Expanded ) ; } public getChildren ( ) { let children = [ ] ; this . _licenses . forEach ( licenseKey => { let license = this . _scanCacheManager . getLicense ( licenseKey . licenseName ) ; if ( ! license ) { return ; } let icon ; if ( licenseKey . violated ) { icon = IconsPaths . VIOLATED_LICENSE ; } children . push ( new TreeDataHolder ( this . createLicenseString ( license ) , license . moreInfoUrl , license . moreInfoUrl , icon ) ) ; } ) ; return children ; } public createLicenseString ( license ) { if ( ! license . fullName && this . isFullNameEmpty ( license ) ) { return license . name ; } return license . fullName + ' (' + license . name + ')' ; } public isFullNameEmpty ( license ) { return ! license . fullName || license . fullName === Consts . UNKNOWN_LICENSE_FULL_NAME ; } }