Version file is now written directly into Kotlin's generated source target directory.

This commit is contained in:
Erik C. Thauvin 2019-03-30 17:33:16 -07:00
parent 42ce43ad46
commit 421f8deff5
2 changed files with 31 additions and 15 deletions

View file

@ -79,6 +79,10 @@ public final class Constants {
* The empty string. * The empty string.
*/ */
public static final String EMPTY = ""; public static final String EMPTY = "";
/**
* The generated source files directory kotlin option.
*/
public static final String KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated";
/** /**
* The build metadata property key. * The build metadata property key.
*/ */
@ -108,6 +112,7 @@ public final class Constants {
*/ */
public static final String KOTLIN_TYPE = "kt"; public static final String KOTLIN_TYPE = "kt";
/** /**
* Disables the default constructor. * Disables the default constructor.
* *

View file

@ -44,10 +44,10 @@ 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.FileObject; import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@ -68,7 +68,7 @@ public class VersionProcessor extends AbstractProcessor {
} }
private void error(final String s, final Throwable t) { private void error(final String s, final Throwable t) {
messager.printMessage(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s));
} }
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN")
@ -108,7 +108,7 @@ public class VersionProcessor extends AbstractProcessor {
findOrRead = "read"; findOrRead = "read";
} }
error("Could not " + findOrRead + ": " + propsFile); error("Could not " + findOrRead + ": " + propsFile);
throw new FileNotFoundException("The system cannot " + findOrRead + " the specified file: `" throw new FileNotFoundException("Could not " + findOrRead + " the specified file: `"
+ propsFile.getAbsolutePath() + '`'); + propsFile.getAbsolutePath() + '`');
} }
} }
@ -188,6 +188,7 @@ public class VersionProcessor extends AbstractProcessor {
} else { } else {
template = version.template(); template = version.template();
} }
writeTemplate(version.type(), versionInfo, template); writeTemplate(version.type(), versionInfo, template);
} catch (IOException e) { } catch (IOException e) {
error("IOException occurred while running the annotation processor: " + e.getMessage(), e); error("IOException occurred while running the annotation processor: " + e.getMessage(), e);
@ -203,7 +204,9 @@ public class VersionProcessor extends AbstractProcessor {
} }
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN")
private void writeTemplate(final String type, final VersionInfo versionInfo, final String template) private void writeTemplate(final String type,
final VersionInfo versionInfo,
final String template)
throws IOException { throws IOException {
final MustacheFactory mf = new DefaultMustacheFactory(); final MustacheFactory mf = new DefaultMustacheFactory();
final Mustache mustache = mf.compile(template); final Mustache mustache = mf.compile(template);
@ -222,20 +225,28 @@ public class VersionProcessor extends AbstractProcessor {
} }
note("Loaded template: " + templateName); note("Loaded template: " + templateName);
final FileObject jfo;
final String fileName = versionInfo.getClassName() + '.' + type; final String fileName = versionInfo.getClassName() + '.' + type;
if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) { if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) {
jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT, versionInfo.getPackageName(), final Map<String, String> options = processingEnv.getOptions();
fileName); final String kaptGenDir = options.get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME);
if (kaptGenDir == null) {
throw new IOException("Could not find the target directory for generated Kotlin files.");
}
final File versionFile = new File(kaptGenDir, fileName);
versionFile.getParentFile().mkdirs();
try (final FileWriter fw = new FileWriter(versionFile);) {
mustache.execute(fw, versionInfo).flush();
}
note("Generated source: " + fileName + " (" + versionFile.getParentFile().getAbsolutePath() + ')');
} else { } else {
jfo = filer.createSourceFile(versionInfo.getPackageName() + '.' + versionInfo.getClassName()); final FileObject jfo = filer.createSourceFile(versionInfo.getPackageName() + '.'
+ versionInfo.getClassName());
try (final Writer writer = jfo.openWriter()) {
mustache.execute(writer, versionInfo).flush();
}
note("Generated source: " + fileName + " ("
+ new File(jfo.getName()).getAbsoluteFile().getParent() + ')');
} }
try (final Writer writer = jfo.openWriter()) {
mustache.execute(writer, versionInfo).flush();
}
note("Generated source: " + fileName + " (" + new File(jfo.getName()).getAbsoluteFile().getParent() + ')');
} }
} }