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.