Added semver.properties processor argument.

This commit is contained in:
Erik C. Thauvin 2019-04-18 23:42:04 -07:00
parent 37d33f1ea3
commit 7f172d0b90
4 changed files with 38 additions and 8 deletions

View file

@ -250,8 +250,15 @@ dependencies {
kapt(semverProcessor) kapt(semverProcessor)
compileOnly(semverProcessor) compileOnly(semverProcessor)
} }
kapt {
arguments {
arg("semver.properties", "$projectDir/version.properties")
}
}
``` ```
As of [Kotlin 1.2.30](https://blog.jetbrains.com/kotlin/2019/04/kotlin-1-3-30-released/#more-6991), when using `kapt` you must include the following in `gradle.properties`:
The arguments block is not required if `kapt` is configured to use the Gradle Worker API in `gradle.properties`:
```ini ```ini
kapt.use.worker.api=true kapt.use.worker.api=true

View file

@ -20,6 +20,12 @@ dependencies {
implementation(kotlin("stdlib")) implementation(kotlin("stdlib"))
} }
kapt {
arguments {
arg("semver.properties", "$projectDir/version.properties")
}
}
repositories { repositories {
mavenLocal() mavenLocal()
jcenter() jcenter()

View file

@ -138,6 +138,11 @@ public final class Constants {
*/ */
public static final String KOTLIN_TYPE = "kt"; public static final String KOTLIN_TYPE = "kt";
/**
* The argument used to specify the properties file location.
*/
public static final String SEMVER_PROPERTIES_ARG = "semver.properties";
/** /**
* Disables the default constructor. * Disables the default constructor.

View file

@ -42,6 +42,7 @@ import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager; import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
@ -69,6 +70,7 @@ import java.util.Set;
* @created 2016-01-13 * @created 2016-01-13
* @since 1.0 * @since 1.0
*/ */
@SupportedOptions({Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, Constants.SEMVER_PROPERTIES_ARG})
public class VersionProcessor extends AbstractProcessor { public class VersionProcessor extends AbstractProcessor {
private Filer filer; private Filer filer;
@ -82,13 +84,23 @@ public class VersionProcessor extends AbstractProcessor {
log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s));
} }
private String getEnv(String envOption, String defaultValue) {
if (processingEnv != null) { // null when testing.
final String prop = processingEnv.getOptions().get(envOption);
if (prop != null) {
return prop;
}
}
return defaultValue;
}
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN")
private VersionInfo findValues(final Version version) private VersionInfo findValues(final Version version)
throws IOException { throws IOException {
final VersionInfo versionInfo = new VersionInfo(version); final VersionInfo versionInfo = new VersionInfo(version);
if (version.properties().length() > 0) { if (version.properties().length() > 0) {
final File propsFile = new File(version.properties()); final File propsFile = new File(getEnv(Constants.SEMVER_PROPERTIES_ARG, version.properties()));
if (propsFile.isFile() && propsFile.canRead()) { if (propsFile.isFile() && propsFile.canRead()) {
note("Found properties: " + propsFile + " (" + propsFile.getAbsoluteFile().getParent() + ')'); note("Found properties: " + propsFile + " (" + propsFile.getAbsoluteFile().getParent() + ')');
@ -248,19 +260,19 @@ public class VersionProcessor extends AbstractProcessor {
final String fileName = versionInfo.getClassName() + '.' + type; final String fileName = versionInfo.getClassName() + '.' + type;
if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) { if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) {
final String kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); final String kaptGenDir = getEnv(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, null);
if (kaptGenDir == null) { if (kaptGenDir == null) {
throw new IOException("Could not find the target directory for generated Kotlin files."); throw new IOException("Could not find the target directory for generated Kotlin files.");
} }
final File versionFile = new File(kaptGenDir, fileName); final File ktFile = new File(kaptGenDir, fileName);
if (!versionFile.getParentFile().exists() && !versionFile.getParentFile().mkdirs()) { if (!ktFile.getParentFile().exists() && !ktFile.getParentFile().mkdirs()) {
note("Could not create target directory: " + versionFile.getParentFile().getAbsolutePath()); note("Could not create target directory: " + ktFile.getParentFile().getAbsolutePath());
} }
try (final OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(versionFile), try (final OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(ktFile),
StandardCharsets.UTF_8)) { StandardCharsets.UTF_8)) {
mustache.execute(osw, versionInfo).flush(); mustache.execute(osw, versionInfo).flush();
} }
note("Generated source: " + fileName + " (" + versionFile.getParentFile().getAbsolutePath() + ')'); note("Generated source: " + fileName + " (" + ktFile.getParentFile().getAbsolutePath() + ')');
} else { } else {
final FileObject jfo = filer.createSourceFile(versionInfo.getPackageName() + '.' final FileObject jfo = filer.createSourceFile(versionInfo.getPackageName() + '.'
+ versionInfo.getClassName()); + versionInfo.getClassName());