Initial commit

This commit is contained in:
Erik C. Thauvin 2023-04-27 22:36:49 -07:00
commit c103a85140
30 changed files with 981 additions and 0 deletions

View file

@ -0,0 +1,44 @@
package rife.bld.extension;
import rife.bld.BaseProject;
import rife.bld.BuildCommand;
import java.util.List;
import static rife.bld.dependencies.Repository.MAVEN_CENTRAL;
import static rife.bld.dependencies.Repository.RIFE2_RELEASES;
import static rife.bld.dependencies.Scope.compile;
import static rife.bld.dependencies.Scope.test;
public class GeneratedVersionOperationBuild extends BaseProject {
public GeneratedVersionOperationBuild() {
pkg = "rife.bld.extension";
name = "GeneratedVersionOperation";
version = version(0, 9, 0, "SNAPSHOT");
javaRelease = 17;
downloadSources = true;
autoDownloadPurge = true;
repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
scope(compile)
.include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 22)));
scope(test)
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 9, 2)))
.include(dependency("org.assertj:assertj-joda-time:2.2.0"));
}
public static void main(String[] args) {
new GeneratedVersionOperationBuild().start(args);
}
@BuildCommand(summary = "Runs PMD analysis")
public void pmd() throws Exception {
new PmdOperation()
.fromProject(this)
.failOnViolation(true)
.ruleSets("config/pmd.xml")
.execute();
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rife.bld.extension;
import rife.bld.BaseProject;
import java.io.File;
/**
* GeneratedVersion data class.
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
@SuppressWarnings("PMD.DataClass")
public class GeneratedVersion {
private String className;
private String packageName;
private BaseProject project;
private String projectName;
private File template;
public String getClassName() {
return className;
}
public String getPackageName() {
return packageName;
}
public BaseProject getProject() {
return project;
}
public String getProjectName() {
return projectName;
}
public File getTemplate() {
return template;
}
public void setClassName(String className) {
this.className = className;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public void setProject(BaseProject project) {
this.project = project;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public void setTemplate(File template) {
this.template = template;
}
}

View file

@ -0,0 +1,187 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rife.bld.extension;
import rife.bld.BaseProject;
import rife.bld.operations.AbstractOperation;
import rife.resources.ResourceFinderDirectories;
import rife.template.Template;
import rife.template.TemplateFactory;
import rife.tools.FileUtils;
import rife.tools.exceptions.FileUtilsErrorException;
import java.io.File;
import java.nio.file.Path;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Implements the GeneratedVersionOperation class.
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
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 String MAJOR = "major";
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.
*/
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(new File[]{gv.getTemplate().getParentFile()});
template = 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;
}
public static void writeTemplate(Template template, GeneratedVersion gv) {
Path generatedVersionPath;
if (gv.getPackageName() != null) {
generatedVersionPath = Path.of(gv.getProject().srcMainJavaDirectory().getAbsolutePath(),
gv.getPackageName().replace(".", File.separator), gv.getClassName());
} else {
generatedVersionPath = Path.of(gv.getProject().srcMainJavaDirectory().getAbsolutePath(),
gv.getClassName());
}
if (generatedVersionPath.getParent().toFile().mkdirs()) {
try {
FileUtils.writeString(template.getContent(), generatedVersionPath.toFile());
} catch (FileUtilsErrorException e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "Unable to write the version class file.", e);
}
}
} else {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.severe("Could not create project package directories.");
}
}
}
/**
* Sets the class name.
*/
public GeneratedVersionOperation className(String className) {
generatedVersion.setClassName(className);
return this;
}
/**
* Sets the class template path
*/
public GeneratedVersionOperation classTemplate(File template) {
generatedVersion.setTemplate(template);
return this;
}
@Override
public void execute() throws Exception {
if (generatedVersion.getProject() == null && LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.severe("A project must be specified.");
}
var template = buildTemplate(generatedVersion);
writeTemplate(template, generatedVersion);
}
/**
* Sets the project name.
*/
public GeneratedVersionOperation fromProject(BaseProject project) {
generatedVersion.setProject(project);
return this;
}
/**
* Sets the package name.
*/
public GeneratedVersionOperation packageName(String packageName) {
generatedVersion.setPackageName(packageName);
return this;
}
/**
* Sets the project name.
*/
public GeneratedVersionOperation projectName(String projectName) {
generatedVersion.setPackageName(projectName);
return this;
}
}

View file

@ -0,0 +1,28 @@
/*
* This file is automatically generated.
* Do not modify! -- ALL CHANGES WILL BE ERASED!
*/
package {{v packageName/}};
import java.util.Date;
/**
* Provides project version information.
*/
public final class {{v className/}} {
public static final String PROJECT = "{{v project/}}";
public static final Date BUILDDATE = 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/}}";
/**
* Disables the default constructor.
*/
private {{v className/}}() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rife.bld.extension;
import org.junit.jupiter.api.Test;
import rife.bld.BaseProject;
import rife.bld.WebProject;
import rife.bld.dependencies.VersionNumber;
import java.io.File;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Implements the GeneratedVersionTest class.
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
class GeneratedVersionTest {
private final BaseProject PROJECT = new WebProject() {
@Override
public String pkg() {
return "com.example";
}
@Override
public String name() {
return "MyExample";
}
@Override
public VersionNumber version() {
return new VersionNumber(2, 1, 3);
}
};
@Test
void buildTemplateCustomTest() {
var gv = new GeneratedVersion();
gv.setProject(PROJECT);
gv.setTemplate(new File(gv.getProject().srcTestResourcesDirectory().getAbsolutePath(),
"version_test.txt"));
gv.setPackageName("com.example.my");
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");
}
@Test
void testBuildTemplate() {
var gv = new GeneratedVersion();
gv.setProject(PROJECT);
var t = GeneratedVersionOperation.buildTemplate(gv);
assertThat(t).isNotNull();
assertThat(gv.getProject()).isEqualTo(PROJECT);
assertThat(gv.getPackageName()).isEqualTo(PROJECT.pkg());
assertThat(gv.getProjectName()).isEqualTo(PROJECT.name());
assertThat(t.getContent()).contains("package com.example;").contains("class GeneratedVersion")
.contains("PROJECT = \"MyExample\";").contains("MAJOR = 2").contains("MINOR = 1")
.contains("REVISION = 3").contains("QUALIFIER = \"\"").contains("VERSION = \"2.1.3\"")
.contains("private GeneratedVersion");
}
}

View file

@ -0,0 +1,20 @@
package {{v packageName/}};
import java.util.Date;
/**
* Provides project version information.
*/
public final class {{v className/}} {
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/}}";
/**
* Disables the default constructor.
*/
private {{v className/}}() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
}