diff --git a/examples/src/bld/java/com/example/SampleBuild.java b/examples/src/bld/java/com/example/SampleBuild.java index f4f6603..ab58414 100644 --- a/examples/src/bld/java/com/example/SampleBuild.java +++ b/examples/src/bld/java/com/example/SampleBuild.java @@ -47,6 +47,7 @@ public class SampleBuild extends Project { .fromProject(this) // .projectName("My App") // .classTemplate("my_app_version.txt") +// .classTemplate("version.txt") .execute(); } } diff --git a/examples/src/main/java/com/example/GeneratedVersion.java b/examples/src/main/java/com/example/GeneratedVersion.java index e8dbf5f..7ebcd6b 100644 --- a/examples/src/main/java/com/example/GeneratedVersion.java +++ b/examples/src/main/java/com/example/GeneratedVersion.java @@ -1,30 +1,28 @@ +/* + * This file is automatically generated. + * Do not modify! -- ALL CHANGES WILL BE ERASED! + */ + package com.example; import java.util.Date; -public final class GeneratedVersion implements Comparable { - public static final Date BUILD_DATE = new Date(1720371304587L); +/** + * Provides project version information. + */ +public final class GeneratedVersion { + public static final String PROJECT = "Sample"; + public static final Date BUILD_DATE = new Date(1720742175167L); public static final int MAJOR = 1; public static final int MINOR = 0; - public static final String PROJECT = "My App"; - public static final String QUALIFIER = "rc1"; public static final int REVISION = 1; + public static final String QUALIFIER = "rc1"; public static final String VERSION = "1.0.1-rc1"; + /** + * Disables the default constructor. + */ private GeneratedVersion() { - // no-op + throw new UnsupportedOperationException("Illegal constructor call."); } - - @Override - public int compareTo(GeneratedVersion other) { - if (MAJOR != other.MAJOR) { - return Integer.compare(MAJOR, other.MAJOR); - } else if (MINOR != other.MINOR) { - return Integer.compare(MINOR, other.MINOR); - } else if (REVISION != other.REVISION) { - return Integer.compare(REVISION, other.REVISION); - } else { - return QUALIFIER.compareTo(other.QUALIFIER); - } - } -} \ No newline at end of file +} diff --git a/examples/version.txt b/examples/version.txt new file mode 100644 index 0000000..8d27e1f --- /dev/null +++ b/examples/version.txt @@ -0,0 +1,17 @@ +package {{v packageName/}}; + +import java.util.Date; + +public final class {{v className/}} { + public static final String PROJECT = "{{v project/}}"; + public static final Date BUILD_DATE = new Date({{v epoch/}}L); + public static final int MAJOR = {{v major/}}; + public static final int MINOR = {{v minor/}}; + public static final int REVISION = {{v revision/}}; + public static final String QUALIFIER = "{{v qualifier/}}"; + public static final String VERSION = "{{v version/}}"; + + private {{v className/}}() { + // no-op + } +} \ No newline at end of file diff --git a/src/main/java/rife/bld/extension/GeneratedVersion.java b/src/main/java/rife/bld/extension/GeneratedVersion.java index 3a79c9d..c7f4bc1 100644 --- a/src/main/java/rife/bld/extension/GeneratedVersion.java +++ b/src/main/java/rife/bld/extension/GeneratedVersion.java @@ -17,7 +17,9 @@ package rife.bld.extension; import rife.bld.BaseProject; +import rife.resources.ResourceFinderClasspath; import rife.resources.ResourceFinderDirectories; +import rife.resources.ResourceFinderGroup; import rife.template.Template; import rife.template.TemplateFactory; import rife.tools.FileUtils; @@ -59,8 +61,10 @@ public class GeneratedVersion { public Template buildTemplate() { Template template; var version = project_.version(); + TemplateFactory.TXT.resetClassLoader(); if (template_ == null) { - template = TemplateFactory.TXT.get("default_generated_version"); + var group = new ResourceFinderGroup().add(ResourceFinderClasspath.instance()); + template = TemplateFactory.TXT.setResourceFinder(group).get("default_generated_version"); } else { File parent; if (template_.getParentFile() != null) { @@ -68,8 +72,8 @@ public class GeneratedVersion { } else { parent = new File(template_.getAbsolutePath()).getParentFile(); } - var dirs = new ResourceFinderDirectories(parent); - template = TemplateFactory.TXT.setResourceFinder(dirs).get(template_.getName()); + var group = new ResourceFinderGroup().add(new ResourceFinderDirectories(parent)); + template = TemplateFactory.TXT.setResourceFinder(group).get(template_.getName()); } if (packageName_ == null) { diff --git a/src/test/java/rife/bld/extension/GeneratedVersionTest.java b/src/test/java/rife/bld/extension/GeneratedVersionTest.java index 2c2a35f..7762290 100644 --- a/src/test/java/rife/bld/extension/GeneratedVersionTest.java +++ b/src/test/java/rife/bld/extension/GeneratedVersionTest.java @@ -16,15 +16,18 @@ package rife.bld.extension; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import rife.bld.BaseProject; import rife.bld.Project; +import rife.bld.blueprints.BaseProjectBlueprint; import rife.bld.dependencies.VersionNumber; import rife.tools.FileUtils; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Objects; import java.util.logging.ConsoleHandler; import java.util.logging.Level; @@ -38,7 +41,6 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Erik C. Thauvin * @since 1.0 */ -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class GeneratedVersionTest { private final BaseProject PROJECT = new Project() { @Override @@ -108,7 +110,6 @@ class GeneratedVersionTest { } @Test - @Order(1) void testBuildTemplate() { var gv = new GeneratedVersion(); gv.setProject(PROJECT); @@ -126,6 +127,29 @@ class GeneratedVersionTest { .contains("private GeneratedVersion"); } + @Test + void testExample() throws Exception { + var tmpDir = Files.createTempDirectory("bld-generated-version-example-").toFile(); + tmpDir.deleteOnExit(); + + new GeneratedVersionOperation() + .fromProject(new BaseProjectBlueprint(new File("examples"), "com.example", "Example")) + .directory(tmpDir.getAbsolutePath()) + //.classTemplate(new File("examples", "my_app_version.txt")) + .classTemplate(new File("examples", "version.txt")) + .execute(); + + deleteOnExit(tmpDir); + + var template = Path.of(tmpDir.getAbsolutePath(), "com", "example", "GeneratedVersion.java"); + assertThat(template).exists(); + + var content = Files.readString(template); + assertThat(content).contains("class GeneratedVersion").contains("PROJECT = \"Example\";") + .contains("MAJOR = 0").contains("MINOR = 0").contains("REVISION = 1").contains("QUALIFIER = \"\"") + .doesNotContain("ERASED!"); // only in default template + } + @Test void testExecute() throws Exception { var tmpDir = Files.createTempDirectory("bld-generated-version-execute-").toFile(); @@ -173,7 +197,6 @@ class GeneratedVersionTest { } @Test - @Order(2) void testWriteTemplate() throws IOException { var tmpDir = Files.createTempDirectory("bld-generated-version-write-").toFile(); tmpDir.deleteOnExit();