Created
June 18, 2022 15:44
-
-
Save jonnycaley/4c42200e197b548730cbd9257fda5ffc to your computer and use it in GitHub Desktop.
BuildTimeLoggerPlugin - register build reporter service
This file contains 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
class BuildTimeLoggerPlugin : Plugin<Project> { | |
override fun apply(project: Project) { | |
val gradle = project.gradle | |
val buildDurationService = registerBuildDurationService(gradle) | |
val buildTaskService = registerBuildTaskService(gradle) | |
registerBuildReporterService(gradle, buildDurationService, buildTaskService) | |
} | |
private fun registerBuildReporterService( | |
gradle: Gradle, | |
buildDurationService: Provider<BuildDurationService>, | |
buildTaskService: Provider<BuildTaskService> | |
): Provider<BuildReporterService> { | |
val registry = gradle.serviceRegistry()[BuildEventListenerRegistryInternal::class.java] | |
val buildReporterService = gradle.sharedServices.registerIfAbsent("build-reporter-service", BuildReporterService::class.java) { service -> | |
service.parameters.getBuildDurationServiceProvider().set(buildDurationService) | |
service.parameters.getBuildTaskServiceProvider().set(buildTaskService) | |
} | |
registry.onOperationCompletion(buildReporterService) // gives gradle a reason to instantiate the build service and call onClose at the end of the build | |
return buildReporterService | |
} | |
private fun registerBuildDurationService(gradle: Gradle) { ... } | |
private fun registerBuildTaskService(gradle: Gradle) { ... } | |
} | |
fun Gradle.serviceRegistry(): ServiceRegistry = (this as DefaultGradle).services |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment