Kapt: Allow to disable info->warning mapping in logger (#KT-24985)

This commit is contained in:
Yan Zhulanow
2018-06-19 00:03:36 +03:00
parent 8b3aa3dc7f
commit 392460e426
3 changed files with 26 additions and 9 deletions
@@ -83,6 +83,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
companion object {
private val VERBOSE_OPTION_NAME = "kapt.verbose"
private val USE_WORKER_API = "kapt.use.worker.api"
private val INFO_AS_WARNINGS = "kapt.info.as.warnings"
const val KAPT_WORKER_DEPENDENCIES_CONFIGURATION_NAME = "kotlinKaptWorkerDependencies"
@@ -112,6 +113,10 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
return isWorkerAPISupported() && hasProperty(USE_WORKER_API) && property(USE_WORKER_API) == "true"
}
fun Project.isInfoAsWarnings(): Boolean {
return hasProperty(INFO_AS_WARNINGS) && property(INFO_AS_WARNINGS) == "true"
}
fun findMainKaptConfiguration(project: Project) = project.findKaptConfiguration(MAIN_KAPT_CONFIGURATION_NAME)
fun createAptConfigurationIfNeeded(project: Project, sourceSetName: String): Configuration {
@@ -332,6 +337,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
pluginOptions += SubpluginOption("useLightAnalysis", "${kaptExtension.useLightAnalysis}")
pluginOptions += SubpluginOption("correctErrorTypes", "${kaptExtension.correctErrorTypes}")
pluginOptions += SubpluginOption("mapDiagnosticLocations", "${kaptExtension.mapDiagnosticLocations}")
pluginOptions += SubpluginOption("infoAsWarnings", "${project.isInfoAsWarnings()}")
pluginOptions += FilesSubpluginOption("stubs", listOf(getKaptStubsDir()))
if (project.isKaptVerbose()) {
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.APT_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.CLASS_OUTPUT_DIR_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.CORRECT_ERROR_TYPES_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.INCREMENTAL_DATA_OUTPUT_DIR_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.INFO_AS_WARNINGS_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.JAVAC_CLI_OPTIONS_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.MAP_DIAGNOSTIC_LOCATIONS_OPTION
import org.jetbrains.kotlin.kapt3.Kapt3CommandLineProcessor.Companion.SOURCE_OUTPUT_DIR_OPTION
@@ -93,6 +94,9 @@ object Kapt3ConfigurationKeys {
val VERBOSE_MODE: CompilerConfigurationKey<String> =
CompilerConfigurationKey.create<String>(VERBOSE_MODE_OPTION.description)
val INFO_AS_WARNINGS: CompilerConfigurationKey<String> =
CompilerConfigurationKey.create<String>(INFO_AS_WARNINGS_OPTION.description)
@Deprecated("Use APT_MODE instead.")
val APT_ONLY: CompilerConfigurationKey<String> =
CompilerConfigurationKey.create<String>(APT_ONLY_OPTION.description)
@@ -147,6 +151,9 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
val VERBOSE_MODE_OPTION: CliOption =
CliOption("verbose", "true | false", "Enable verbose output", required = false)
val INFO_AS_WARNINGS_OPTION: CliOption =
CliOption("infoAsWarnings", "true | false", "Show information messages as warnings", required = false)
@Deprecated("Use APT_MODE_OPTION instead.")
val APT_ONLY_OPTION: CliOption =
CliOption("aptOnly", "true | false", "Run only annotation processing, do not compile Kotlin files", required = false)
@@ -172,7 +179,7 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
listOf(SOURCE_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION, JAVAC_CLI_OPTIONS_OPTION,
CLASS_OUTPUT_DIR_OPTION, VERBOSE_MODE_OPTION, STUBS_OUTPUT_DIR_OPTION, APT_ONLY_OPTION, APT_MODE_OPTION,
USE_LIGHT_ANALYSIS_OPTION, CORRECT_ERROR_TYPES_OPTION, ANNOTATION_PROCESSORS_OPTION, INCREMENTAL_DATA_OUTPUT_DIR_OPTION,
CONFIGURATION, MAP_DIAGNOSTIC_LOCATIONS_OPTION)
CONFIGURATION, MAP_DIAGNOSTIC_LOCATIONS_OPTION, INFO_AS_WARNINGS_OPTION)
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
when (option) {
@@ -221,9 +228,10 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
configuration.get(Kapt3ConfigurationKeys.APT_ONLY))
val isVerbose = configuration.get(Kapt3ConfigurationKeys.VERBOSE_MODE) == "true"
val infoAsWarnings = configuration.get(Kapt3ConfigurationKeys.INFO_AS_WARNINGS) == "true"
val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
?: PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, isVerbose)
val logger = MessageCollectorBackedKaptLogger(isVerbose, messageCollector)
val logger = MessageCollectorBackedKaptLogger(isVerbose, infoAsWarnings, messageCollector)
fun abortAnalysis() = AnalysisHandlerExtension.registerExtension(project, AbortAnalysisHandlerExtension())
@@ -292,6 +300,7 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
logger.info("Use light analysis: $useLightAnalysis")
logger.info("Correct error types: $correctErrorTypes")
logger.info("Map diagnostic locations: $mapDiagnosticLocations")
logger.info("Info as warnings: $infoAsWarnings")
paths.log(logger)
logger.info("Annotation processors: " + annotationProcessors.joinToString())
logger.info("Javac options: $apOptions")
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.kapt3.util
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
@@ -15,28 +16,29 @@ import java.io.StringWriter
class MessageCollectorBackedKaptLogger(
override val isVerbose: Boolean,
infoAsWarnings: Boolean = true,
val messageCollector: MessageCollector = PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, isVerbose)
) : KaptLogger {
private companion object {
val PREFIX = "[kapt] "
}
override val errorWriter = makeWriter(CompilerMessageSeverity.ERROR)
override val warnWriter = makeWriter(CompilerMessageSeverity.STRONG_WARNING)
override val infoWriter = makeWriter(CompilerMessageSeverity.WARNING)
override val errorWriter = makeWriter(ERROR)
override val warnWriter = makeWriter(STRONG_WARNING)
override val infoWriter = makeWriter(if (infoAsWarnings) WARNING else INFO)
override fun info(message: String) {
if (isVerbose) {
messageCollector.report(CompilerMessageSeverity.INFO, PREFIX + message)
messageCollector.report(INFO, PREFIX + message)
}
}
override fun warn(message: String) {
messageCollector.report(CompilerMessageSeverity.WARNING, PREFIX + message)
messageCollector.report(WARNING, PREFIX + message)
}
override fun error(message: String) {
messageCollector.report(CompilerMessageSeverity.ERROR, PREFIX + message)
messageCollector.report(ERROR, PREFIX + message)
}
override fun exception(e: Throwable) {
@@ -45,7 +47,7 @@ class MessageCollectorBackedKaptLogger(
e.printStackTrace(PrintWriter(writer))
writer.toString()
}
messageCollector.report(CompilerMessageSeverity.ERROR, PREFIX + "An exception occurred: " + stacktrace)
messageCollector.report(ERROR, PREFIX + "An exception occurred: " + stacktrace)
}
private fun makeWriter(severity: CompilerMessageSeverity): PrintWriter {