Skip to content

Commit 9c3de46

Browse files
committed
add template configs support (default template for iss.vtl)
1 parent 5205786 commit 9c3de46

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

src/main/java/io/github/fvarrui/javapackager/model/Template.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
public class Template {
44

55
private String name;
6-
private boolean bom = true;
6+
private boolean bom = false;
7+
8+
public Template() {}
9+
10+
public Template(String name, boolean bom) {
11+
this.name = name;
12+
this.bom = bom;
13+
}
714

815
public String getName() {
916
return name;

src/main/java/io/github/fvarrui/javapackager/packagers/GenerateSetup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected File doApply(WindowsPackager packager) throws Exception {
5656

5757
// generates iss file from velocity template
5858
File issFile = new File(assetsFolder, name + ".iss");
59-
VelocityUtils.render("windows/iss.vtl", issFile, packager, true);
59+
VelocityUtils.render("windows/iss.vtl", issFile, packager);
6060

6161
// generates windows installer with inno setup command line compiler
6262
CommandUtils.execute("iscc", "/O" + outputDirectory.getAbsolutePath(), "/F" + name + "_" + version, issFile);

src/main/java/io/github/fvarrui/javapackager/packagers/Packager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ private void init() throws Exception {
101101
throw new Exception("'mainClass' cannot be null");
102102
}
103103

104-
// sets assetsDir for velocity to locate custom velocity templates
105-
VelocityUtils.setAssetsDir(assetsDir);
104+
// init velocity utils
105+
VelocityUtils.init(this);
106106

107107
// using name as displayName, if it's not specified
108108
displayName = defaultIfBlank(displayName, name);

src/main/java/io/github/fvarrui/javapackager/utils/VelocityUtils.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.io.File;
66
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Optional;
79
import java.util.UUID;
810

911
import org.apache.velocity.Template;
@@ -14,15 +16,27 @@
1416
import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
1517
import org.apache.velocity.util.StringBuilderWriter;
1618

19+
import io.github.fvarrui.javapackager.packagers.Packager;
20+
1721
/**
1822
* Velocity utils
1923
*/
2024
public 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

Comments
 (0)