[英]Converting some code snippet from groovy to kotlin KTS in my build.gradle.kts file
I have the following code in my build.gradle.kts.我的 build.gradle.kts 中有以下代码。 I have now migrated to kotlin KTS.
我现在已经迁移到 kotlin KTS。 And need help on translating this code from groovy to kotlin script.
并且需要帮助将此代码从 groovy 转换为 kotlin 脚本。
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
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
最新的错误是
Unresolved reference: execute
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*.*.*\""
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:试试这个,但是它可能不会像当前使用 groovy 那样拆分,因为您必须创建函数 runCommand 来拆分字符串:
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? {
...
}
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.在 MacOS 和 Windows 上测试。
By using import org.codehaus.groovy.runtime.ProcessGroovyMethods
通过使用
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.