Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the experience and/or documentation on Kotlin #89

Open
JavierSegoviaCordoba opened this issue Sep 7, 2021 · 2 comments
Open

Comments

@JavierSegoviaCordoba
Copy link

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.

@dcoraboeuf
Copy link
Contributor

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.

@sodevel
Copy link

sodevel commented Sep 9, 2021

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 😉. 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
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants