versioning icon indicating copy to clipboard operation
versioning copied to clipboard

Improve the experience and/or documentation on Kotlin

Open JavierSegoviaCordoba opened this issue 4 years ago • 2 comments

I am trying to use this one in Kotlin:

versioning {
   releaseMode = { nextTag, lastTag, currentTag, extension ->
       "${nextTag}"
   }
}

But I am not sure how to use it on Kotlin, I need to specify the types but it is impossible to know easily what are types of those properties:

versioning {
    releaseMode = { nextTag: String, lastTag: String, currentTag: String, extension: VersioningExtension ->
        println(lastTag) // this is not being printed
        lastTag
    }
}

I tried Any in all types (I hate it) but it doesn't print too.

JavierSegoviaCordoba avatar Sep 07 '21 16:09 JavierSegoviaCordoba

There are ways to do this in Kotlin. I'll document them as soon as I find some time. However, it might handier to have a more Kotlin friendly version of the plugin.

dcoraboeuf avatar Sep 08 '21 05:09 dcoraboeuf

It doesn't print because it doesn't get executed. If it would get executed, you would get a runtime error because of a type mismatch :wink:. For the releaseMode to get used you have to set releaseBuild = true and your current branch must be part of releases. The releaseMode parameter only accepts a String or a Groovy Closure, but your used syntax doesn't create a Groovy Closure in Kotlin.

Without extending the plugin you could use one of the KotlinClosureX helper classes of the Gradle Kotlin DSL, sadly these are only defined up to 3 parameters but in your case you need 4. However, it is trivial to create such a class yourself (you can define it directly in the build script):

class KotlinClosure4<in T : Any?, in U : Any?, in V : Any?, in W : Any?, R : Any>(
    val function: (T, U, V, W) -> R?,
    owner: Any? = null,
    thisObject: Any? = null
) : groovy.lang.Closure<R?>(owner, thisObject) {
    @Suppress("unused") // to be called dynamically by Groovy
    fun doCall(t: T, u: U, v: V, w: W): R? = function(t, u, v, w)
}

With that class you can create the required Groovy Closure like this:

versioning {
    releaseMode = KotlinClosure4<String?, String?, String?, VersioningExtension, String>({ nextTag, lastTag, currentTag, extension ->
        println(lastTag)
        lastTag
    })
}

sodevel avatar Sep 09 '21 13:09 sodevel