Added support for generating koltin source file.

This commit is contained in:
Erik C. Thauvin 2017-04-14 16:29:43 -07:00
parent 733576c580
commit 13dec4f063
4 changed files with 79 additions and 57 deletions

View file

@ -43,62 +43,58 @@ public final class Constants {
* The default class name.
*/
public static final String DEFAULT_CLASS_NAME = "GeneratedVersion";
/**
* The default java type.
**/
public static final String DEFAULT_JAVA_TYPE = "java";
/**
* The default major version.
*/
public static final int DEFAULT_MAJOR = 1;
/**
* The default minor version.
*/
public static final int DEFAULT_MINOR = 0;
/**
* 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.
*/
public static final String KEY_VERSION_BUILDMETA = "version.buildmeta";
/**
* The major version property key.
*/
public static final String KEY_VERSION_MAJOR = "version.major";
/**
* The minor version property key.
*/
public static final String KEY_VERSION_MINOR = "version.minor";
/**
* The patch version property key.
*/
public static final String KEY_VERSION_PATCH = "version.patch";
/**
* The pre-release version property key.
*/
public static final String KEY_VERSION_PRERELEASE = "version.prerelease";
/**
* The project property key.
*/
public static final String KEY_VERSION_PROJECT = "version.project";
/**
* The kotlin type.
*/
public static final String KOTLIN_TYPE = "kt";
/**
* The velocity properties name.
*/

View file

@ -75,4 +75,6 @@ public @interface Version {
String properties() default Constants.EMPTY;
String template() default Constants.DEFAULT_TEMPLATE;
String type() default Constants.DEFAULT_JAVA_TYPE;
}

View file

@ -42,7 +42,8 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.*;
import java.net.URL;
import java.util.HashSet;
@ -161,7 +162,8 @@ public class VersionProcessor extends AbstractProcessor {
try {
final VersionInfo versionInfo = findValues(version);
note("Found version: " + versionInfo.getVersion());
writeTemplate(packageElement.getQualifiedName().toString(),
writeTemplate(version.type(),
packageElement.getQualifiedName().toString(),
version.className(),
versionInfo,
version.template());
@ -178,7 +180,8 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.WARNING, s);
}
private void writeTemplate(final String packageName,
private void writeTemplate(final String type,
final String packageName,
final String className,
final VersionInfo versionInfo,
final String template)
@ -207,7 +210,14 @@ public class VersionProcessor extends AbstractProcessor {
note("Loaded template: " + vt.getName());
final JavaFileObject jfo = filer.createSourceFile(packageName + '.' + className);
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);
}

View file

@ -28,6 +28,9 @@ import java.util.Date;
* Annotation Processor</a>
*/
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};
@ -86,7 +89,7 @@ public final class ${className} {
return Integer.toString(getMajor()) + '.'
+ Integer.toString(getMinor()) + '.'
+ Integer.toString(getPatch())
+ getPreRelease(true) + getBuildMetadata(true);
+ getPreReleaseWithPrefix + getBuildMetadataWithPrefix;
}
/**
@ -119,46 +122,10 @@ public final class ${className} {
/**
* Returns the pre-release version.
*
* @param isHyphen Prepend a hyphen, if <code>true</code>.
* @return The pre-release version, if any.
* @return The pre-release version, if any
*/
public static String getPreRelease(final boolean isHyphen) {
if (prerelease.length() > 0) {
if (isHyphen) {
return '-' + prerelease;
} else {
return prerelease;
}
}
return "";
}
/**
* Returns the pre-release version.
*
* @return The pre-release version, if any.
*/
public static String getPreRelease() {
return getPreRelease(false);
}
/**
* Returns the build metadata.
*
* @param isPlus Prepend a plus sign, if <code>true</code>.
* @return The build metadata, if any.
*/
public static String getBuildMetadata(final boolean isPlus) {
if (buildmeta.length() > 0) {
if (isPlus) {
return '+' + buildmeta;
} else {
return buildmeta;
}
}
return "";
public static int getPreRelease() {
return prerelease;
}
/**
@ -167,6 +134,53 @@ public final class ${className} {
* @return The build metadata, if any.
*/
public static String getBuildMetadata() {
return getBuildMetadata(false);
return buildmeta;
}
/**
* Returns the pre-release version with default prefix.
*
* @return The pre-release version, if any.
*/
public static String getPreReleaseWithPrefix() {
return getPreReleaseWithPrefix(DEFAULT_PRERELEASE_PREFIX);
}
/**
* Returns the pre-release version.
*
* @param prefix The refix 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;
} else {
return prerelease;
}
}
/**
* Returns the build metadata with default prefix.
*
* @param prefix The prefix to prepend.
* @return The build metadata, if any.
*/
public static String getBuildMetadataWithPrefix() {
return getBuildMetadataWithPrefix(DEFAULT_PRERELEASE_PREFIX);
}
/**
* Returns the build metadata.
*
* @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;
} else {
return buildmeta;
}
}
}