2

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

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • 1
    Only groovy can call `execute()` on a String, see this question for the kotlin alternatives https://stackoverflow.com/questions/35421699/how-to-invoke-external-command-from-within-kotlin-code – Leonard Brünings Jul 08 '22 at 21:23

4 Answers4

2

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*.*.*\""

user16358266
  • 397
  • 2
  • 7
  • Thanks, that helped me resolve my issue with the escape, I was thinking it was something like that. However, I have go a issue with `execute` as that might not exist in kotlin KTS. I have updated my question – ant2009 Jul 08 '22 at 15:27
1

Try this, however it might not split out as it is currently with groovy, as you would have to create the function runCommand to split the string:

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
    }
}

fun String.runCommand(workingDir: File): String? {
    ...
}
djmonki
  • 3,020
  • 7
  • 18
  • I got an `unreferenced runCommand` Is there an import that I should use? However, I have answered my own question below. – ant2009 Jul 11 '22 at 01:34
  • I missed off part of my answer, there was no library, it was about creating your own function to work out the split of the substring. Your answer is exactly what is required, was not aware that library existed, definitely one to keep a mind of and would have made things a lot simpler a while ago. Upvote on your answer. – djmonki Jul 11 '22 at 13:11
1

This should do the trick:

import java.io.ByteArrayOutputStream

fun getVersionFromGit(fallback: String) =
    try {
        ByteArrayOutputStream().use { out ->
            exec {
                commandLine = listOf("git", "describe", "--tags", "--abbrev=0", "--match", "v*.*.*")
                standardOutput = out
            }.assertNormalExitValue()
            out.toString().substring(1).trim()
        }
    } catch (e: Throwable) {
        println("Skipping git version")
        fallback
    }

task("demo") {
    println(getVersionFromGit("oops"))
}

Tested on MacOS and Windows.

Pierre Carrier
  • 396
  • 2
  • 9
-1

By using import org.codehaus.groovy.runtime.ProcessGroovyMethods

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