Moved template generation methods to GeneratedVersion class

This commit is contained in:
Erik C. Thauvin 2024-07-03 16:23:08 -07:00
parent 4b00dd801a
commit 3e564915ea
Signed by: erik
GPG key ID: 776702A6A2DA330E
4 changed files with 182 additions and 176 deletions

View file

@ -17,8 +17,16 @@
package rife.bld.extension; package rife.bld.extension;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.resources.ResourceFinderDirectories;
import rife.template.Template;
import rife.template.TemplateConfig;
import rife.template.TemplateFactory;
import rife.tools.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;
/** /**
* GeneratedVersion data class. * GeneratedVersion data class.
@ -26,16 +34,86 @@ import java.io.File;
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0 * @since 1.0
*/ */
@SuppressWarnings("PMD.DataClass")
public class GeneratedVersion { public class GeneratedVersion {
private File classFile; private static final String CLASSNAME = "className";
private String className; private static final String EPOCH = "epoch";
private File directory; private static final String MAJOR = "major";
private String extension = ".java"; private static final String MINOR = "minor";
private String packageName; private static final String PACKAGE_NAME = "packageName";
private BaseProject project; private static final String PROJECT = "project";
private String projectName; private static final String QUALIFIER = "qualifier";
private File template; private static final String REVISION = "revision";
private static final String VERSION = "version";
private File classFile_;
private String className_;
private File directory_;
private String extension_ = ".java";
private String packageName_;
private String projectName_;
private BaseProject project_;
private File template_;
/**
* Builds the template based on the {@link GeneratedVersion} data.
*
* @return the template
*/
public Template buildTemplate() {
Template template;
var version = project_.version();
if (template_ == null) {
template = TemplateFactory.TXT.get("version.txt");
} else {
var files = new ResourceFinderDirectories(template_.getParentFile());
template = new TemplateFactory(TemplateConfig.TXT, "txtFiles", TemplateFactory.TXT)
.setResourceFinder(files).get(template_.getName());
}
if (packageName_ == null) {
packageName_ = project_.pkg();
}
if (template.hasValueId(PACKAGE_NAME)) {
template.setValue(PACKAGE_NAME, packageName_);
}
if (template.hasValueId(CLASSNAME)) {
template.setValue(CLASSNAME, Objects.requireNonNullElse(className_, "GeneratedVersion"));
}
if (template.hasValueId(PROJECT)) {
if (projectName_ == null) {
projectName_ = project_.name();
}
template.setValue(PROJECT, projectName_);
}
if (template.hasValueId(EPOCH)) {
template.setValue(EPOCH, System.currentTimeMillis());
}
if (template.hasValueId(VERSION)) {
template.setValue(VERSION, version.toString());
}
if (template.hasValueId(MAJOR)) {
template.setValue(MAJOR, version.majorInt());
}
if (template.hasValueId(MINOR)) {
template.setValue(MINOR, version.minorInt());
}
if (template.hasValueId(REVISION)) {
template.setValue(REVISION, version.revisionInt());
}
if (template.hasValueId(QUALIFIER)) {
template.setValue(QUALIFIER, version.qualifier());
}
return template;
}
/** /**
* Returns the class file. * Returns the class file.
@ -43,7 +121,7 @@ public class GeneratedVersion {
* @return the class file * @return the class file
*/ */
public File getClassFile() { public File getClassFile() {
return classFile; return classFile_;
} }
/** /**
@ -52,7 +130,7 @@ public class GeneratedVersion {
* @return the class name * @return the class name
*/ */
public String getClassName() { public String getClassName() {
return className; return className_;
} }
/** /**
@ -61,7 +139,7 @@ public class GeneratedVersion {
* @return the destination directory * @return the destination directory
*/ */
public File getDirectory() { public File getDirectory() {
return directory; return directory_;
} }
/** /**
@ -70,7 +148,7 @@ public class GeneratedVersion {
* @return the file extension * @return the file extension
*/ */
public String getExtension() { public String getExtension() {
return extension; return extension_;
} }
/** /**
@ -79,7 +157,7 @@ public class GeneratedVersion {
* @return the package name * @return the package name
*/ */
public String getPackageName() { public String getPackageName() {
return packageName; return packageName_;
} }
/** /**
@ -88,7 +166,7 @@ public class GeneratedVersion {
* @return the project * @return the project
*/ */
public BaseProject getProject() { public BaseProject getProject() {
return project; return project_;
} }
/** /**
@ -97,7 +175,7 @@ public class GeneratedVersion {
* @return the project name * @return the project name
*/ */
public String getProjectName() { public String getProjectName() {
return projectName; return projectName_;
} }
/** /**
@ -106,16 +184,7 @@ public class GeneratedVersion {
* @return the template * @return the template
*/ */
public File getTemplate() { public File getTemplate() {
return template; return template_;
}
/**
* Sets the class file.
*
* @param classFile the class file
*/
public void setClassFile(File classFile) {
this.classFile = classFile;
} }
/** /**
@ -124,7 +193,7 @@ public class GeneratedVersion {
* @param className the class name * @param className the class name
*/ */
public void setClassName(String className) { public void setClassName(String className) {
this.className = className; this.className_ = className;
} }
/** /**
@ -133,7 +202,7 @@ public class GeneratedVersion {
* @param directory the destination directory * @param directory the destination directory
*/ */
public void setDirectory(File directory) { public void setDirectory(File directory) {
this.directory = directory; this.directory_ = directory;
} }
/** /**
@ -142,7 +211,7 @@ public class GeneratedVersion {
* @param extension the file extension * @param extension the file extension
*/ */
public void setExtension(String extension) { public void setExtension(String extension) {
this.extension = extension; this.extension_ = extension;
} }
/** /**
@ -151,7 +220,7 @@ public class GeneratedVersion {
* @param packageName the package name * @param packageName the package name
*/ */
public void setPackageName(String packageName) { public void setPackageName(String packageName) {
this.packageName = packageName; this.packageName_ = packageName;
} }
/** /**
@ -160,7 +229,7 @@ public class GeneratedVersion {
* @param project the project * @param project the project
*/ */
public void setProject(BaseProject project) { public void setProject(BaseProject project) {
this.project = project; this.project_ = project;
} }
/** /**
@ -169,7 +238,7 @@ public class GeneratedVersion {
* @param projectName the project name * @param projectName the project name
*/ */
public void setProjectName(String projectName) { public void setProjectName(String projectName) {
this.projectName = projectName; this.projectName_ = projectName;
} }
/** /**
@ -178,6 +247,31 @@ public class GeneratedVersion {
* @param template the template * @param template the template
*/ */
public void setTemplate(File template) { public void setTemplate(File template) {
this.template = template; this.template_ = template;
}
/**
* Writes the project version class in the given directory.
*/
public void writeTemplate(Template template) throws IOException {
if (packageName_ != null) {
classFile_ = Path.of(directory_.getAbsolutePath(), packageName_.replace(".", File.separator),
className_ + extension_).toFile();
} else {
classFile_ = new File(directory_, className_ + ".java");
}
if (!classFile_.getParentFile().exists()) {
var dirs = classFile_.getParentFile().mkdirs();
if (!dirs && !classFile_.getParentFile().exists()) {
throw new IOException("Could not create project package directories: " + classFile_.getParent());
}
}
try {
FileUtils.writeString(template.getContent(), classFile_);
} catch (IOException e) {
throw new IOException("Unable to write the version class file: " + e.getMessage(), e);
}
} }
} }

