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

View file

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

View file

@ -28,6 +28,9 @@ import java.util.Date;
* 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_BUILDMETA_PREFIX = "+";
private final static String buildmeta = "${buildmeta}"; private final static String buildmeta = "${buildmeta}";
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};
@ -86,7 +89,7 @@ public final class ${className} {
return Integer.toString(getMajor()) + '.' return Integer.toString(getMajor()) + '.'
+ Integer.toString(getMinor()) + '.' + Integer.toString(getMinor()) + '.'
+ Integer.toString(getPatch()) + Integer.toString(getPatch())
+ getPreRelease(true) + getBuildMetadata(true); + getPreReleaseWithPrefix + getBuildMetadataWithPrefix;
} }
/** /**
@ -119,47 +122,11 @@ public final class ${className} {
/** /**
* Returns the pre-release version. * 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) { public static int getPreRelease() {
if (prerelease.length() > 0) {
if (isHyphen) {
return '-' + prerelease;
} else {
return prerelease; 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 "";
}
/** /**
* Returns the build metadata. * Returns the build metadata.
@ -167,6 +134,53 @@ public final class ${className} {
* @return The build metadata, if any. * @return The build metadata, if any.
*/ */
public static String getBuildMetadata() { 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;
}
} }
} }