Added support for mustache instead of Velocity.

This commit is contained in:
Erik C. Thauvin 2017-04-14 22:27:07 -07:00
parent 13dec4f063
commit 5efb2a9e1d
10 changed files with 199 additions and 133 deletions

6
.gitignore vendored
View file

@ -4,8 +4,8 @@
**/.idea/tasks.xml **/.idea/tasks.xml
**/.idea/workspace.xml **/.idea/workspace.xml
*.iws *.iws
.DS_Store
.classpath .classpath
.DS_Store
.gradle .gradle
.kobalt .kobalt
.nb-gradle .nb-gradle
@ -17,12 +17,14 @@
/dist /dist
/gen /gen
/gradle.properties /gradle.properties
/lib/kotlin*
/libs
/local.properties /local.properties
/out /out
/proguard-project.txt /proguard-project.txt
/project.properties /project.properties
/target /target
/test-output /test-output
Thumbs.db
ehthumbs.db ehthumbs.db
kobaltBuild kobaltBuild
Thumbs.db

View file

@ -59,7 +59,7 @@ repositories {
} }
dependencies { dependencies {
compile 'org.apache.velocity:velocity:1.7' compile 'com.github.spullara.mustache.java:compiler:0.9.4'
testCompile 'org.testng:testng:6.11' testCompile 'org.testng:testng:6.11'
} }

View file

