Improved template search path, again

This commit is contained in:
Erik C. Thauvin 2024-07-11 16:25:54 -07:00
parent 61d723b589
commit 07c23883c1
Signed by: erik
GPG key ID: 776702A6A2DA330E
5 changed files with 69 additions and 26 deletions

View file

@ -47,6 +47,7 @@ public class SampleBuild extends Project {
.fromProject(this)
// .projectName("My App")
// .classTemplate("my_app_version.txt")
// .classTemplate("version.txt")
.execute();
}
}

View file

@ -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<GeneratedVersion> {
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);
}
}
}
}

17
examples/version.txt Normal file
View file

@ -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
}
}

View file

@ -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) {

View file

@ -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 <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @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();