import * as fse from 'fs-extra' ; import * as path from 'path' ; import { IActionContext } from 'vscode-azureextensionui' ; import { pomXmlFileName } from '../../constants' ; import { localize } from '../../localize' ; import { mavenUtils } from '../../utils/mavenUtils' ; import { parseJson } from '../../utils/parseJson' ; import { ITemplates } from '../ITemplates' ; import { parseScriptTemplates } from '../script/parseScriptTemplates' ; import { ScriptTemplateProvider } from '../script/ScriptTemplateProvider' ; import { TemplateType } from '../TemplateProviderBase' ; /**
 * Describes templates output before it has been parsed
 */ interface IRawJavaTemplates { templates : object [ ] ; } /**
 * Java templates largely follow the same formatting as script templates, but they come from maven
 */ export class JavaTemplateProvider extends ScriptTemplateProvider { public templateType : TemplateType = TemplateType . Java ; protected get backupSubpath ( ) : string { return path . join ( 'java' , this . version ) ; } public async getLatestTemplateVersion ( ) { const pomPath = path . join ( this . getProjectPath ( ) , pomXmlFileName ) ; const pomContents = ( await fse . readFile ( pomPath ) ) . toString ( ) ; const match = pomContents . match ( /<azure.functions.maven.plugin.version>(.*)<\/azure.functions.maven.plugin.version>/i ) ; if ( ! match ) { throw new Error ( localize ( 'failedToDetectPluginVersion' , 'Failed to detect Azure Functions maven plugin version.' ) ) ; } else { return match [ 1 ] . trim ( ) ; } } public async getLatestTemplates ( context ) { await mavenUtils . validateMavenInstalled ( context ) ; const projectPath = this . getProjectPath ( ) ; const commandResult = await mavenUtils . executeMvnCommand ( context . telemetry . properties , undefined , projectPath , 'azure-functions:list' ) ; const regExp = />> templates begin <<([\S\s]+)^.+INFO.+ >> templates end <<$[\S\s]+>> bindings begin <<([\S\s]+)^.+INFO.+ >> bindings end <<$[\S\s]+>> resources begin <<([\S\s]+)^.+INFO.+ >> resources end <<$/gm ; const regExpResult = regExp . exec ( commandResult ) ; if ( regExpResult && regExpResult . length > 3 ) { this . _rawTemplates = parseJson < IRawJavaTemplates > ( regExpResult [ 1 ] ) . templates ; this . _rawBindings = parseJson ( regExpResult [ 2 ] ) ; this . _rawResources = parseJson ( regExpResult [ 3 ] ) ; return parseScriptTemplates ( this . _rawResources , this . _rawTemplates , this . _rawBindings ) ; } else { throw new Error ( localize ( 'oldFunctionPlugin' , 'You must update the Azure Functions maven plugin for this functionality.' ) ) ; } } /**
     * Unlike script where templates come from multiple sources (bundle vs non-bundle), java always gets templates from the same source (maven)
     */ public includeTemplate ( ) { return true ; } protected async getCacheKeySuffix ( ) { return 'Java' ; } private getProjectPath ( ) { if ( ! this . projectPath ) { throw new Error ( localize ( 'projectMustBeOpen' , 'You must have a project open to list Java templates.' ) ) ; } else { return this . projectPath ; } } }