View file

@ -19,16 +19,9 @@ package rife.bld.extension;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.operations.AbstractOperation; import rife.bld.operations.AbstractOperation;
import rife.resources.ResourceFinderDirectories;
import rife.template.Template;
import rife.template.TemplateConfig;
import rife.template.TemplateFactory;
import rife.tools.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -39,119 +32,8 @@ import java.util.logging.Logger;
* @since 1.0 * @since 1.0
*/ */
public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersionOperation> { public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersionOperation> {
private static final String CLASSNAME = "className";
private static final String EPOCH = "epoch";
private static final Logger LOGGER = Logger.getLogger(GeneratedVersionOperation.class.getName()); private static final Logger LOGGER = Logger.getLogger(GeneratedVersionOperation.class.getName());
private static final String MAJOR = "major"; private final GeneratedVersion generatedVersion_ = new GeneratedVersion();
private static final String MINOR = "minor";
private static final String PACKAGE_NAME = "packageName";
private static final String PROJECT = "project";
private static final String QUALIFIER = "qualifier";
private static final String REVISION = "revision";
private static final String VERSION = "version";
private final GeneratedVersion generatedVersion = new GeneratedVersion();
/**
* Builds the template based on the {@link GeneratedVersion} data.
*
* @param gv the generated version
* @return the template
*/
public static Template buildTemplate(GeneratedVersion gv) {
Template template;
var version = gv.getProject().version();
if (gv.getTemplate() == null) {
template = TemplateFactory.TXT.get("version.txt");
} else {
var files = new ResourceFinderDirectories(gv.getTemplate().getParentFile());
template = new TemplateFactory(TemplateConfig.TXT, "txtFiles", TemplateFactory.TXT)
.setResourceFinder(files).get(gv.getTemplate().getName());
}
if (gv.getPackageName() == null) {
gv.setPackageName(gv.getProject().pkg());
}
if (template.hasValueId(PACKAGE_NAME)) {
template.setValue(PACKAGE_NAME, gv.getPackageName());
}
gv.setClassName(Objects.requireNonNullElse(gv.getClassName(), "GeneratedVersion"));
if (template.hasValueId(CLASSNAME)) {
template.setValue(CLASSNAME, gv.getClassName());
}
if (template.hasValueId(PROJECT)) {
if (gv.getProjectName() == null) {
gv.setProjectName(gv.getProject().name());
}
template.setValue(PROJECT, gv.getProjectName());
}
if (template.hasValueId(EPOCH)) {
template.setValue(EPOCH, System.currentTimeMillis());
}
if (template.hasValueId(VERSION)) {
template.setValue(VERSION, version.toString());
}
if (template.hasValueId(MAJOR)) {
template.setValue(MAJOR, version.majorInt());
}
if (template.hasValueId(MINOR)) {
template.setValue(MINOR, version.minorInt());
}
if (template.hasValueId(REVISION)) {
template.setValue(REVISION, version.revisionInt());
}
if (template.hasValueId(QUALIFIER)) {
template.setValue(QUALIFIER, version.qualifier());
}
return template;
}
/**
* Writes the project version class in the given directory.
*
* @param template the template
* @param gv the generated version
*/
public static void writeTemplate(Template template, GeneratedVersion gv) {
if (gv.getPackageName() != null) {
gv.setClassFile(Path.of(gv.getDirectory().getAbsolutePath(),
gv.getPackageName().replace(".", File.separator), gv.getClassName()
+ gv.getExtension()).toFile());
} else {
gv.setClassFile(Path.of(gv.getDirectory().getAbsolutePath(), gv.getClassName() + ".java").toFile());
}
if (!gv.getClassFile().getParentFile().exists()) {
var mkdirs = gv.getClassFile().getParentFile().mkdirs();
if (!mkdirs && !gv.getClassFile().getParentFile().exists() && LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "Could not create project package directories: {0}",
gv.getClassFile().getParent());
}
}
try {
var updated = gv.getClassFile().exists();
FileUtils.writeString(template.getContent(), gv.getClassFile());
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "Generated version ({0}) class has been {1}: {2}",
new String[]{gv.getProject().version().toString(), updated ? "updated" : "created",
"file://" + gv.getClassFile().toURI().getPath()});
}
} catch (IOException e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "Unable to write the version class file.", e);
}
}
}
/** /**
* Sets the class name. * Sets the class name.
@ -160,7 +42,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation className(String className) { public GeneratedVersionOperation className(String className) {
generatedVersion.setClassName(className); generatedVersion_.setClassName(className);
return this; return this;
} }
@ -171,7 +53,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation classTemplate(File template) { public GeneratedVersionOperation classTemplate(File template) {
generatedVersion.setTemplate(template); generatedVersion_.setTemplate(template);
return this; return this;
} }
@ -192,7 +74,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation directory(File directory) { public GeneratedVersionOperation directory(File directory) {
generatedVersion.setDirectory(directory); generatedVersion_.setDirectory(directory);
return this; return this;
} }
@ -214,9 +96,6 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
if (generatedVersion.getProject() == null && LOGGER.isLoggable(Level.SEVERE)) { if (generatedVersion.getProject() == null && LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.severe("A project must be specified."); LOGGER.severe("A project must be specified.");
} }
var template = buildTemplate(generatedVersion);
writeTemplate(template, generatedVersion);
} }
/** /**
@ -226,7 +105,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation extension(String extension) { public GeneratedVersionOperation extension(String extension) {
generatedVersion.setExtension(extension); generatedVersion_.setExtension(extension);
return this; return this;
} }
@ -237,8 +116,8 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation fromProject(BaseProject project) { public GeneratedVersionOperation fromProject(BaseProject project) {
generatedVersion.setProject(project); generatedVersion_.setProject(project);
generatedVersion.setDirectory(project.srcMainJavaDirectory()); generatedVersion_.setDirectory(project.srcMainJavaDirectory());
return this; return this;
} }
@ -249,7 +128,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation packageName(String packageName) { public GeneratedVersionOperation packageName(String packageName) {
generatedVersion.setPackageName(packageName); generatedVersion_.setPackageName(packageName);
return this; return this;
} }
@ -260,7 +139,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
* @return this operation instance * @return this operation instance
*/ */
public GeneratedVersionOperation projectName(String projectName) { public GeneratedVersionOperation projectName(String projectName) {
generatedVersion.setProjectName(projectName); generatedVersion_.setProjectName(projectName);
return this; return this;
} }
} }

