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"?>
<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">
<option name="skipTestSources" value="false" />
</component>

View file

@ -78,9 +78,20 @@ public class VersionProcessor extends AbstractProcessor {
return template;
}
private Mustache compileTemplate(final File dir, final String template) {
final var mf = new DefaultMustacheFactory(dir);
return mf.compile(template);
private Mustache compileTemplate(final File dir, final String template) throws IOException {
if (Constants.DEFAULT_JAVA_TEMPLATE.equals(template) || Constants.DEFAULT_KOTLIN_TEMPLATE.equals(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) {
@ -237,7 +248,7 @@ public class VersionProcessor extends AbstractProcessor {
private void writeTemplate(final String type, final VersionInfo versionInfo, final String template)
throws IOException {
final var dir = getLocalFile("");
final var mustache = compileTemplate(dir, template);
final Mustache mustache = compileTemplate(dir, template);
final var templateName = switch (mustache.getName()) {
case Constants.DEFAULT_JAVA_TEMPLATE -> "default (Java)";

View file

@ -74,11 +74,11 @@ class VersionProcessorTest {
* This file is automatically generated.
* Do not modify! -- ALL CHANGES WILL BE ERASED!
*/
package %s;
import java.util.Date;
/**
* Provides semantic version information.
*
@ -96,7 +96,7 @@ class VersionProcessorTest {
public static final String BUILDMETA_PREFIX = "%s";
public static final String SEPARATOR = "%s";
public static final String VERSION = "";
/**
* Disables the default constructor.
*/