import * as fse from 'fs-extra' ; import * as vscode from 'vscode' ; import { DialogResponses , IActionContext , IAzExtOutputChannel , TelemetryProperties } from "vscode-azureextensionui" ; import * as xml2js from 'xml2js' ; import { localize } from '../localize' ; import { cpUtils } from './cpUtils' ; import { openUrl } from './openUrl' ; export namespace mavenUtils { const mvnCommand = 'mvn' ; export async function validateMavenInstalled ( context ) { try { await cpUtils . executeCommand ( undefined , undefined , mvnCommand , '--version' ) ; } catch ( error ) { const message = localize ( 'mvnNotFound' , 'Failed to find "maven", please ensure that the maven bin directory is in your system path.' ) ; if ( ! context . errorHandling . suppressDisplay ) { void vscode . window . showErrorMessage ( message , DialogResponses . learnMore ) . then ( async ( result ) => { if ( result === DialogResponses . learnMore ) { await openUrl ( 'https://aka.ms/azurefunction_maven' ) ; } } ) ; context . errorHandling . suppressDisplay = true ; } throw new Error ( message ) ; } } export async function getFunctionAppNameInPom ( pomLocation ) { const pomString = await fse . readFile ( pomLocation , 'utf-8' ) ; return await new Promise ( ( resolve ) : void => { xml2js . parseString ( pomString , { explicitArray : false } , ( err , result ) : void => { if ( result && ! err ) { if ( result [ 'project' ] && result [ 'project' ] [ 'properties' ] ) { resolve ( result [ 'project' ] [ 'properties' ] [ 'functionAppName' ] ) ; return ; } } resolve ( undefined ) ; } ) ; } ) ; } export async function executeMvnCommand ( telemetryProperties , outputChannel , workingDirectory , ... args ) { const result = await cpUtils . tryExecuteCommand ( outputChannel , workingDirectory , mvnCommand , ... args ) ; if ( result . code !== 0 ) { const mvnErrorRegexp = new RegExp ( /^\[ERROR\](.*)$/ , 'gm' ) ; const linesWithErrors = result . cmdOutputIncludingStderr . match ( mvnErrorRegexp ) ; let errorOutput = '' ; if ( linesWithErrors !== null ) { for ( const line of linesWithErrors ) { errorOutput += `${ line . trim ( ) ? line . trim ( ) : '' }\n` ; } } errorOutput = errorOutput . replace ( /^\[ERROR\]/gm , '' ) ; if ( telemetryProperties ) { telemetryProperties . mavenErrors = errorOutput ; } if ( outputChannel ) { outputChannel . show ( ) ; throw new Error ( localize ( 'commandErrorWithOutput' , 'Failed to run "{0}" command. Check output window for more details.' , mvnCommand ) ) ; } } else { if ( outputChannel ) { outputChannel . appendLine ( localize ( 'finishedRunningCommand' , 'Finished running command: "{0} {1}".' , mvnCommand , result . formattedArgs ) ) ; } } return result . cmdOutput ; } export function formatMavenArg ( key , value ) { return `-${ key }=${ cpUtils . wrapArgInQuotes ( value ) }` ; } }