View file

@ -16,6 +16,7 @@
package rife.bld.extension; package rife.bld.extension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.Project; import rife.bld.Project;
@ -26,6 +27,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Objects; import java.util.Objects;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -53,6 +57,17 @@ class GeneratedVersionTest {
} }
}; };
@BeforeAll
static void beforeAll() {
var level = Level.ALL;
var logger = Logger.getLogger("rife.bld.extension");
var consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(level);
logger.addHandler(consoleHandler);
logger.setLevel(level);
logger.setUseParentHandlers(false);
}
static void deleteOnExit(File folder) { static void deleteOnExit(File folder) {
folder.deleteOnExit(); folder.deleteOnExit();
for (var f : Objects.requireNonNull(folder.listFiles())) { for (var f : Objects.requireNonNull(folder.listFiles())) {
@ -68,13 +83,12 @@ class GeneratedVersionTest {
void testBuildCustomTemplate() { void testBuildCustomTemplate() {
var gv = new GeneratedVersion(); var gv = new GeneratedVersion();
gv.setProject(PROJECT); gv.setProject(PROJECT);
gv.setTemplate(new File(gv.getProject().srcTestResourcesDirectory().getAbsolutePath(), gv.setTemplate(new File(gv.getProject().srcTestResourcesDirectory().getAbsolutePath(), "version_test.txt"));
"version_test.txt"));
gv.setPackageName("com.example.my"); gv.setPackageName("com.example.my");
gv.setProjectName("My App"); gv.setProjectName("My App");
gv.setClassName("MyVersion"); gv.setClassName("MyVersion");
var t = GeneratedVersionOperation.buildTemplate(gv); var t = gv.buildTemplate();
//noinspection TrailingWhitespacesInTextBlock //noinspection TrailingWhitespacesInTextBlock
assertThat(t.getContent()).isEqualTo(""" assertThat(t.getContent()).isEqualTo("""
package com.example.my; package com.example.my;
@ -97,8 +111,8 @@ class GeneratedVersionTest {
void testBuildTemplate() { void testBuildTemplate() {
var gv = new GeneratedVersion(); var gv = new GeneratedVersion();
gv.setProject(PROJECT); gv.setProject(PROJECT);
var t = GeneratedVersionOperation.buildTemplate(gv);
var t = gv.buildTemplate();
assertThat(t).isNotNull(); assertThat(t).isNotNull();
assertThat(gv.getProject()).isEqualTo(PROJECT); assertThat(gv.getProject()).isEqualTo(PROJECT);
@ -112,15 +126,15 @@ class GeneratedVersionTest {
} }
@Test @Test
void testExecute() throws IOException { void testExecute() throws Exception {
var tmpDir = Files.createTempDirectory("bld-generated-version-").toFile(); var tmpDir = Files.createTempDirectory("bld-generated-version-").toFile();
tmpDir.deleteOnExit(); tmpDir.deleteOnExit();
new GeneratedVersionOperation() new GeneratedVersionOperation()
.fromProject(PROJECT) .fromProject(PROJECT)
.directory(tmpDir.getAbsolutePath()) .directory(tmpDir.getAbsolutePath())
.extension(".java") .extension(".java")
.classTemplate("src/test/resources/myversion_test.txt") .classTemplate("src/test/resources/other_version_test.txt")
.packageName("") .packageName("")
.className("MyVersion") .className("MyVersion")
.execute(); .execute();
@ -130,23 +144,42 @@ class GeneratedVersionTest {
assertThat(new File(tmpDir, "MyVersion.java")).exists(); assertThat(new File(tmpDir, "MyVersion.java")).exists();
} }
@Test
void testGeneratedVersion() {
var gv = new GeneratedVersion();
gv.setProject(PROJECT);
gv.setTemplate(new File(gv.getProject().srcTestResourcesDirectory().getAbsolutePath(), "version_test.txt"));
gv.setPackageName("com.example.cool");
gv.setProjectName("Cool App");
gv.setClassName("CoolVersion");
gv.setDirectory(new File("build"));
gv.setExtension(".java");
assertThat(gv.getProject()).as("project").isEqualTo(PROJECT);
assertThat(gv.getTemplate()).as("template").exists();
assertThat(gv.getPackageName()).as("package name").isEqualTo("com.example.cool");
assertThat(gv.getProjectName()).as("project name").isEqualTo("Cool App");
assertThat(gv.getClassName()).as("class name").isEqualTo("CoolVersion");
assertThat(gv.getExtension()).as("extension").isEqualTo(".java");
assertThat(gv.getDirectory()).as("directory").isDirectory();
}
@Test @Test
void testWriteTemplate() throws IOException { void testWriteTemplate() throws IOException {
var tmpDir = Files.createTempDirectory("bld-generated-version-").toFile(); var tmpDir = Files.createTempDirectory("bld-generated-version-").toFile();
tmpDir.deleteOnExit(); tmpDir.deleteOnExit();
var gv = new GeneratedVersion();
var gv = new GeneratedVersion();
gv.setProject(PROJECT); gv.setProject(PROJECT);
gv.setDirectory(tmpDir); gv.setDirectory(tmpDir);
var t = GeneratedVersionOperation.buildTemplate(gv); var t = gv.buildTemplate();
gv.writeTemplate(t);
GeneratedVersionOperation.writeTemplate(t, gv);
assertThat(gv.getClassFile()).exists();
deleteOnExit(tmpDir); deleteOnExit(tmpDir);
assertThat(gv.getClassFile()).exists();
var versionClass = FileUtils.readString(gv.getClassFile()); var versionClass = FileUtils.readString(gv.getClassFile());
assertThat(versionClass).contains("package com.example;").contains("class GeneratedVersion") assertThat(versionClass).contains("package com.example;").contains("class GeneratedVersion")
.contains("MAJOR = 2").contains("MINOR = 1").contains("REVISION = 3") .contains("MAJOR = 2").contains("MINOR = 1").contains("REVISION = 3")