@Test public void testGenerate_customDirectory_byApp() throws Exception { File targetFilePattern = new File( this.folder.newFolder(), "${app}/${app}.paf" ); String templateContent; InputStream in = getClass().getResourceAsStream( "/templates/basic-with-custom-output.txt.tpl" ); try { ByteArrayOutputStream os = new ByteArrayOutputStream(); Utils.copyStreamUnsafelyUseWithCaution( in, os ); templateContent = os.toString( "UTF-8" ).replace( "%DIR%", targetFilePattern.getAbsolutePath()); } finally { Utils.closeQuietly( in ); } File dir = this.folder.newFolder(); File tplFile = new File( dir, "basic-with-custom-output.txt.tpl" ); Utils.writeStringInto( templateContent, tplFile ); TemplateEntry te = new TemplateWatcher( null, dir, 100 ).compileTemplate( tplFile ); Assert.assertNotNull( te ); Assert.assertNull( te.getAppName()); Assert.assertEquals( tplFile, te.getTemplateFile()); Assert.assertEquals( targetFilePattern.getAbsolutePath(), te.getTargetFilePath()); Assert.assertNotNull( te.getTemplate()); Application app = GenerationTest.testApplicationForTemplates(); File targetFile = new File( targetFilePattern.getParentFile().getParentFile(), app.getName() + "/" + app.getName() + ".paf" ); Assert.assertFalse( targetFilePattern.exists()); Assert.assertFalse( targetFile.exists()); Logger logger = Logger.getLogger( getClass().getName()); File outputDir = this.folder.newFolder(); Assert.assertEquals( 0, outputDir.listFiles().length ); TemplateUtils.generate( app, outputDir, Collections.singleton( te ), logger ); Assert.assertEquals( 0, outputDir.listFiles().length ); Assert.assertFalse( targetFilePattern.exists()); Assert.assertTrue( targetFile.exists()); String currentContent = Utils.readFileContent( targetFile ); File expectedFile = TestUtils.findTestFile( "/output/basic-with-custom-output.txt" ); String expectedContent = Utils.readFileContent( expectedFile ); Assert.assertEquals( expectedContent, currentContent ); }
public static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ) throws IOException { ApplicationContextBean appCtx = ContextUtils.toContext( app ); Context wrappingCtx = Context .newBuilder( appCtx ) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE, new ComponentPathResolver() ).build(); try { for( TemplateEntry template : templates ) { logger.fine( "Processing template " + template.getTemplateFile() + " to application " + app + "." ); File target; String targetFilePath = template.getTargetFilePath(); if( ! Utils.isEmptyOrWhitespaces( targetFilePath )) { target = new File( targetFilePath.replace( "${app}", app.getName())); } else { String filename = template.getTemplateFile().getName().replaceFirst( "\\.tpl$", "" ); target = new File( outputDirectory, app.getName() + "/" + filename ); } Utils.createDirectory( target.getParentFile()); String output = template.getTemplate().apply( wrappingCtx ); Utils.writeStringInto( output, target ); logger.fine( "Template " + template.getTemplateFile() + " was processed with application " + app + ". Output is in " + target ); } } finally { wrappingCtx.destroy(); } }
TemplateUtils { public static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ) throws IOException { ApplicationContextBean appCtx = ContextUtils.toContext( app ); Context wrappingCtx = Context .newBuilder( appCtx ) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE, new ComponentPathResolver() ).build(); try { for( TemplateEntry template : templates ) { logger.fine( "Processing template " + template.getTemplateFile() + " to application " + app + "." ); File target; String targetFilePath = template.getTargetFilePath(); if( ! Utils.isEmptyOrWhitespaces( targetFilePath )) { target = new File( targetFilePath.replace( "${app}", app.getName())); } else { String filename = template.getTemplateFile().getName().replaceFirst( "\\.tpl$", "" ); target = new File( outputDirectory, app.getName() + "/" + filename ); } Utils.createDirectory( target.getParentFile()); String output = template.getTemplate().apply( wrappingCtx ); Utils.writeStringInto( output, target ); logger.fine( "Template " + template.getTemplateFile() + " was processed with application " + app + ". Output is in " + target ); } } finally { wrappingCtx.destroy(); } } }
TemplateUtils { public static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ) throws IOException { ApplicationContextBean appCtx = ContextUtils.toContext( app ); Context wrappingCtx = Context .newBuilder( appCtx ) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE, new ComponentPathResolver() ).build(); try { for( TemplateEntry template : templates ) { logger.fine( "Processing template " + template.getTemplateFile() + " to application " + app + "." ); File target; String targetFilePath = template.getTargetFilePath(); if( ! Utils.isEmptyOrWhitespaces( targetFilePath )) { target = new File( targetFilePath.replace( "${app}", app.getName())); } else { String filename = template.getTemplateFile().getName().replaceFirst( "\\.tpl$", "" ); target = new File( outputDirectory, app.getName() + "/" + filename ); } Utils.createDirectory( target.getParentFile()); String output = template.getTemplate().apply( wrappingCtx ); Utils.writeStringInto( output, target ); logger.fine( "Template " + template.getTemplateFile() + " was processed with application " + app + ". Output is in " + target ); } } finally { wrappingCtx.destroy(); } } private  TemplateUtils(); }
TemplateUtils { public static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ) throws IOException { ApplicationContextBean appCtx = ContextUtils.toContext( app ); Context wrappingCtx = Context .newBuilder( appCtx ) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE, new ComponentPathResolver() ).build(); try { for( TemplateEntry template : templates ) { logger.fine( "Processing template " + template.getTemplateFile() + " to application " + app + "." ); File target; String targetFilePath = template.getTargetFilePath(); if( ! Utils.isEmptyOrWhitespaces( targetFilePath )) { target = new File( targetFilePath.replace( "${app}", app.getName())); } else { String filename = template.getTemplateFile().getName().replaceFirst( "\\.tpl$", "" ); target = new File( outputDirectory, app.getName() + "/" + filename ); } Utils.createDirectory( target.getParentFile()); String output = template.getTemplate().apply( wrappingCtx ); Utils.writeStringInto( output, target ); logger.fine( "Template " + template.getTemplateFile() + " was processed with application " + app + ". Output is in " + target ); } } finally { wrappingCtx.destroy(); } } private  TemplateUtils(); static String findApplicationName( final File templateDir, final File templateFile ); static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ); static void deleteGeneratedFiles( Application app, File outputDirectory ); static Collection<TemplateEntry> findTemplatesForApplication( String appName, Collection<TemplateEntry> templates ); }
TemplateUtils { public static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ) throws IOException { ApplicationContextBean appCtx = ContextUtils.toContext( app ); Context wrappingCtx = Context .newBuilder( appCtx ) .resolver( MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, MethodValueResolver.INSTANCE, new ComponentPathResolver() ).build(); try { for( TemplateEntry template : templates ) { logger.fine( "Processing template " + template.getTemplateFile() + " to application " + app + "." ); File target; String targetFilePath = template.getTargetFilePath(); if( ! Utils.isEmptyOrWhitespaces( targetFilePath )) { target = new File( targetFilePath.replace( "${app}", app.getName())); } else { String filename = template.getTemplateFile().getName().replaceFirst( "\\.tpl$", "" ); target = new File( outputDirectory, app.getName() + "/" + filename ); } Utils.createDirectory( target.getParentFile()); String output = template.getTemplate().apply( wrappingCtx ); Utils.writeStringInto( output, target ); logger.fine( "Template " + template.getTemplateFile() + " was processed with application " + app + ". Output is in " + target ); } } finally { wrappingCtx.destroy(); } } private  TemplateUtils(); static String findApplicationName( final File templateDir, final File templateFile ); static void generate( Application app, File outputDirectory, Collection<TemplateEntry> templates, Logger logger ); static void deleteGeneratedFiles( Application app, File outputDirectory ); static Collection<TemplateEntry> findTemplatesForApplication( String appName, Collection<TemplateEntry> templates );  }