org.jetbrains.kotlin.gradle.plugin.StatisticsBuildFlowManager.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.plugin
import org.gradle.api.Project
import org.gradle.api.flow.*
import org.gradle.api.provider.Property
import org.gradle.api.services.ServiceReference
import org.gradle.api.tasks.Input
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.report.BuildMetricsService
import org.jetbrains.kotlin.gradle.report.BuildScanExtensionHolder
import javax.inject.Inject
internal abstract class StatisticsBuildFlowManager @Inject constructor(
private val flowScope: FlowScope,
private val flowProviders: FlowProviders,
) {
companion object {
fun getInstance(project: Project) =
project.objects.newInstance(StatisticsBuildFlowManager::class.java)
}
fun subscribeForBuildResult() {
flowScope.always(
BuildFinishFlowAction::class.java
) { spec ->
spec.parameters.buildFailed.set(flowProviders.buildWorkResult.map { it.failure.isPresent })
}
}
fun subscribeForBuildScan(buildScanHolder: BuildScanExtensionHolder) {
flowScope.always(
BuildScanFlowAction::class.java
) { spec ->
spec.parameters.buildScanExtensionHolder.set(buildScanHolder)
}
}
}
internal class BuildScanFlowAction : FlowAction {
interface Parameters : FlowParameters {
@get:ServiceReference
val buildMetricService: Property
@get: Input
val buildScanExtensionHolder: Property
}
override fun execute(parameters: Parameters) {
parameters.buildMetricService.orNull?.addBuildScanReport(parameters.buildScanExtensionHolder.orNull)
}
}
internal class BuildFinishFlowAction : FlowAction {
interface Parameters : FlowParameters {
@get:ServiceReference
val buildFusServiceProperty: Property
@get:Input
val buildFailed: Property
}
override fun execute(parameters: Parameters) {
parameters.buildFusServiceProperty.orNull?.recordBuildFinished(parameters.buildFailed.get())
}
}