44
55import java .io .File ;
66import java .util .ArrayList ;
7+ import java .util .List ;
8+ import java .util .Optional ;
79import java .util .UUID ;
810
911import org .apache .velocity .Template ;
1416import org .apache .velocity .runtime .resource .loader .FileResourceLoader ;
1517import org .apache .velocity .util .StringBuilderWriter ;
1618
19+ import io .github .fvarrui .javapackager .packagers .Packager ;
20+
1721/**
1822 * Velocity utils
1923 */
2024public class VelocityUtils {
2125
2226 private static File assetsDir = new File ("assets" );
2327 private static VelocityEngine velocityEngine = null ;
28+ private static List <io .github .fvarrui .javapackager .model .Template > templates ;
2429
2530 private VelocityUtils () {}
31+
32+ public static void init (Packager packager ) {
33+ assetsDir = packager .getAssetsDir ();
34+ templates = packager .getTemplates () != null ? packager .getTemplates () : new ArrayList <>();
35+ // add default template configs
36+ if (templates .stream ().noneMatch (t -> t .getName ().equals ("windows/iss.vtl" ))) {
37+ templates .add (new io .github .fvarrui .javapackager .model .Template ("windows/iss.vtl" , true ));
38+ }
39+ }
2640
2741 private static VelocityEngine getVelocityEngine () {
2842
@@ -58,16 +72,8 @@ private static String render(String templatePath, Object info) throws Exception
5872 template .merge (context , writer );
5973 return writer .toString ();
6074 }
61-
62- public static void setAssetsDir (File assetsDir ) {
63- VelocityUtils .assetsDir = assetsDir ;
64- }
65-
66- public static void render (String templatePath , File output , Object info ) throws Exception {
67- render (templatePath , output , info , false );
68- }
6975
70- public static void render (String templatePath , File output , Object info , boolean includeBom ) throws Exception {
76+ private static void render (String templatePath , File output , Object info , boolean includeBom ) throws Exception {
7177 String data = render (templatePath , info );
7278 data = StringUtils .dosToUnix (data );
7379 if (!includeBom ) {
@@ -76,5 +82,10 @@ public static void render(String templatePath, File output, Object info, boolean
7682 FileUtils .writeStringToFileWithBOM (output , data );
7783 }
7884 }
79-
85+
86+ public static void render (String templatePath , File output , Object info ) throws Exception {
87+ Optional <io .github .fvarrui .javapackager .model .Template > template = templates .stream ().filter (t -> t .getName ().equals (templatePath )).findFirst ();
88+ render (templatePath , output , info , template .isPresent () ? template .get ().isBom () : false );
89+ }
90+
8091}
0 commit comments