Last active
May 31, 2022 17:09
-
-
Save maxirosson/b15a2d1d563ffc6a5deceb9d8e7eeb5e to your computer and use it in GitHub Desktop.
Fail every time a Kotlin warning is found, except for "is deprecated" warnings. https://medium.com/dipien/fail-your-build-on-kotlin-warnings-96c4d9b3fd33
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 org.gradle.api.Project | |
import org.gradle.api.internal.GradleInternal | |
import org.gradle.configurationcache.extensions.serviceOf | |
import org.gradle.internal.logging.events.operations.LogEventBuildOperationProgressDetails | |
import org.gradle.internal.operations.BuildOperationDescriptor | |
import org.gradle.internal.operations.BuildOperationListener | |
import org.gradle.internal.operations.BuildOperationListenerManager | |
import org.gradle.internal.operations.OperationFinishEvent | |
import org.gradle.internal.operations.OperationIdentifier | |
import org.gradle.internal.operations.OperationProgressEvent | |
import org.gradle.internal.operations.OperationStartEvent | |
val kotlinWarnings = mutableListOf<String>() | |
val nonKotlinWarnings = mutableListOf<String>() | |
val errors = mutableListOf<String>() | |
val buildOperationListener = object : BuildOperationListener { | |
override fun started(buildOperation: BuildOperationDescriptor, startEvent: OperationStartEvent) { } | |
override fun progress(operationIdentifier: OperationIdentifier, progressEvent: OperationProgressEvent) { | |
val log = progressEvent.details | |
if (log is LogEventBuildOperationProgressDetails) { | |
if (log.logLevel == LogLevel.WARN) { | |
if (log.message.contains("w:") && log.message.contains(".kt")) { | |
if (!log.message.contains("is deprecated") && !kotlinWarnings.contains(log.message)) { | |
kotlinWarnings.add(log.message) | |
} | |
} else { | |
if (!nonKotlinWarnings.contains(log.message)) { | |
nonKotlinWarnings.add(log.message) | |
} | |
} | |
} else if (log.logLevel == LogLevel.ERROR) { | |
if (!errors.contains(log.message)) { | |
errors.add(log.message) | |
} | |
} | |
} | |
} | |
override fun finished(buildOperation: BuildOperationDescriptor, finishEvent: OperationFinishEvent) { } | |
} | |
val gradleInternal = project.gradle as GradleInternal | |
val buildOperationListenerManager = gradleInternal.serviceOf() as BuildOperationListenerManager? | |
buildOperationListenerManager?.addListener(buildOperationListener) | |
project.gradle.buildFinished { buildResult -> | |
buildOperationListenerManager?.removeListener(buildOperationListener) | |
if (buildResult.failure == null && nonKotlinWarnings.isNotEmpty()) { | |
project.logger.warn("") | |
project.logger.warn("================================= Project Warnings Summary ================================================================================") | |
nonKotlinWarnings.forEach { project.logger.warn(it) } | |
project.logger.warn("===========================================================================================================================================") | |
} | |
if (kotlinWarnings.isNotEmpty()) { | |
project.logger.warn("") | |
project.logger.warn("====================================== Kotlin Warnings ====================================================================================") | |
kotlinWarnings.forEach { project.logger.warn(it) } | |
project.logger.warn("===========================================================================================================================================") | |
if (buildResult.failure == null) { | |
throw RuntimeException("Kotlin warning found") | |
} | |
} | |
if (buildResult.failure != null && errors.isNotEmpty()) { | |
project.logger.warn("") | |
project.logger.warn("================================= Project Errors Summary ==================================================================================") | |
errors.forEach { project.logger.error(it) } | |
project.logger.warn("===========================================================================================================================================") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment