Added directory and extension options
This commit is contained in:
parent
b56f684ac8
commit
f8003c72f0
10 changed files with 130 additions and 54 deletions
|
@ -18,6 +18,7 @@ package rife.bld.extension;
|
|||
|
||||
import rife.bld.BuildCommand;
|
||||
import rife.bld.Project;
|
||||
import rife.bld.operations.JarOperation;
|
||||
import rife.bld.publish.PublishDeveloper;
|
||||
import rife.bld.publish.PublishLicense;
|
||||
import rife.bld.publish.PublishScm;
|
||||
|
@ -35,7 +36,7 @@ public class GeneratedVersionOperationBuild extends Project {
|
|||
public GeneratedVersionOperationBuild() {
|
||||
pkg = "rife.bld.extension";
|
||||
name = "GeneratedVersionOperation";
|
||||
version = version(0, 9, 2);
|
||||
version = version(0, 9, 3, "SNAPSHOT");
|
||||
|
||||
javaRelease = 17;
|
||||
downloadSources = true;
|
||||
|
@ -45,8 +46,8 @@ public class GeneratedVersionOperationBuild extends Project {
|
|||
scope(compile)
|
||||
.include(dependency("com.uwyn.rife2", "bld", version(1, 7, 5)));
|
||||
scope(test)
|
||||
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 0)))
|
||||
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 0)))
|
||||
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1)))
|
||||
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 1)))
|
||||
.include(dependency("org.assertj", "assertj-core", version(3, 24, 2)));
|
||||
|
||||
precompileOperation()
|
||||
|
@ -54,6 +55,7 @@ public class GeneratedVersionOperationBuild extends Project {
|
|||
|
||||
javadocOperation()
|
||||
.javadocOptions()
|
||||
.author()
|
||||
.docLint(NO_MISSING)
|
||||
.link("https://rife2.github.io/bld/")
|
||||
.link("https://rife2.github.io/rife2/")
|
||||
|
|
|
@ -30,6 +30,8 @@ import java.io.File;
|
|||
public class GeneratedVersion {
|
||||
private File classFile;
|
||||
private String className;
|
||||
private File directory;
|
||||
private String extension = ".java";
|
||||
private String packageName;
|
||||
private BaseProject project;
|
||||
private String projectName;
|
||||
|
@ -42,13 +44,6 @@ public class GeneratedVersion {
|
|||
return classFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the class file.
|
||||
*/
|
||||
public void setClassFile(File classFile) {
|
||||
this.classFile = classFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class name.
|
||||
*/
|
||||
|
@ -57,10 +52,17 @@ public class GeneratedVersion {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the class name.
|
||||
* Returns the destination directory.
|
||||
*/
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
public File getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file extension.
|
||||
*/
|
||||
public String getExtension() {
|
||||
return extension;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,13 +72,6 @@ public class GeneratedVersion {
|
|||
return packageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the package name.
|
||||
*/
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the project.
|
||||
*/
|
||||
|
@ -84,13 +79,6 @@ public class GeneratedVersion {
|
|||
return project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the project.
|
||||
*/
|
||||
public void setProject(BaseProject project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the project name.
|
||||
*/
|
||||
|
@ -98,13 +86,6 @@ public class GeneratedVersion {
|
|||
return projectName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the project name.
|
||||
*/
|
||||
public void setProjectName(String projectName) {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the template.
|
||||
*/
|
||||
|
@ -112,6 +93,55 @@ public class GeneratedVersion {
|
|||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the class file.
|
||||
*/
|
||||
public void setClassFile(File classFile) {
|
||||
this.classFile = classFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the class name.
|
||||
*/
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the destination directory.
|
||||
*/
|
||||
public void setDirectory(File directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the file extension. (e.g. {@code .java}
|
||||
*/
|
||||
public void setExtension(String extension) {
|
||||
this.extension = extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the package name.
|
||||
*/
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the project.
|
||||
*/
|
||||
public void setProject(BaseProject project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the project name.
|
||||
*/
|
||||
public void setProjectName(String projectName) {
|
||||
this.projectName = projectName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the template file.
|
||||
*/
|
||||
|
|
|
@ -115,12 +115,13 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
|
|||
/**
|
||||
* Writes the project version class in the given directory.
|
||||
*/
|
||||
public static void writeTemplate(Template template, File directory, GeneratedVersion gv) {
|
||||
public static void writeTemplate(Template template, GeneratedVersion gv) {
|
||||
if (gv.getPackageName() != null) {
|
||||
gv.setClassFile(Path.of(directory.getAbsolutePath(),
|
||||
gv.getPackageName().replace(".", File.separator), gv.getClassName() + ".java").toFile());
|
||||
gv.setClassFile(Path.of(gv.getDirectory().getAbsolutePath(),
|
||||
gv.getPackageName().replace(".", File.separator), gv.getClassName()
|
||||
+ gv.getExtension()).toFile());
|
||||
} else {
|
||||
gv.setClassFile(Path.of(directory.getAbsolutePath(), gv.getClassName() + ".java").toFile());
|
||||
gv.setClassFile(Path.of(gv.getDirectory().getAbsolutePath(), gv.getClassName() + ".java").toFile());
|
||||
}
|
||||
|
||||
if (!gv.getClassFile().getParentFile().exists()) {
|
||||
|
@ -135,8 +136,8 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
|
|||
var updated = gv.getClassFile().exists();
|
||||
FileUtils.writeString(template.getContent(), gv.getClassFile());
|
||||
if (LOGGER.isLoggable(Level.INFO)) {
|
||||
LOGGER.log(Level.INFO, "Generated version class has been {0} to {1}: {2}",
|
||||
new String[]{updated ? "updated" : "created", gv.getProject().version().toString(),
|
||||
LOGGER.log(Level.INFO, "Generated version ({0}) class has been {1}: {2}",
|
||||
new String[]{gv.getProject().version().toString(), updated ? "updated" : "created",
|
||||
gv.getClassFile().toString()});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
@ -162,6 +163,14 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the destination directory.
|
||||
*/
|
||||
public GeneratedVersionOperation directory(File directory) {
|
||||
generatedVersion.setDirectory(directory);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a version data class for this project.
|
||||
*/
|
||||
|
@ -172,7 +181,15 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
|
|||
}
|
||||
|
||||
var template = buildTemplate(generatedVersion);
|
||||
writeTemplate(template, generatedVersion.getProject().srcMainJavaDirectory(), generatedVersion);
|
||||
writeTemplate(template, generatedVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the file extension. (e.g. {@code .java})
|
||||
*/
|
||||
public GeneratedVersionOperation extension(String extension) {
|
||||
generatedVersion.setExtension(extension);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,6 +197,7 @@ public class GeneratedVersionOperation extends AbstractOperation<GeneratedVersio
|
|||
*/
|
||||
public GeneratedVersionOperation fromProject(BaseProject project) {
|
||||
generatedVersion.setProject(project);
|
||||
generatedVersion.setDirectory(project.srcMainJavaDirectory());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -75,10 +75,21 @@ class GeneratedVersionTest {
|
|||
gv.setClassName("MyVersion");
|
||||
|
||||
var t = GeneratedVersionOperation.buildTemplate(gv);
|
||||
|
||||
assertThat(t.getContent()).contains("package com.example.my;").contains("class MyVersion")
|
||||
.contains("MAJOR = 2").contains("MINOR = 1").contains("REVISION = 3").contains("QUALIFIER = \"\"")
|
||||
.contains("private MyVersion").contains("PROJECT = \"My App\"");
|
||||
assertThat(t.getContent()).isEqualTo("""
|
||||
package com.example.my;
|
||||
|
||||
public final class MyVersion {
|
||||
public static final int PROJECT = "My App";
|
||||
public static final int MAJOR = 2;
|
||||
public static final int MINOR = 1;
|
||||
public static final int REVISION = 3;
|
||||
public static final String QUALIFIER = "";
|
||||
|
||||
private MyVersion() {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -101,13 +112,15 @@ class GeneratedVersionTest {
|
|||
|
||||
@Test
|
||||
void testWriteTemplate() throws IOException {
|
||||
var tmpDir = Files.createTempDirectory("bldGeneratedVersion").toFile();
|
||||
var gv = new GeneratedVersion();
|
||||
|
||||
gv.setProject(PROJECT);
|
||||
gv.setDirectory(tmpDir);
|
||||
|
||||
var t = GeneratedVersionOperation.buildTemplate(gv);
|
||||
|
||||
var tmpDir = Files.createTempDirectory("bldGeneratedVersion").toFile();
|
||||
|
||||
GeneratedVersionOperation.writeTemplate(t, tmpDir, gv);
|
||||
GeneratedVersionOperation.writeTemplate(t, gv);
|
||||
|
||||
assertThat(gv.getClassFile()).exists();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue