This message was deleted.
# plugin-development
s
This message was deleted.
t
Copy code
import org.gradle.api.Incubating
import org.gradle.api.flow.FlowAction
import org.gradle.api.flow.FlowParameters
import org.gradle.api.flow.FlowProviders
import org.gradle.api.flow.FlowScope
import org.gradle.api.invocation.Gradle
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.kotlin.dsl.support.serviceOf

/**
 * @since Gradle 8.1
 */
@Incubating
@Suppress("UnstableApiUsage")
fun Gradle.buildFlowFinished(action: (Throwable?) -> Unit) {
	serviceOf<FlowScope>().always(ExecuteAction::class.java) {
		parameters.action.set(action)
		val buildResult = serviceOf<FlowProviders>().buildWorkResult
		parameters.failure.set(buildResult.map { it.failure.orElse(null) })
	}
}

@Suppress("UnstableApiUsage")
private class ExecuteAction : FlowAction<ExecuteAction.Parameters> {
	interface Parameters : FlowParameters {
		@get:Input
		val action: Property<(Throwable?) -> Unit>

		@get:Input
		val failure: Property<Throwable>
	}

	override fun execute(parameters: Parameters) {
		parameters.action.get().invoke(parameters.failure.orNull)
	}
}
v
To start with from a very cursory look,
serviceOf
is not really public API but should be considered internal as far as I remember, even with it not being in an
*.internal.*
package. Other than that, I cannot say much as I didn't use `Flow`s at all so far. And of course it is Kotlin-only, due to being an extension function and the type of
action
not being
Action
.
t
Oh, good point about the action, I'll fix that, in the first iteration I had no param so couldn't use Action. The extension function is ok to call, it'll just look different in other languages. Re internal, good point, although my thinking is that if I can inject it via an intermediary boilerplate interface, then it should be fine to get a public service from the Gradle object too.
👌 1