This message was deleted.
# plugin-development
s
This message was deleted.
c
Use ‘Action’ instead of lambda parameter for compatibility with Kotlin and Gradle
👍 2
g
How can I do it, if I want to add a lambda configuration on
RepositoryHandler
?
c
Like this, following the same pattern as the Gradle code.
Copy code
fun RepositoryHandler.github(action: Action<GithubArtifactRepository>) { .. action.execute(..) .. }
From a caller perspective it’s the same:
github { … }
, and the same for Kotlin or Groovy DSLs.
g
thanks, it was that easy indeed..
👍 1
and how do you encode the return value? (In my case is
Unit
, but what about other types?)
c
The method returns the desired value, such as:
Copy code
fun RepositoryHandler.github(action: Action<GithubArtifactRepository>) : String { .. action.execute(..) .. }
Action
itself doesn’t have a return value, by design, as it’s intended to configure other objects.
👍 1
example:
Copy code
public fun ConfigurationContainer.createResolvable(
    name: String,
    action: Action<Configuration> = Action {}
): Configuration {
    var configuration = findByName(name)
    if (configuration == null) {
        configuration = create(name) {
            isCanBeConsumed = false
            isCanBeResolved = true
            isVisible = false
            action.execute(this)
        }
    }
    return configuration
}
👍 1
g
ok, next step now
Copy code
fun PublicationContainer.createGithubPublication(name: String = "maven",
                                                 block: MavenPublication.() -> Unit) {
    currentSnapshot = null
    create(name, block)
    currentSnapshot?.let {
        create(it.name, block).version = it.version
        currentSnapshot = null
    }
}
create
does accept a
String, Action<in Publication>
, however if I switch to
Copy code
fun PublicationContainer.createGithubPublication(name: String = "maven",
                                                 action: Action<MavenPublication>) {
    currentSnapshot = null
    create(name, action)
then
create
turns red
None of the following functions can be called with the arguments supplied.
take in account that
MavenPublication extends Publication
c
use this variant:
Copy code
<U extends T> U create(String name, Class<U> type, Action<? super U> configuration) throws InvalidUserDataException;
you need to provide the class of object to create
or use this from org.gradle.kotlin.dsl:
Copy code
create<MavenPublication>("javaLibrary")
Copy code
create<MavenPublication>("javaLibrary") {
   action.execute(this)
}
g
thanks Chris, that seemed to make the compiler happy
👍 1