I want to pass a Gradle closure to a custom task.
@CacheableTask
abstract class FooTask : DefaultTask() {
@get:Input
abstract val input: RegularFileProperty
@get:Input
abstract val renamer: Property<Action<Path>>
@TaskAction
fun generate() {
val path = input.get().asFile.toPath()
val result = renamer.get().execute(path)
logger.quiet("original: $path, final: $result")
}
}
The usage in a 'build.gradle.kts' would be something like...
tasks {
register<FooTask>("foo") {
group = "foo"
input.set(fooFile)
renamer.set { filename: String ->
filename.replace(Regex("(.*)\\bar"), "$1.baz")
}
}
}
I am, however, having difficulty getting it to work. For example, it appears that the Action is not serializable which it apparently needs to be.
I have been looking at how the Copy task does this.
Starting with the rename method in Copy task After digging around in the source I found how the Transformer class is used. The following appears to work in the fashion I wanted.
The usage in ‘build.gradle.kts’.
Enjoy!