import * as fse from 'fs-extra' ; import { nonNullProp } from '../../../utils/nonNull' ; import { getJavaFunctionFilePath , IJavaProjectWizardContext } from '../../createNewProject/javaSteps/IJavaProjectWizardContext' ; import { FunctionCreateStepBase } from '../FunctionCreateStepBase' ; import { getBindingSetting , IFunctionWizardContext } from '../IFunctionWizardContext' ; import { IJavaFunctionTemplate , IJavaFunctionWizardContext } from './IJavaFunctionWizardContext' ; export class JavaFunctionCreateStep extends FunctionCreateStepBase < IFunctionWizardContext & IJavaProjectWizardContext > { public async executeCore ( context ) { const template = nonNullProp ( context , 'functionTemplate' ) ; const packageName = nonNullProp ( context , 'javaPackageName' ) ; const functionName = nonNullProp ( context , 'functionName' ) ; const args = new Map < string , string > ( ) ; for ( const setting of template . userPromptedSettings ) { const value = getBindingSetting ( context , setting ) ; if ( value !== undefined ) { const fixedValue = setting . valueType === 'enum' ? String ( value ) . toUpperCase ( ) : String ( value ) ; args . set ( setting . name , fixedValue ) ; } } args . set ( "packageName" , packageName ) ; args . set ( "functionName" , functionName ) ; args . set ( "className" , functionName . replace ( '-' , '_' ) ) ; const content = substituteParametersInTemplate ( template , args ) ; const path = getJavaFunctionFilePath ( context . projectPath , packageName , functionName ) ; await fse . ensureFile ( path ) ; await fse . writeFile ( path , content ) ; return getJavaFunctionFilePath ( context . projectPath , packageName , functionName ) ; } } function substituteParametersInTemplate ( template , args ) { let javaTemplate = template . templateFiles [ "function.java" ] ; args . forEach ( ( value , key ) => { javaTemplate = javaTemplate . replace ( new RegExp ( `\\$${ key }\\$` , 'g' ) , value ) ; } ) ; return javaTemplate ; }