I don’t know if I can describe this in sufficient detail without constructing a repo that reproduces it, but I’m gonna try.
The broader context is that I’m trying to write an extension to simplify configuration and execution of test suites. There are different test drivers and different options for different test suites and I want to try to hide as much of that complexity as I can. So I wrote a TestSuitesExtension
that looks something like this:
interface TestSuitesExtension {
abstract val project: Property<Project>
abstract val platform: Property<String>
abstract val edition: Property<String>
// more properties
fun configureSuite(suite: String, language: String,
options: Map<String,String> = emptyMap()) {
// more stuff
val taskName = "${computeTheTaskName()}"
val thisSuite = project.get().tasks.register(taskName) {
doLast {
println("Hello, world")
}
}
}
Then I wrote a little ts.gradle.kts
to import and use it (yes, “ts” is a stupid name, I’m just trying to make something work, I’ll fix that later):
import ...
val extension = project.extensions.create<TestSuitesExtension>("ts")
extension.project.convention(project)
extension.platform.convention("")
extension.edition.convention("")
Both of those files are in buildSrc/src/main/kotlin/...
Then in my actual build.gradle.kts
, I use the plugin:
plugins {
id("ts")
}
configure the plugin:
ts {
platform = "java"
edition = "ee"
}
And I can run the task. Yay!
Now, In TestSuitesExtension.kt
I change my task so it can do something more useful than print hello world,:
val thisSuite = project.get().tasks.register<JavaExec>(taskName) {
classpath = ...
mainClass = ...
args(...)
}
And it won’t compile:
: file:///path/buildSrc/src/main/kotlin/TestSuitesExtension.kt:90:56 Type mismatch: inferred type is () -> Unit but Class<TypeVariable(T)!> was expected
If I move the task registration into the ts.gradle.kts
file, it will register, but then if I attempt to do reference layout.buildDirectory
or run mkdir(dir)
, it blows up in a different way:
e: file:///path/buildSrc/src/main/kotlin/ts.gradle.kts:21:1 Interface TestSuitesExtension captures the script class instance. Try to use class instead
I’m hoping this is enough detail to spot the place where I’m being an idiot