twisterrob
05/18/2024, 10:10 PMtasks.named("a").configure {
doLast {
if (tasks.named("b").map { it.state.failure != null }.get()) {
println("b failed")
}
}
}
twisterrob
05/18/2024, 10:10 PMtasks
pulls in an implicit Project
. I tried pulling a TaskProvider through the boundary, but then I get:
1 problem was found storing the configuration cache.
- Task `:a` of type `A`: cannot serialize object of type 'B', a subtype of 'org.gradle.api.Task', as these are not supported with the configuration cache.
twisterrob
05/18/2024, 10:11 PMTaskContainer
is not an injectable service:
abstract class TaskFailedValueSource : ValueSource<Boolean, TaskFailedValueSource.Params> {
@get:Inject abstract val taskContainer: TaskContainer
interface Params : ValueSourceParameters {
val taskName: Property<String>
}
override fun obtain(): Boolean =
taskContainer.getByName(parameters.taskName.get()).state.failure != null
}
twisterrob
05/18/2024, 10:11 PMtwisterrob
05/18/2024, 10:12 PMfalse
at that point.Vampire
05/19/2024, 9:28 AMtwisterrob
05/19/2024, 9:29 AMtwisterrob
05/19/2024, 4:00 PM// Note to reader: both registerIfAbsent and onTaskCompletion can be called many times, they're idempotent.
val taskStateService = project.gradle.sharedServices
.registerIfAbsent("taskState", TaskStateService::class.java) { }
serviceOf<BuildEventsListenerRegistry>().onTaskCompletion(taskStateService)
val bTaskPath = "${project.path}:b"
tasks.named("a").configure {
usesService(taskStateService)
doLast {
if (taskStateService.get().isFailed(bTaskPath)) {
println("b failed")
}
}
}
abstract class TaskStateService : BuildService<BuildServiceParameters.None>, OperationCompletionListener {
private val state: MutableMap<String, TaskFinishEvent> = mutableMapOf()
fun isFailed(taskPath: String): Boolean {
val state = state[taskPath] ?: error("Task ${taskPath} is not complete yet.")
return state.result is TaskFailureResult
}
override fun onFinish(event: FinishEvent) {
if (event is TaskFinishEvent) {
state[event.descriptor.taskPath] = event
}
}
}
Vampire
05/19/2024, 8:59 PMserviceOf
is internal API.twisterrob
05/19/2024, 9:01 PMtwisterrob
05/19/2024, 9:01 PM