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

ru.tinkoff.plugins.buildmetrics.gradle.labels.GradleLabelsFactory.kt Maven / Gradle / Ivy

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

import org.gradle.StartParameter
import org.gradle.api.configuration.BuildFeatures
import org.gradle.api.invocation.Gradle
import ru.tinkoff.plugins.buildmetrics.api.factories.Factory
import ru.tinkoff.plugins.buildmetrics.api.labels.Label

/**
 * Labels:
 * - gradle_version;
 * - gradle_configuration_cache_enabled;
 * - gradle_requested_tasks;
 * - gradle_max_workers;
 */
class GradleLabelsFactory(
    gradle: Gradle,
    buildFeatures: BuildFeatures,
) : Factory.Labels {

    private val labels: List> = mutableListOf>().apply {
        add(Label(name = "gradle_version", value = gradle.gradleVersion))
        add(Label(name = "gradle_configuration_cache_enabled", value = buildFeatures.configurationCache.active.get()))
        addAll(rootStartParameterLabels(rootStartParameter = rootStartParameter(gradle = gradle)))
        addAll(startParametersLabels(startParameter = gradle.startParameter))
    }

    override fun create(): List> = labels

    private fun rootStartParameterLabels(rootStartParameter: StartParameter): List> = listOf(
        Label(
            name = "gradle_requested_tasks",
            value = rootStartParameter.taskNames.joinToString(separator = " ")
        ),
    )

    private fun startParametersLabels(startParameter: StartParameter): List> = listOf(
        // gradle_max_workers do not support configuration cache
        // it means that if configuration is got from cache then startParameter.maxWorkerCount will also be got from cache
        // even if you use command-line option --max-workers
        Label(name = "gradle_max_workers", value = startParameter.maxWorkerCount),
    )

    private fun rootStartParameter(gradle: Gradle): StartParameter = rootGradle(gradle).startParameter

    private fun rootGradle(gradle: Gradle): Gradle {
        var root: Gradle? = gradle
        while (root?.parent != null) {
            root = root.parent
        }
        return root ?: error("Failed to get root gradle.")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy