I need some help refactoring poorly written Gradle...
# plugin-development
k
I need some help refactoring poorly written Gradle code. Suppose I have this:
Copy code
val taskA = tasks.named("A")
tasks.register("B") {
  onlyIf { taskA.get().onlyIf.isSatisfiedBy(this) }
}
I think the intent is that B has the same
onlyIf
conditions as A, whatever they they may be, but the issue is that
onlyIf
appears to be internal API even though the compiler doesn't flag it. Any advice on a redesign?
a
is taskA's onlyIf defined by you? If so, you could extract it into a common value, e.g.
Copy code
val sharedOnlyIf = Spec { ... }

taskA.configure {
  onlyIf(sharedOnlyIf)
}

taskB.configure {
  onlyIf(sharedOnlyIf)
}
k
Not defined by me, unfortunately.
a
Hmmm okay
does taskB need to be a task? Could it be a doLast {} action of taskA?
k
There are examples of B being complex tasks in their own right, and A being finalized by B.
o
I'm unsure if it will keep working in the future, but you could consider
onlyIf { taskA.get().didWork }
. I think the timing of the
onlyIf
evaluation is late enough for that.