import * as fse from "fs-extra" ; import * as _ from "lodash" ; import * as os from "os" ; import * as path from "path" ; import { Extension , ExtensionContext , extensions } from "vscode" ; import { mavenOutputChannel } from "../mavenOutputChannel" ; import { Utils } from "./Utils" ; let EXTENSION_CONTEXT ; let EXTENSION_PUBLISHER ; let EXTENSION_NAME ; let EXTENSION_VERSION ; let EXTENSION_AI_KEY ; let MAVEN_LOCAL_REPOSITORY ; let TEMP_FOLDER_PER_USER ; export async function loadPackageInfo ( context ) { EXTENSION_CONTEXT = context ; const { publisher , name , version , aiKey } = await fse . readJSON ( context . asAbsolutePath ( "./package.json" ) ) ; EXTENSION_AI_KEY = aiKey ; EXTENSION_PUBLISHER = publisher ; EXTENSION_NAME = name ; EXTENSION_VERSION = version ; TEMP_FOLDER_PER_USER = path . join ( os . tmpdir ( ) , `${ EXTENSION_NAME }-${ os . userInfo ( ) . username }` ) ; await loadMavenSettingsFilePath ( ) ; if ( ! MAVEN_LOCAL_REPOSITORY ) { MAVEN_LOCAL_REPOSITORY = path . join ( os . homedir ( ) , ".m2" , "repository" ) ; } } export async function loadMavenSettingsFilePath ( ) { try { let userSettingsPath = Utils . settingsFilePath ( ) ; if ( ! userSettingsPath ) { userSettingsPath = path . join ( os . homedir ( ) , ".m2" , "settings.xml" ) ; } const userSettings = await Utils . parseXmlFile ( userSettingsPath ) ; MAVEN_LOCAL_REPOSITORY = path . resolve ( _ . get ( userSettings , "settings.localRepository[0]" ) ) ; mavenOutputChannel . appendLine ( `local repository: ${ MAVEN_LOCAL_REPOSITORY }` ) ; } catch ( error ) { } } export function getMavenLocalRepository ( ) { return MAVEN_LOCAL_REPOSITORY ; } export function getExtensionPublisher ( ) { return EXTENSION_PUBLISHER ; } export function getExtensionName ( ) { return EXTENSION_NAME ; } export function getExtensionId ( ) { return `${ EXTENSION_PUBLISHER }.${ EXTENSION_NAME }` ; } export function getExtensionVersion ( ) { return EXTENSION_VERSION ; } export function getAiKey ( ) { return EXTENSION_AI_KEY ; } export function getPathToTempFolder ( ... args ) { return path . join ( TEMP_FOLDER_PER_USER , ... args ) ; } export function getPathToExtensionRoot ( ... args ) { const ext = extensions . getExtension ( getExtensionId ( ) ) ; if ( ! ext ) { throw new Error ( "Cannot identify Maven extension." ) ; } return path . join ( ext . extensionPath , ... args ) ; } export function getPathToWorkspaceStorage ( ... args ) { if ( EXTENSION_CONTEXT . storagePath === undefined ) { return undefined ; } fse . ensureDirSync ( EXTENSION_CONTEXT . storagePath ) ; return path . join ( EXTENSION_CONTEXT . storagePath , ... args ) ; } export function localPomPath ( gid , aid , version ) { return path . join ( getMavenLocalRepository ( ) , ... gid . split ( "." ) , aid , version , `${ aid }-${ version }.pom` ) ; }