diff --git a/.gitignore b/.gitignore
index 24f188e..ac3aa13 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,8 +4,8 @@
**/.idea/tasks.xml
**/.idea/workspace.xml
*.iws
-.DS_Store
.classpath
+.DS_Store
.gradle
.kobalt
.nb-gradle
@@ -17,12 +17,14 @@
/dist
/gen
/gradle.properties
+/lib/kotlin*
+/libs
/local.properties
/out
/proguard-project.txt
/project.properties
/target
/test-output
-Thumbs.db
ehthumbs.db
-kobaltBuild
\ No newline at end of file
+kobaltBuild
+Thumbs.db
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index 5a62455..5fee110 100644
--- a/build.gradle
+++ b/build.gradle
@@ -59,7 +59,7 @@ repositories {
}
dependencies {
- compile 'org.apache.velocity:velocity:1.7'
+ compile 'com.github.spullara.mustache.java:compiler:0.9.4'
testCompile 'org.testng:testng:6.11'
}
diff --git a/src/main/java/net/thauvin/erik/semver/Constants.java b/src/main/java/net/thauvin/erik/semver/Constants.java
index a9063dd..dc29230 100644
--- a/src/main/java/net/thauvin/erik/semver/Constants.java
+++ b/src/main/java/net/thauvin/erik/semver/Constants.java
@@ -43,10 +43,23 @@ public final class Constants {
* The default class name.
*/
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.
**/
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.
*/
@@ -59,14 +72,11 @@ public final class Constants {
* The default patch version.
*/
public static final int DEFAULT_PATCH = 0;
- /**
- * The default Velocity template.
- */
- public static final String DEFAULT_TEMPLATE = "version.vm";
/**
* The empty string.
*/
public static final String EMPTY = "";
+
/**
* The build metadata property key.
*/
@@ -95,10 +105,6 @@ public final class Constants {
* The kotlin type.
*/
public static final String KOTLIN_TYPE = "kt";
- /**
- * The velocity properties name.
- */
- public static final String VELOCITY_PROPERTIES = "velocity.properties";
/**
* Disables the default constructor.
diff --git a/src/main/java/net/thauvin/erik/semver/Version.java b/src/main/java/net/thauvin/erik/semver/Version.java
index 178eb4d..eae0a20 100644
--- a/src/main/java/net/thauvin/erik/semver/Version.java
+++ b/src/main/java/net/thauvin/erik/semver/Version.java
@@ -74,7 +74,7 @@ public @interface Version {
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;
}
\ No newline at end of file
diff --git a/src/main/java/net/thauvin/erik/semver/VersionInfo.java b/src/main/java/net/thauvin/erik/semver/VersionInfo.java
index a6819df..d6648d5 100644
--- a/src/main/java/net/thauvin/erik/semver/VersionInfo.java
+++ b/src/main/java/net/thauvin/erik/semver/VersionInfo.java
@@ -41,18 +41,16 @@ package net.thauvin.erik.semver;
public class VersionInfo {
private final long epoch = System.currentTimeMillis();
- private String buildmeta;
-
+ private String buildMeta;
+ private String className;
private int major;
-
private int minor;
-
+ private String packageName;
private int patch;
-
- private String prerelease;
-
+ private String preRelease;
private String project;
+
/**
* Creates a new object with default values
*/
@@ -60,9 +58,11 @@ public class VersionInfo {
major = Constants.DEFAULT_MAJOR;
minor = Constants.DEFAULT_MINOR;
patch = Constants.DEFAULT_PATCH;
- buildmeta = Constants.EMPTY;
- prerelease = Constants.EMPTY;
+ buildMeta = Constants.EMPTY;
+ preRelease = Constants.EMPTY;
project = Constants.EMPTY;
+ className = Constants.DEFAULT_CLASS_NAME;
+ packageName = Constants.EMPTY;
}
/**
@@ -74,9 +74,10 @@ public class VersionInfo {
major = version.major();
minor = version.minor();
patch = version.patch();
- buildmeta = version.buildmeta();
- prerelease = version.prerelease();
+ buildMeta = version.buildmeta();
+ preRelease = version.prerelease();
project = version.project();
+ className = version.className();
}
/**
@@ -84,17 +85,35 @@ public class VersionInfo {
*
* @return The build metadata.
*/
- public String getBuildMetadata() {
- return buildmeta;
+ public String getBuildMeta() {
+ return buildMeta;
}
/**
* Sets the build metadata.
*
- * @param buildmeta The new build metadata.
+ * @param buildMeta The new build metadata.
*/
- public void setBuildMetadata(final String buildmeta) {
- this.buildmeta = buildmeta;
+ public void setBuildMeta(final String 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;
}
+ /**
+ * 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.
*
@@ -166,16 +203,16 @@ public class VersionInfo {
* @return The pre-release version.
*/
public String getPreRelease() {
- return prerelease;
+ return preRelease;
}
/**
* 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) {
- this.prerelease = prerelease;
+ public void setPreRelease(final String preRelease) {
+ this.preRelease = preRelease;
}
/**
@@ -218,7 +255,7 @@ public class VersionInfo {
+ Integer.toString(minor)
+ '.'
+ Integer.toString(patch)
- + (prerelease.length() > 0 ? '-' + prerelease : "")
- + (buildmeta.length() > 0 ? '+' + buildmeta : "");
+ + (preRelease.length() > 0 ? '-' + preRelease : "")
+ + (buildMeta.length() > 0 ? '+' + buildMeta : "");
}
}
\ No newline at end of file
diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java
index b5038db..17bd67c 100644
--- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java
+++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java
@@ -31,9 +31,9 @@
*/
package net.thauvin.erik.semver;
-import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
+import com.github.mustachejava.DefaultMustacheFactory;
+import com.github.mustachejava.Mustache;
+import com.github.mustachejava.MustacheFactory;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@@ -45,7 +45,6 @@ import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.*;
-import java.net.URL;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
@@ -89,7 +88,7 @@ public class VersionProcessor extends AbstractProcessor {
versionInfo.setMajor(parseIntProperty(p, version.majorKey(), Constants.DEFAULT_MAJOR));
versionInfo.setMinor(parseIntProperty(p, version.minorKey(), Constants.DEFAULT_MINOR));
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));
}
} else {
@@ -161,12 +160,19 @@ public class VersionProcessor extends AbstractProcessor {
final PackageElement packageElement = (PackageElement) enclosingElement;
try {
final VersionInfo versionInfo = findValues(version);
+ versionInfo.setPackageName(packageElement.getQualifiedName().toString());
note("Found version: " + versionInfo.getVersion());
- writeTemplate(version.type(),
- packageElement.getQualifiedName().toString(),
- version.className(),
- versionInfo,
- version.template());
+ final String template;
+ if (version.template().equals(Constants.DEFAULT_JAVA_TEMPLATE) &&
+ new File(Constants.DEFAULT_TEMPLATE).exists()) {
+ template = Constants.DEFAULT_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) {
error("IOException occurred while running the annotation processor", e);
}
@@ -180,51 +186,26 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.WARNING, s);
}
- private void writeTemplate(final String type,
- final String packageName,
- final String className,
- final VersionInfo versionInfo,
- final String template)
+ private void writeTemplate(final String type, final VersionInfo versionInfo, final String template)
throws IOException {
- final Properties p = new Properties();
- final URL url = this.getClass().getClassLoader().getResource(Constants.VELOCITY_PROPERTIES);
+ final MustacheFactory mf = new DefaultMustacheFactory();
+ final Mustache mustache = mf.compile(template);
- if (url != null) {
- p.load(url.openStream());
+ note("Loaded template: " + mustache.getName());
- final VelocityEngine ve = new VelocityEngine(p);
- ve.init();
-
- final VelocityContext vc = new VelocityContext();
- 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());
+ final FileObject jfo;
+ if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) {
+ jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT, versionInfo.getPackageName(),
+ versionInfo.getClassName() + '.' + type);
} 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());
}
+
}
\ No newline at end of file
diff --git a/src/main/resources/semver-kt.mustache b/src/main/resources/semver-kt.mustache
new file mode 100644
index 0000000..ed26be3
--- /dev/null
+++ b/src/main/resources/semver-kt.mustache
@@ -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 Semantic Version
+ * Annotation Processor
+ */
+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
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/resources/version.vm b/src/main/resources/semver.mustache
similarity index 61%
rename from src/main/resources/version.vm
rename to src/main/resources/semver.mustache
index 80bde66..af49af4 100644
--- a/src/main/resources/version.vm
+++ b/src/main/resources/semver.mustache
@@ -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.
* Do not modify! -- ALL CHANGES WILL BE ERASED!
*/
-package ${packageName};
+package {{packageName}};
import java.util.Date;
@@ -27,25 +12,26 @@ import java.util.Date;
* @author Semantic Version
* Annotation Processor
*/
-public final class ${className} {
+public final class {{className}} {
private final static String DEFAULT_PRERELEASE_PREFIX = "-";
private final static string DEFAULT_BUILDMETA_PREFIX = "+";
- private final static String buildmeta = "${buildmeta}";
- private final static Date date = new Date(${epoch}L);
- private final static int major = ${major};
- private final static int minor = ${minor};
- private final static int patch = ${patch};
- private final static String prerelease = "${prerelease}";
- private final static String project = "${project}";
+ private final static String project = "{{project}}";
+ private final static Date date = new Date({{epoch}}L);
+ private final static int major = {{major}};
+ private final static int minor = {{minor}};
+ private final static int patch = {{patch}};
+ private final static String preRelease = "{{preRelease}}";
+ private final static String buildMeta = "{{buildMeta}}";
+
/**
* Disables the default constructor.
*
* @throws UnsupportedOperationException If the constructor is called.
*/
- private ${className}()
- throws UnsupportedOperationException {
+ private {{className}}()
+ throws UnsupportedOperationException {
throw new UnsupportedOperationException("Illegal constructor call.");
}
@@ -87,9 +73,9 @@ public final class ${className} {
*/
public static String getVersion() {
return Integer.toString(getMajor()) + '.'
- + Integer.toString(getMinor()) + '.'
- + Integer.toString(getPatch())
- + getPreReleaseWithPrefix + getBuildMetadataWithPrefix;
+ + Integer.toString(getMinor()) + '.'
+ + Integer.toString(getPatch())
+ + getPreReleaseWithPrefix + getBuildMetaWithPrefix;
}
/**
@@ -125,7 +111,7 @@ public final class ${className} {
* @return The pre-release version, if any
*/
public static int getPreRelease() {
- return prerelease;
+ return preRelease;
}
/**
@@ -133,8 +119,8 @@ public final class ${className} {
*
* @return The build metadata, if any.
*/
- public static String getBuildMetadata() {
- return buildmeta;
+ public static String getBuildMeta() {
+ return buildMeta;
}
/**
@@ -149,14 +135,14 @@ public final class ${className} {
/**
* Returns the pre-release version.
*
- * @param prefix The refix to prepend.
+ * @param prefix The prefix to prepend.
* @return The pre-release version, if any.
*/
public static String getPreReleaseWithPrefix(final String prefix) {
- if (prerelease.length() > 0 && prefix.length() > 0) {
- return prefix + prerelease;
+ if (preRelease.length() > 0 && prefix.length() > 0) {
+ return prefix + preRelease;
} else {
- return prerelease;
+ return preRelease;
}
}
@@ -166,8 +152,8 @@ public final class ${className} {
* @param prefix The prefix to prepend.
* @return The build metadata, if any.
*/
- public static String getBuildMetadataWithPrefix() {
- return getBuildMetadataWithPrefix(DEFAULT_PRERELEASE_PREFIX);
+ public static String getBuildMetaWithPrefix() {
+ return getBuildMetaWithPrefix(DEFAULT_PRERELEASE_PREFIX);
}
/**
@@ -176,11 +162,11 @@ public final class ${className} {
* @param prefix Prefix to prepend.
* @return The build metadata, if any.
*/
- public static String getBuildMetadataWithPrefix(final String prefix) {
- if (buildmeta.length() > 0 && prefix.length() > 0) {
- return prefix + buildmeta;
+ public static String getBuildMetaWithPrefix(final String prefix) {
+ if (buildMeta.length() > 0 && prefix.length() > 0) {
+ return prefix + buildMeta;
} else {
- return buildmeta;
+ return buildMeta;
}
}
}
\ No newline at end of file
diff --git a/src/main/resources/velocity.properties b/src/main/resources/velocity.properties
deleted file mode 100644
index ba28119..0000000
--- a/src/main/resources/velocity.properties
+++ /dev/null
@@ -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
\ No newline at end of file
diff --git a/src/test/java/net/thauvin/erik/semver/VersionInfoTest.java b/src/test/java/net/thauvin/erik/semver/VersionInfoTest.java
index 90639be..de26917 100644
--- a/src/test/java/net/thauvin/erik/semver/VersionInfoTest.java
+++ b/src/test/java/net/thauvin/erik/semver/VersionInfoTest.java
@@ -68,12 +68,24 @@ public class VersionInfoTest
Assert.assertEquals(version.getVersion(), "3.2.1-beta");
- version.setBuildMetadata("001");
+ version.setBuildMeta("001");
Assert.assertEquals(version.getVersion(), "3.2.1-beta+001");
version.setPreRelease("");
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");
}
}
\ No newline at end of file