@ -43,10 +43,23 @@ public final class Constants {
* The default class name. * The default class name.
*/ */
public static final String DEFAULT_CLASS_NAME = "GeneratedVersion"; public static final String DEFAULT_CLASS_NAME = "GeneratedVersion";
/**
* The default mustache template.
*/
public static final String DEFAULT_TEMPLATE = "version.mustache";
/**
* /**
* The default Java mustache template.
*/
public static final String DEFAULT_JAVA_TEMPLATE = "semver.mustache";
/** /**
* The default java type. * The default java type.
**/ **/
public static final String DEFAULT_JAVA_TYPE = "java"; public static final String DEFAULT_JAVA_TYPE = "java";
/**
* The default Kotlin mustache template.
*/
public static final String DEFAULT_KOTLIN_TEMPLATE = "semver-kt.mustache";
/** /**
* The default major version. * The default major version.
*/ */
@ -59,14 +72,11 @@ public final class Constants {
* The default patch version. * The default patch version.
*/ */
public static final int DEFAULT_PATCH = 0; public static final int DEFAULT_PATCH = 0;
/**
* The default Velocity template.
*/
public static final String DEFAULT_TEMPLATE = "version.vm";
/** /**
* The empty string. * The empty string.
*/ */
public static final String EMPTY = ""; public static final String EMPTY = "";
/** /**
* The build metadata property key. * The build metadata property key.
*/ */
@ -95,10 +105,6 @@ public final class Constants {
* The kotlin type. * The kotlin type.
*/ */
public static final String KOTLIN_TYPE = "kt"; public static final String KOTLIN_TYPE = "kt";
/**
* The velocity properties name.
*/
public static final String VELOCITY_PROPERTIES = "velocity.properties";
/** /**
* Disables the default constructor. * Disables the default constructor.

View file

@ -74,7 +74,7 @@ public @interface Version {
String properties() default Constants.EMPTY; String properties() default Constants.EMPTY;
String template() default Constants.DEFAULT_TEMPLATE; String template() default Constants.DEFAULT_JAVA_TEMPLATE;
String type() default Constants.DEFAULT_JAVA_TYPE; String type() default Constants.DEFAULT_JAVA_TYPE;
} }

View file

@ -41,18 +41,16 @@ package net.thauvin.erik.semver;
public class VersionInfo { public class VersionInfo {
private final long epoch = System.currentTimeMillis(); private final long epoch = System.currentTimeMillis();
private String buildmeta; private String buildMeta;
private String className;
private int major; private int major;
private int minor; private int minor;
private String packageName;
private int patch; private int patch;
private String preRelease;
private String prerelease;
private String project; private String project;
/** /**
* Creates a new object with default values * Creates a new object with default values
*/ */
@ -60,9 +58,11 @@ public class VersionInfo {
major = Constants.DEFAULT_MAJOR; major = Constants.DEFAULT_MAJOR;
minor = Constants.DEFAULT_MINOR; minor = Constants.DEFAULT_MINOR;
patch = Constants.DEFAULT_PATCH; patch = Constants.DEFAULT_PATCH;
buildmeta = Constants.EMPTY; buildMeta = Constants.EMPTY;
prerelease = Constants.EMPTY; preRelease = Constants.EMPTY;
project = Constants.EMPTY; project = Constants.EMPTY;
className = Constants.DEFAULT_CLASS_NAME;
packageName = Constants.EMPTY;
} }
/** /**
@ -74,9 +74,10 @@ public class VersionInfo {
major = version.major(); major = version.major();
minor = version.minor(); minor = version.minor();
patch = version.patch(); patch = version.patch();
buildmeta = version.buildmeta(); buildMeta = version.buildmeta();
prerelease = version.prerelease(); preRelease = version.prerelease();
project = version.project(); project = version.project();
className = version.className();
} }
/** /**
@ -84,17 +85,35 @@ public class VersionInfo {
* *
* @return The build metadata. * @return The build metadata.
*/ */
public String getBuildMetadata() { public String getBuildMeta() {
return buildmeta; return buildMeta;
} }
/** /**
* Sets the build metadata. * Sets the build metadata.
* *
* @param buildmeta The new build metadata. * @param buildMeta The new build metadata.
*/ */
public void setBuildMetadata(final String buildmeta) { public void setBuildMeta(final String buildMeta) {
this.buildmeta = buildmeta; this.buildMeta = buildMeta;
}
/**
* Returns the class name.
*
* @return The class name.
*/
public String getClassName() {
return className;
}
/**
* Sets the class name.
*
* @param className The new class name.
*/
public void setClassName(String className) {
this.className = className;
} }
/** /**
@ -142,6 +161,24 @@ public class VersionInfo {
this.minor = minor; this.minor = minor;
} }
/**
* Returns the package name.
*
* @return The package name.
*/
public String getPackageName() {
return packageName;
}
/**
* Sets the package name version.
*
* @param packageName The new patch version.
*/
public void setPackageName(String packageName) {
this.packageName = packageName;
}
/** /**
* Returns the patch version. * Returns the patch version.
* *
@ -166,16 +203,16 @@ public class VersionInfo {
* @return The pre-release version. * @return The pre-release version.
*/ */
public String getPreRelease() { public String getPreRelease() {
return prerelease; return preRelease;
} }
/** /**
* Sets the pre-release version. * Sets the pre-release version.
* *
* @param prerelease The new pre-release version. * @param preRelease The new pre-release version.
*/ */
public void setPreRelease(final String prerelease) { public void setPreRelease(final String preRelease) {
this.prerelease = prerelease; this.preRelease = preRelease;
} }
/** /**
@ -218,7 +255,7 @@ public class VersionInfo {
+ Integer.toString(minor) + Integer.toString(minor)
+ '.' + '.'
+ Integer.toString(patch) + Integer.toString(patch)
+ (prerelease.length() > 0 ? '-' + prerelease : "") + (preRelease.length() > 0 ? '-' + preRelease : "")
+ (buildmeta.length() > 0 ? '+' + buildmeta : ""); + (buildMeta.length() > 0 ? '+' + buildMeta : "");
} }
} }

View file

@ -31,9 +31,9 @@
*/ */
package net.thauvin.erik.semver; package net.thauvin.erik.semver;
import org.apache.velocity.Template; import com.github.mustachejava.DefaultMustacheFactory;
import org.apache.velocity.VelocityContext; import com.github.mustachejava.Mustache;
import org.apache.velocity.app.VelocityEngine; import com.github.mustachejava.MustacheFactory;
import javax.annotation.processing.*; import javax.annotation.processing.*;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@ -45,7 +45,6 @@ import javax.tools.Diagnostic;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.StandardLocation; import javax.tools.StandardLocation;
import java.io.*; import java.io.*;
import java.net.URL;
import java.util.HashSet; import java.util.HashSet;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@ -89,7 +88,7 @@ public class VersionProcessor extends AbstractProcessor {
versionInfo.setMajor(parseIntProperty(p, version.majorKey(), Constants.DEFAULT_MAJOR)); versionInfo.setMajor(parseIntProperty(p, version.majorKey(), Constants.DEFAULT_MAJOR));
versionInfo.setMinor(parseIntProperty(p, version.minorKey(), Constants.DEFAULT_MINOR)); versionInfo.setMinor(parseIntProperty(p, version.minorKey(), Constants.DEFAULT_MINOR));
versionInfo.setPatch(parseIntProperty(p, version.patchKey(), Constants.DEFAULT_PATCH)); versionInfo.setPatch(parseIntProperty(p, version.patchKey(), Constants.DEFAULT_PATCH));
versionInfo.setBuildMetadata(p.getProperty(version.buildmetaKey(), Constants.EMPTY)); versionInfo.setBuildMeta(p.getProperty(version.buildmetaKey(), Constants.EMPTY));
versionInfo.setPreRelease(p.getProperty(version.prereleaseKey(), Constants.EMPTY)); versionInfo.setPreRelease(p.getProperty(version.prereleaseKey(), Constants.EMPTY));
} }
} else { } else {
@ -161,12 +160,19 @@ public class VersionProcessor extends AbstractProcessor {
final PackageElement packageElement = (PackageElement) enclosingElement; final PackageElement packageElement = (PackageElement) enclosingElement;
try { try {
final VersionInfo versionInfo = findValues(version); final VersionInfo versionInfo = findValues(version);
versionInfo.setPackageName(packageElement.getQualifiedName().toString());
note("Found version: " + versionInfo.getVersion()); note("Found version: " + versionInfo.getVersion());
writeTemplate(version.type(), final String template;
packageElement.getQualifiedName().toString(), if (version.template().equals(Constants.DEFAULT_JAVA_TEMPLATE) &&
version.className(), new File(Constants.DEFAULT_TEMPLATE).exists()) {
versionInfo, template = Constants.DEFAULT_TEMPLATE;
version.template()); } else if (version.template().equals(Constants.DEFAULT_JAVA_TEMPLATE) &&
version.type().equals(Constants.KOTLIN_TYPE)) {
template = Constants.DEFAULT_KOTLIN_TEMPLATE;
} else {
template = version.template();
}
writeTemplate(version.type(), versionInfo, template);
} catch (IOException e) { } catch (IOException e) {
error("IOException occurred while running the annotation processor", e); error("IOException occurred while running the annotation processor", e);
} }
@ -180,51 +186,26 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.WARNING, s); log(Diagnostic.Kind.WARNING, s);
} }
private void writeTemplate(final String type, private void writeTemplate(final String type, final VersionInfo versionInfo, final String template)
final String packageName,
final String className,
final VersionInfo versionInfo,
final String template)
throws IOException { throws IOException {
final Properties p = new Properties(); final MustacheFactory mf = new DefaultMustacheFactory();
final URL url = this.getClass().getClassLoader().getResource(Constants.VELOCITY_PROPERTIES); final Mustache mustache = mf.compile(template);
if (url != null) { note("Loaded template: " + mustache.getName());
p.load(url.openStream());
final VelocityEngine ve = new VelocityEngine(p); final FileObject jfo;
ve.init(); if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) {
jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT, versionInfo.getPackageName(),
final VelocityContext vc = new VelocityContext(); versionInfo.getClassName() + '.' + type);
vc.put("packageName", packageName);
vc.put("className", className);
vc.put("project", versionInfo.getProject());
vc.put("buildmeta", versionInfo.getBuildMetadata());
vc.put("epoch", versionInfo.getEpoch());
vc.put("patch", versionInfo.getPatch());
vc.put("major", versionInfo.getMajor());
vc.put("minor", versionInfo.getMinor());
vc.put("prerelease", versionInfo.getPreRelease());
final Template vt = ve.getTemplate(template);
note("Loaded template: " + vt.getName());
final FileObject jfo;
if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) {
jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName,
className + '.' + type);
} else {
jfo = filer.createSourceFile(packageName + '.' + className);
}
try (final Writer writer = jfo.openWriter()) {
vt.merge(vc, writer);
}
note("Generated source: " + jfo.getName());
} else { } else {
error("Could not load '" + Constants.VELOCITY_PROPERTIES + "' from jar."); jfo = filer.createSourceFile(versionInfo.getPackageName() + '.' + versionInfo.getClassName());
} }
try (final Writer writer = jfo.openWriter()) {
mustache.execute(writer, versionInfo).flush();
}
note("Generated source: " + jfo.getName());
} }
} }

View file

@ -0,0 +1,46 @@
/*
* This file is automatically generated.
* Do not modify! -- ALL CHANGES WILL BE ERASED!
*/
package {{packageName}}
import java.util.*
/**
* Provides semantic version information.
*
* @author <a href="https://github.com/ethauvin/semver">Semantic Version
* Annotation Processor</a>
*/
open class {{className}}
private constructor() {
companion object {
val project = "{{project}}"
val buildDate = Date({{epoch}}L)
val major = {{major}}
val minor = {{minor}}
val patch = {{patch}}
val buildMeta = "{{buildMeta}}"
val preRelease = "{{preRelease}}"
val version: String
get() = ("$major.$minor.$patch" + preReleaseWithPrefix() + buildMetaWithPrefix())
fun preReleaseWithPrefix(prefix: String = "-"): String {
return if (preRelease.isNotEmpty() && prefix.isNotEmpty()) {
"$prefix$preRelease"
} else {
preRelease
}
}
fun buildMetaWithPrefix(prefix: String = "+"): String {
return if (buildMeta.isNotEmpty() && prefix.isNotEmpty()) {
"$prefix$buildMeta"
} else {
buildMeta
}
}
}
}

View file

@ -1,23 +1,8 @@
##############################################################################
##
## Available variables:
##
## $packageName - The package name. (string)
## $className - The class name. (string)
## $project - The project name. (string)
## $epoch - The build epoch/unix time. (long)
## $major - The major version. (int)
## $minor - The minor version. (int)
## $patch - The patch version. (int)
## $prerelease - The pre-release version. (string)
## $buildmeta - The build metadata version. (string)
##
##############################################################################
/* /*
* This file is automatically generated. * This file is automatically generated.
* Do not modify! -- ALL CHANGES WILL BE ERASED! * Do not modify! -- ALL CHANGES WILL BE ERASED!
*/ */
package ${packageName}; package {{packageName}};
import java.util.Date; import java.util.Date;
@ -27,25 +12,26 @@ import java.util.Date;
* @author <a href="https://github.com/ethauvin/semver">Semantic Version * @author <a href="https://github.com/ethauvin/semver">Semantic Version
* Annotation Processor</a> * Annotation Processor</a>
*/ */
public final class ${className} { public final class {{className}} {
private final static String DEFAULT_PRERELEASE_PREFIX = "-"; private final static String DEFAULT_PRERELEASE_PREFIX = "-";
private final static string DEFAULT_BUILDMETA_PREFIX = "+"; private final static string DEFAULT_BUILDMETA_PREFIX = "+";
private final static String buildmeta = "${buildmeta}"; private final static String project = "{{project}}";
private final static Date date = new Date(${epoch}L); private final static Date date = new Date({{epoch}}L);
private final static int major = ${major}; private final static int major = {{major}};
private final static int minor = ${minor}; private final static int minor = {{minor}};
private final static int patch = ${patch}; private final static int patch = {{patch}};
private final static String prerelease = "${prerelease}"; private final static String preRelease = "{{preRelease}}";
private final static String project = "${project}"; private final static String buildMeta = "{{buildMeta}}";
/** /**
* Disables the default constructor. * Disables the default constructor.
* *
* @throws UnsupportedOperationException If the constructor is called. * @throws UnsupportedOperationException If the constructor is called.
*/ */
private ${className}() private {{className}}()
throws UnsupportedOperationException { throws UnsupportedOperationException {
throw new UnsupportedOperationException("Illegal constructor call."); throw new UnsupportedOperationException("Illegal constructor call.");
} }
@ -87,9 +73,9 @@ public final class ${className} {
*/ */
public static String getVersion() { public static String getVersion() {
return Integer.toString(getMajor()) + '.' return Integer.toString(getMajor()) + '.'
+ Integer.toString(getMinor()) + '.' + Integer.toString(getMinor()) + '.'
+ Integer.toString(getPatch()) + Integer.toString(getPatch())
+ getPreReleaseWithPrefix + getBuildMetadataWithPrefix; + getPreReleaseWithPrefix + getBuildMetaWithPrefix;
} }
/** /**
@ -125,7 +111,7 @@ public final class ${className} {
* @return The pre-release version, if any * @return The pre-release version, if any
*/ */
public static int getPreRelease() { public static int getPreRelease() {
return prerelease; return preRelease;
} }
/** /**
@ -133,8 +119,8 @@ public final class ${className} {
* *
* @return The build metadata, if any. * @return The build metadata, if any.
*/ */
public static String getBuildMetadata() { public static String getBuildMeta() {
return buildmeta; return buildMeta;
} }
/** /**
@ -149,14 +135,14 @@ public final class ${className} {
/** /**
* Returns the pre-release version. * Returns the pre-release version.
* *
* @param prefix The refix to prepend. * @param prefix The prefix to prepend.
* @return The pre-release version, if any. * @return The pre-release version, if any.
*/ */
public static String getPreReleaseWithPrefix(final String prefix) { public static String getPreReleaseWithPrefix(final String prefix) {
if (prerelease.length() > 0 && prefix.length() > 0) { if (preRelease.length() > 0 && prefix.length() > 0) {
return prefix + prerelease; return prefix + preRelease;
} else { } else {
return prerelease; return preRelease;
} }
} }
@ -166,8 +152,8 @@ public final class ${className} {
* @param prefix The prefix to prepend. * @param prefix The prefix to prepend.
* @return The build metadata, if any. * @return The build metadata, if any.
*/ */
public static String getBuildMetadataWithPrefix() { public static String getBuildMetaWithPrefix() {
return getBuildMetadataWithPrefix(DEFAULT_PRERELEASE_PREFIX); return getBuildMetaWithPrefix(DEFAULT_PRERELEASE_PREFIX);
} }
/** /**
@ -176,11 +162,11 @@ public final class ${className} {
* @param prefix Prefix to prepend. * @param prefix Prefix to prepend.
* @return The build metadata, if any. * @return The build metadata, if any.
*/ */
public static String getBuildMetadataWithPrefix(final String prefix) { public static String getBuildMetaWithPrefix(final String prefix) {
if (buildmeta.length() > 0 && prefix.length() > 0) { if (buildMeta.length() > 0 && prefix.length() > 0) {
return prefix + buildmeta; return prefix + buildMeta;
} else { } else {
return buildmeta; return buildMeta;
} }
} }
} }

View file

@ -1,4 +0,0 @@
runtime.log.logsystem.class = org.apache.velocity.runtime.log.SystemLogChute
resource.loader = file, class
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

View file

@ -68,12 +68,24 @@ public class VersionInfoTest
Assert.assertEquals(version.getVersion(), "3.2.1-beta"); Assert.assertEquals(version.getVersion(), "3.2.1-beta");
version.setBuildMetadata("001"); version.setBuildMeta("001");
Assert.assertEquals(version.getVersion(), "3.2.1-beta+001"); Assert.assertEquals(version.getVersion(), "3.2.1-beta+001");
version.setPreRelease(""); version.setPreRelease("");
Assert.assertEquals(version.getVersion(), "3.2.1+001"); Assert.assertEquals(version.getVersion(), "3.2.1+001");
version.setPackageName("com.example");
Assert.assertEquals(version.getPackageName(), "com.example");
version.setProject("Example");
Assert.assertEquals(version.getProject(), "Example");
version.setClassName("Example");
Assert.assertEquals(version.getClassName(), "Example");
} }
} }