import * as fse from 'fs-extra' ; import * as path from 'path' ; import { DebugConfiguration , TaskDefinition , window } from 'vscode' ; import { buildGradleFileName , func , hostStartCommand , javaBuildTool , JavaBuildTool , pomXmlFileName , ProjectLanguage } from '../../../constants' ; import { javaDebugConfig } from '../../../debug/JavaDebugProvider' ; import { localize } from "../../../localize" ; import { mavenUtils } from '../../../utils/mavenUtils' ; import { nonNullProp } from '../../../utils/nonNull' ; import { getFuncWatchProblemMatcher } from '../../../vsCodeConfig/settings' ; import { convertToFunctionsTaskLabel } from '../../../vsCodeConfig/tasks' ; import { IJavaProjectWizardContext } from '../../createNewProject/javaSteps/IJavaProjectWizardContext' ; import { InitVSCodeStepBase } from './InitVSCodeStepBase' ; const javaPackageTaskLabel = convertToFunctionsTaskLabel ( 'package' ) ; export class JavaInitVSCodeStep extends InitVSCodeStepBase { protected preDeployTask : string = javaPackageTaskLabel ; private _debugSubpath : string ; private _buildTool : JavaBuildTool ; protected async executeCore ( context ) { this . _buildTool = nonNullProp ( context , 'buildTool' ) ; this . settings . push ( { key : javaBuildTool , value : this . _buildTool } ) ; const functionAppName = await getFunctionAppName ( context . projectPath , this . _buildTool ) ; if ( ! functionAppName ) { this . _debugSubpath = '<function_build_path>' ; void window . showWarningMessage ( localize ( 'functionAppNameNotFound' , 'Cannot parse the Azure Functions name, you may need to specify it in the tasks.json.' ) ) ; } else { this . _debugSubpath = getJavaDebugSubpath ( functionAppName , this . _buildTool ) ; } this . setDeploySubpath ( context , this . _debugSubpath ) ; } protected getTasks ( language ) { return [ { type : func , command : hostStartCommand , problemMatcher : getFuncWatchProblemMatcher ( language ) , isBackground : true , options : { cwd : this . _debugSubpath } , dependsOn : javaPackageTaskLabel } , { label : javaPackageTaskLabel , command : getPackageCommand ( this . _buildTool ) , type : 'shell' , group : { kind : 'build' , isDefault : true } } ] ; } protected getDebugConfiguration ( ) { return javaDebugConfig ; } protected getRecommendedExtensions ( ) { return [ 'vscjava.vscode-java-debug' ] ; } } export async function getFunctionAppName ( projectPath , buildTool ) { switch ( buildTool ) { case JavaBuildTool . maven : const pomXmlPath = path . join ( projectPath , pomXmlFileName ) ; return mavenUtils . getFunctionAppNameInPom ( pomXmlPath ) ; case JavaBuildTool . gradle : const buildGradlePath = path . join ( projectPath , buildGradleFileName ) ; const buildGradle = ( await fse . readFile ( buildGradlePath ) ) . toString ( ) ; const match = /appName\s*?=\s*?['|"](.+?)['|"]/g . exec ( buildGradle ) ; return match ? match [ 1 ] : undefined ; default : throw new Error ( localize ( 'invalidJavaBuildTool' , 'Invalid java build tool {0}.' , buildTool ) ) ; } } export function getJavaDebugSubpath ( functionAppName , buildTool ) { switch ( buildTool ) { case JavaBuildTool . maven : return path . posix . join ( 'target' , 'azure-functions' , functionAppName ) ; case JavaBuildTool . gradle : return path . posix . join ( 'build' , 'azure-functions' , functionAppName ) ; default : throw new Error ( localize ( 'invalidJavaBuildTool' , 'Invalid java build tool {0}.' , buildTool ) ) ; } } function getPackageCommand ( buildTool ) { switch ( buildTool ) { case JavaBuildTool . maven : return "mvn clean package" ; case JavaBuildTool . gradle : return "gradle azureFunctionsPackage" ; default : throw new Error ( localize ( 'invalidJavaBuildTool' , 'Invalid java build tool {0}.' , buildTool ) ) ; } }