All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.BuildMetricsPlugin.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
package ru.tinkoff.plugins.buildmetrics

import org.gradle.api.Plugin
import org.gradle.api.flow.FlowProviders
import org.gradle.api.flow.FlowScope
import org.gradle.api.initialization.Settings
import org.gradle.internal.build.event.BuildEventListenerRegistryInternal
import org.gradle.kotlin.dsl.always
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.registerIfAbsent
import ru.tinkoff.plugins.buildmetrics.api.extensions.BuildMetricsExtension
import ru.tinkoff.plugins.buildmetrics.internal.BuildMetricsFlowAction
import ru.tinkoff.plugins.buildmetrics.internal.BuildMetricsService
import javax.inject.Inject

@Suppress("unused")
abstract class BuildMetricsPlugin : Plugin {

    @get:Inject
    abstract val buildEventListenerRegistryInternal: BuildEventListenerRegistryInternal

    @get:Inject
    abstract val flowScope: FlowScope

    @get:Inject
    abstract val flowProviders: FlowProviders

    override fun apply(target: Settings) {
        val extension = target.extensions.create("buildMetrics")
        target.gradle.sharedServices.registerIfAbsent("buildMetricsService", BuildMetricsService::class) {
            parameters.metrics.set(extension.metrics.list)
            parameters.labels.set(extension.labels.list)
            parameters.metricsFactories.set(extension.metrics.factories)
            parameters.labelsFactories.set(extension.labels.factories)
            parameters.publishers.set(extension.publishers.list)
            parameters.buildHandlers.set(extension.buildHandlers.list)
        }.also { serviceProvider ->
            buildEventListenerRegistryInternal.onOperationCompletion(serviceProvider)

            target.gradle.settingsEvaluated {
                serviceProvider.get().also { service ->
                    service.setup(
                        metrics = extension.metrics.list,
                        labels = extension.labels.list,
                        metricsFactories = extension.metrics.factories,
                        labelsFactories = extension.labels.factories,
                        publishers = extension.publishers.list,
                        buildHandlers = extension.buildHandlers.list,
                    )
                }
            }
        }
        flowScope.always(BuildMetricsFlowAction::class) {
            parameters.buildWorkResult.set(flowProviders.buildWorkResult)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy