Load template resources from JAR manually. Close #8

This commit is contained in:
Erik C. Thauvin 2024-04-29 18:32:27 -07:00
parent d11420dae3
commit 47ffb6e12b
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 26 additions and 8 deletions

7
.idea/misc.xml generated
View file

@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<pattern value="net.thauvin.erik.semver.SemverBuild" method="jacoco" />
<pattern value="net.thauvin.erik.semver.SemverBuild" method="pandoc" />
<pattern value="net.thauvin.erik.semver.SemverBuild" method="pmd" />
<pattern value="net.thauvin.erik.semver.SemverBuild" method="pmdCli" />
<pattern value="net.thauvin.erik.semver.SemverBuild" />
</component>
<component name="PDMPlugin"> <component name="PDMPlugin">
<option name="skipTestSources" value="false" /> <option name="skipTestSources" value="false" />
</component> </component>

View file

@ -78,9 +78,20 @@ public class VersionProcessor extends AbstractProcessor {
return template; return template;
} }
private Mustache compileTemplate(final File dir, final String template) { private Mustache compileTemplate(final File dir, final String template) throws IOException {
final var mf = new DefaultMustacheFactory(dir); if (Constants.DEFAULT_JAVA_TEMPLATE.equals(template) || Constants.DEFAULT_KOTLIN_TEMPLATE.equals(template)) {
return mf.compile(template); try (var in = getClass().getResourceAsStream("/" + template)) {
if (in != null) {
try (var reader = new BufferedReader(new InputStreamReader(in))) {
return new DefaultMustacheFactory().compile(reader, template);
}
} else {
throw new IOException("Resource not found: " + template);
}
}
} else {
return new DefaultMustacheFactory(dir).compile(template);
}
} }
private void error(final String s) { private void error(final String s) {
@ -237,7 +248,7 @@ public class VersionProcessor extends AbstractProcessor {
private void writeTemplate(final String type, final VersionInfo versionInfo, final String template) private void writeTemplate(final String type, final VersionInfo versionInfo, final String template)
throws IOException { throws IOException {
final var dir = getLocalFile(""); final var dir = getLocalFile("");
final var mustache = compileTemplate(dir, template); final Mustache mustache = compileTemplate(dir, template);
final var templateName = switch (mustache.getName()) { final var templateName = switch (mustache.getName()) {
case Constants.DEFAULT_JAVA_TEMPLATE -> "default (Java)"; case Constants.DEFAULT_JAVA_TEMPLATE -> "default (Java)";