Extension configuration lifecycle/callbacks

Hi,

I am getting started with custom plugins and I am lost at how to approach this specific use case in an idiomatic way. My plugin takes input from an extension object and that input is used to configure another plugin’s extension.

This other plugin that I’m wrapping (Kotlin Multiplatform) however is written in such a way that it needs to be configured during the evaluation phase - as soon as the project is evaluated, it generates tasks and expects its extensions to be properly configured.

So I must configure it after my plugin’s extension has been configured, but before the whole project is evaluated. What is the idiomatic way of doing so?

My current workaround is to have my extension accept a listener, and set the listener from my plugin. This way I am able to know when the extension has been configured. However this doesn’t look like the correct way of doing things.

Thanks for helping! For context, this is my workaround:

open class ListenableExtension {
    private val wrapped = MyExtension()
    private var onConfigured: ((MyExtension) -> Unit)? = null

    internal fun onConfigured(action: (MyExtension) -> Unit) { // called by my plugin
        onConfigured = action
    }

    fun configure(action: MyExtension.() -> Unit) { // called by users
        action(wrapped)
        onConfigured?.invoke(wrapped)
    }
}

class MyExtension {
    var parameter1 = ""
    var parameter2 = ""
}

This way the plugin users will call:

myExtension {
    configure {
        parameter1 = "foo"
        parameter2 = "bar"
    }
}

And my plugin can do stuff when the configure method returns. Is there any better approach?