Slackbot
03/15/2023, 11:41 PMAdam
03/15/2023, 11:41 PMabstract class MyPluginExtension @Inject constructor(
objects: ObjectFactory
) {
val userProvidedConfig: ExtensiblePolymorphicDomainObjectContainer<UserProvidedConfig> =
objects.polymorphicDomainObjectContainer(UserProvidedConfig::class)
}
And here's some simple user-provided data that I want to capture. It's just a string property, that gets encoded later on. Users could provide their own subclass of UserProvidedConfig
if they needed.
abstract class UserProvidedConfig @Inject constructor (
private val configId: String
): Named {
abstract fun encode(): String
override fun getName(): String = configId
}
abstract class StringConfig : UserProvidedConfig("string-config") {
@get:Input
abstract val someValue: Property<String>
override fun encode(): String = someValue.orNull.toString()
}
So I create the extension, and register the NamedDomainObjectFactory
for the subclass
val myExtension = extensions.create<MyPluginExtension>("myExtension").apply {
userProvidedConfig.registerFactory(StringConfig::class.java) { objects.newInstance() }
}
Now, I want to re-use this data in a task. It also has a ExtensiblePolymorphicDomainObjectContainer
abstract class MyTask @Inject constructor(
objects: ObjectFactory,
) : DefaultTask() {
val userProvidedConfig: ExtensiblePolymorphicDomainObjectContainer<UserProvidedConfig> =
objects.polymorphicDomainObjectContainer(UserProvidedConfig::class)
}
But the problem is when I try to register the task, I want to re-use the values from the extension.
val myTask = tasks.register<MyTask>("myTask") {
// re-use the extension values
userProvidedConfig.addAllLater(provider { myExtension.userProvidedConfig })
}
But this leads to an error: this type is not known to this container. Known types are: (None)
I've tried accessing the createable types using the internal Gradle API, but I can't figure out how to do this type-safely
val createableTypes =
(myExtension.userProvidedConfig as org.gradle.api.internal.DefaultPolymorphicDomainObjectContainer<UserProvidedConfig>).createableTypes
createableTypes.forEach { createableType ->
userProvidedConfig.registerFactory(
// ERROR None of the following functions can be called with the arguments supplied.
createableType,
NamedDomainObjectFactory { name ->
myExtension.userProvidedConfig
.containerWithType(createableType)
.create(name)
}
)
}
Adam
03/15/2023, 11:50 PMVampire
03/16/2023, 12:11 AMaddAllLater
?Adam
03/16/2023, 12:22 AM.set()
functionVampire
03/16/2023, 12:24 AMabstract class MyTask @Inject constructor(
objects: ObjectFactory,
userProvidedConfig: ExtensiblePolymorphicDomainObjectContainer<UserProvidedConfig>
) : DefaultTask() {
val userProvidedConfig = userProvidedConfig
}
Adam
03/16/2023, 10:51 AMExtensiblePolymorphicDomainObjectContainer
instance, because then it means tasks could be registered arbitrarily, without needing the container. But that’s probably rare in my case.Adam
03/16/2023, 6:39 PMVampire
03/16/2023, 7:20 PMObjectFactory.domainObjectContainer()
Adam
03/16/2023, 8:45 PM