Created
June 25, 2020 21:42
-
-
Save ghale/a6ca91d034333ae4de6a227154a3ccba to your computer and use it in GitHub Desktop.
Capture Kotlin processor options as custom value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field | |
tasks.withType(getKaptWithoutKotlincTaskClass()).configureEach { task -> | |
doFirst { | |
buildScan.value "${task.path}.processorOptions", getCompilerPluginOptions(task).collect { "${it.key} = ${it.value}" }.join("\n") | |
} | |
} | |
static def getCompilerPluginOptions(Task task) { | |
def processorOptionsField = getAccessibleField(task.class, "processorOptions") | |
def compilerPluginOptions = processorOptionsField.get(task) | |
return compilerPluginOptions.subpluginOptionsByPluginId['org.jetbrains.kotlin.kapt3'] | |
} | |
static Class<?> getKaptWithoutKotlincTaskClass() { | |
return Class.forName("org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask") | |
} | |
private static Field getAccessibleField(Class<?> clazz, String fieldName) { | |
for (Field field : clazz.declaredFields) { | |
if (field.name == fieldName) { | |
field.setAccessible(true) | |
return field | |
} | |
} | |
if (clazz.superclass != null) { | |
return getAccessibleField(clazz.superclass, fieldName) | |
} else { | |
throw new RuntimeException("Field '${fieldName}' not found") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment