ben-manes / gradle-versions-plugin

Gradle plugin to discover dependency updates
Apache License 2.0
3.82k stars 199 forks source link

How can i migrate groovy to kotlin dsl? #764

Closed VahidGarousi closed 1 year ago

VahidGarousi commented 1 year ago

my groovy code:

apply plugin: "com.github.ben-manes.versions"

def isNonStable = {  version ->
    def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
    def regex = /^[0-9,.v-]+(-r)?$/
    return !stableKeyword && !(version ==~ regex)
}

tasks.named("dependencyUpdates").configure {
    rejectVersionIf {
        isNonStable(it.candidate.version)
    }
    outputFormatter = "html"
    gradleReleaseChannel = "current"
}

my kotlin dsl code:

import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

apply(plugin = "com.github.ben-manes.versions")

fun String.isNonStable(): Boolean {
    val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { toUpperCase().contains(it) }
    val regex = "^[0-9,.v-]+(-r)?$".toRegex()
    val isStable = stableKeyword || regex.matches(this)
    return isStable.not()
}

tasks.named<com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask>("dependencyUpdates") {
    rejectVersionIf {
        isNonStable(it.candidate.version)
    }
    outputFormatter = DependencyUpdatesTask.OutputFormatter.HTML
    gradleReleaseChannel = "current"
}

Error :

e: ..\buildsrcripts\versionManager.gradle.kts:1:19: Unresolved reference: benmanes
ben-manes commented 1 year ago

Please try our kotlin example to debug your differences.

VahidGarousi commented 1 year ago

I can't do that because i have separate file for versionUpdates which its name is versionManager.groovy and i apply it in settings.groovy.kts subProjects with this

subprojects {
    apply(from = "$rootDir/buildsrcripts/versionManager.gradle.kts")
}

i can't figure our what i actually happening there

ben-manes commented 1 year ago

Oh, we’ll script plugins are on a different ClassLoader iirc, so you have to add the plugin to the buildscript classpath per script file. It’s a weird and annoying quirk.

The newer approach is an includeBuild of your own plugin, which install 3rd party as conventions. I think junit5 and others use that, but I haven’t in my own builds yet.