Home > Net >  Converting some code snippet from groovy to kotlin KTS in my build.gradle.kts file
Converting some code snippet from groovy to kotlin KTS in my build.gradle.kts file

Time:07-11

I have the following code in my build.gradle.kts. I have now migrated to kotlin KTS. And need help on translating this code from groovy to kotlin script.

fun getVersionFromGit(fallback: String): String {
    return try {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            "git describe --tags --abbrev=0 --match "v*.*.*"".execute().text.substring(1).trim()
        } else {
            ["sh , "-c"", "git describe --tags --abbrev=0 --match "v*.*.*""].execute().text.substring(1).trim()
        }
    } catch (e: Throwable) {
        println("Skipping git version")
        fallback
    }
}

I am getting errors

Unexpected tokens (use ';' to separate expressions on the same line) Unsupported [literal prefixes and suffixes] Unresolved reference: v Expecting an element

Many thanks in advance

UPDATE:

fun getVersionFromGit(fallback: String): String {
    return try {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            "git describe --tags --abbrev=0 --match \"v*.*.*\"".execute().text.substring(1).trim()
        } else {
            listOf("sh , \"-c\"", "git describe --tags --abbrev=0 --match \"v*.*.*\"").execute().text.substring(1).trim()
        }
    } catch (e: Throwable) {
        println("Skipping git version")
        fallback
    }
}

The latest error is Unresolved reference: execute

CodePudding user response:

Looks like you didn't properly escape the quotes in the strings.

"git describe --tags --abbrev=0 --match "v*.*.*""

should be

"git describe --tags --abbrev=0 --match \"v*.*.*\""

CodePudding user response:

Try this, however it might not split out as it is currently with groovy:

fun getVersionFromGit(fallback: String): String {
    return try {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            "git describe --tags --abbrev=0 --match \"v*.*.*\"".runCommand()?.trim()
        } else {
            listOf("sh , \"-c\"", "git describe --tags --abbrev=0 --match \"v*.*.*\"").runCommand()?.trim()
        }
    } catch (e: Throwable) {
        println("Skipping git version")
        fallback
    }
}
  • Related