Is there any way to disable some tasks without con...
# plugin-development
g
Is there any way to disable some tasks without configuring all tasks eargerly? I'm using something like this:
Copy code
tasks.matching { it.name.contains(specificName) }
    .configureEach { it.enabled = false }
It works, but it makes all tasks eagerly configured just to find out their name.
c
something like this may work:
Copy code
tasks.withType<Task>().configureEach {
	enabled = !name.contains("foo")
}
this variation better reflects your use case:
Copy code
tasks.withType<Task>().configureEach {
  if( name.contains(specificName)) {
	  enabled = false
  }
}
g
Nice, however, I must ask, how much less work does Gradle have to do with
tasks.withType<Task>.configureEach
vs
tasks.matching
? From outside, it seems like they are doing the same amount of work 😅
c
they are doing the same thing - comparing names, just using different approaches. In the latter case it maintains laziness.
g
Awesome, thanks for your help, sir
👍 1
c
there’s a slightly improved variant, yields same results:
Copy code
tasks.configureEach {
  if( name.contains(specificName)) {
	  enabled = false

  }
}
…calling configureEach on all tasks isn’t often used, but is appropriate in this case. Skips matching the type of each task (which will be a Task anyhow).
thank you 1
Turns out rolling this yourself isn’t all that much code:
Copy code
fun TaskCollection<Task>.lazyMatch(
    predicate: (Task).() -> Boolean,
    action: Action<Task>
) {
    configureEach {
        if (predicate(this)) {
            action.execute(this)
        }
    }
}
Copy code
tasks.lazyMatch({ name.contains("foo") }) {
            enabled = false
        }