import * as _ from "lodash" ; import * as vscode from "vscode" ; import { fetchPluginMetadataXml } from "../utils/requestUtils" ; import { Utils } from "../utils/Utils" ; const KEY_PLUGINS = "plugins" ; interface IPluginCache { [ groupId ] : { [ artifactId ] : IPluginInfo ; } ; } interface IPluginInfo { prefix ? : string ; versions ? : { [ version ] : string [ ] ; } ; } class PluginInfoProvider { private _context : vscode . ExtensionContext ; public initialize ( context ) { this . _context = context ; } public async getPluginPrefix ( gid , aid ) { const infos = _ . get ( this . getPluginCache ( ) , [ gid ] ) ?? { } ; const info = _ . get ( infos , [ aid ] ) ?? { } ; if ( info . prefix !== undefined ) { return info . prefix ; } const metadataXml = await fetchPluginMetadataXml ( gid ) ; const xml = await Utils . parseXmlContent ( metadataXml ) ; const plugins = _ . get ( xml , "metadata.plugins[0].plugin" ) ; plugins . forEach ( plugin => { const a = _ . get ( plugin , "artifactId[0]" ) ; const p = _ . get ( plugin , "prefix[0]" ) ; infos [ a ] = infos [ a ] ?? { } ; infos [ a ] . prefix = p ; } ) ; await this . cachePluginInfos ( gid , infos ) ; return infos [ aid ] ?. prefix ; } public async getPluginGoals ( pomPath , groupId , artifactId , version ) { const infos = _ . get ( this . getPluginCache ( ) , [ groupId ] ) ?? { } ; const info = _ . get ( infos , [ artifactId ] ) ?? { } ; info . versions = info . versions ?? { } ; const goalsFromCache = _ . get ( info . versions , [ version ] ) ; if ( goalsFromCache !== undefined ) { return goalsFromCache ; } const { prefix , goals } = await this . fetchFromRepository ( pomPath , groupId , artifactId , version ) ; info . prefix = info . prefix ?? prefix ; info . versions [ version ] = goals ?? [ ] ; await this . cachePluginInfo ( groupId , artifactId , info ) ; return goals ; } private getPluginCache ( ) { return this . _context . globalState . get ( KEY_PLUGINS ) ?? { } ; } private async cachePluginInfos ( gid , infos ) { const plugins = this . _context . globalState . get ( KEY_PLUGINS ) ?? { } ; _ . set ( plugins , [ gid ] , infos ) ; await this . _context . globalState . update ( KEY_PLUGINS , plugins ) ; } private async cachePluginInfo ( gid , aid , info ) { const plugins = this . _context . globalState . get ( KEY_PLUGINS ) ?? { } ; _ . set ( plugins , [ gid , aid ] , info ) ; await this . _context . globalState . update ( KEY_PLUGINS , plugins ) ; } private async fetchFromRepository ( projectBasePath , gid , aid , version ? ) { let prefix ; const goals = [ ] ; const textOutput = await Utils . getPluginDescription ( this . getPluginId ( gid , aid , version ) , projectBasePath ) ; const versionRegExp = /^Version: (.*)/m ; const versionMatch = textOutput . match ( versionRegExp ) ; if ( versionMatch !== null && versionMatch . length === 2 ) { version = versionMatch [ 1 ] ; } const prefixRegExp = /^Goal Prefix: (.*)/m ; const prefixMatch = textOutput . match ( prefixRegExp ) ; if ( prefixMatch !== null && prefixMatch . length === 2 ) { prefix = prefixMatch [ 1 ] ; } if ( version && prefix !== undefined ) { const goalRegExp = new RegExp ( `^${ prefix }:(.*)` , "gm" ) ; const goalsMatch = textOutput . match ( goalRegExp ) ; if ( goalsMatch !== null ) { goals . push ( ... goalsMatch ) ; } } return { prefix , goals } ; } private getPluginId ( gid , aid , version ? ) { return `${ gid }:${ aid }${ version ? `:${ version }` : "" }` ; } } export const pluginInfoProvider = new PluginInfoProvider ( ) ;