Skip to content

Commit

Permalink
Improved template search path, again
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Jul 12, 2024
1 parent 61d723b commit 07c2388
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
1 change: 1 addition & 0 deletions examples/src/bld/java/com/example/SampleBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void genver() throws Exception {
.fromProject(this)
// .projectName("My App")
// .classTemplate("my_app_version.txt")
// .classTemplate("version.txt")
.execute();
}
}
36 changes: 17 additions & 19 deletions examples/src/main/java/com/example/GeneratedVersion.java
Original file line number Diff line number Diff line change
@@ -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
}

@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);
}
throw new UnsupportedOperationException("Illegal constructor call.");
}
}
}
17 changes: 17 additions & 0 deletions examples/version.txt
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 7 additions & 3 deletions src/main/java/rife/bld/extension/GeneratedVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,17 +61,19 @@ 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) {
parent = template_.getParentFile();
} 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) {
Expand Down
31 changes: 27 additions & 4 deletions src/test/java/rife/bld/extension/GeneratedVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +41,6 @@
* @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
Expand Down Expand Up @@ -108,7 +110,6 @@ private MyVersion() {
}

@Test
@Order(1)
void testBuildTemplate() {
var gv = new GeneratedVersion();
gv.setProject(PROJECT);
Expand All @@ -126,6 +127,29 @@ void testBuildTemplate() {
.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();
Expand Down Expand Up @@ -173,7 +197,6 @@ void testGeneratedVersion() {
}

@Test
@Order(2)
void testWriteTemplate() throws IOException {
var tmpDir = Files.createTempDirectory("bld-generated-version-write-").toFile();
tmpDir.deleteOnExit();
Expand Down

0 comments on commit 07c2388

Please sign in to comment.