将Groovy gradle迁移到Kotlin gradle(缺少ext或闭包转换,不确定)

将Groovy gradle迁移到Kotlin gradle(缺少ext或闭包转换,不确定),gradle,groovy,kotlin,Gradle,Groovy,Kotlin,我用groovy编写了一些Git助手: git.gradle: def gitHash() { def res = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim() def diff = 'git diff'.execute([], project.rootDir).text.trim() if (diff != null && diff.length() >

我用groovy编写了一些Git助手:

git.gradle:

def gitHash() {
    def res = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()

    def diff = 'git diff'.execute([], project.rootDir).text.trim()
    if (diff != null && diff.length() > 0) {
        res += "-dirty"
    }

    return res
}

// method needs to be converted to closure, in order to be visible outside of script
ext.gitHash =  { return gitHash();}
现在我把所有的东西都转换成Kotlin,看起来像这样:

git.gradle.kts:

import java.io.IOException
import java.util.concurrent.TimeUnit

fun gitHash() : String {
    var res = "git rev-parse --short HEAD".runCommand(project.rootDir)?.trim()

    val diff = "git diff".runCommand(project.rootDir)?.trim()
    if (diff != null && diff.isNotEmpty()) {
        res += "-dirty"
    }
    return res!!
}

fun String.runCommand(workingDir: File): String? {
    ...
}

// method needs to be converted to closure, in order to be visible outside of script
//ext.gitHash =  { return gitHash();} // <-- HERE'S THE PROBLEM

task("gitTask") { // <-- calling ./gradlew gitTask works
    println(gitHash())
}
现在,问题是,主脚本无法识别gitHash()方法,很可能是因为我无法通过ext闭包公开它。与Groovy脚本中相同,该方法在该文件中似乎是私有的(或本地的)

据我所知,extclosure是我试图集成的“project.extra”的缩写。而且,典型的Groovy闭包在Kotlin中似乎没有等价物。我被困在这里,不知道还能做什么。欢迎任何意见

更新

与:

但它不适用于Kotlin脚本。。。因为invoke()指向call()(这里是扩展方法)。当我试图使用它作为闭包
gitHash()
时,它会导致这样的错误:

Parameter specified as non-null is null: method org.gradle.script.lang.kotlin.KotlinClosure.doCall, parameter it 

看起来我遗漏了什么…

如果您仍在寻找解决方案,我建议您:

import java.io.ByteArrayOutputStream

inline
fun String.runCommand(workingDir: File): String {
    val command = this
    val stdout = ByteArrayOutputStream()
    project.exec {
        this.workingDir = workingDir
        this.commandLine = command.split(" ")
        this.standardOutput = stdout
    }
    return String(stdout.toByteArray()).trim()
}

task("gitHash") {
    var res = "git rev-parse --short HEAD".runCommand(project.rootDir)
    val diff = "git diff".runCommand(project.rootDir)
    if (!diff.isNullOrBlank()) { res += "-dirty" }
    println(res)
}
我试过了

。。或者更为惯用的说法:

inline
fun String.runCommand(workingDir: File): String {
    val stdout = ByteArrayOutputStream()
    project.exec {
        this.workingDir = workingDir
        this.commandLine = this@runCommand.split(" ")
        this.standardOutput = stdout
    }
    return String(stdout.toByteArray()).trim()
}

val commitHash by lazy { "git rev-parse --short HEAD".runCommand(project.rootDir) }
val workingCopyDiff by lazy { "git diff".runCommand(project.rootDir) }

val gitHash by tasks.creating {
    println(commitHash + if (workingCopyDiff.isBlank()) "" else "-dirty")
}

如果您仍在寻找解决方案,我建议您:

import java.io.ByteArrayOutputStream

inline
fun String.runCommand(workingDir: File): String {
    val command = this
    val stdout = ByteArrayOutputStream()
    project.exec {
        this.workingDir = workingDir
        this.commandLine = command.split(" ")
        this.standardOutput = stdout
    }
    return String(stdout.toByteArray()).trim()
}

task("gitHash") {
    var res = "git rev-parse --short HEAD".runCommand(project.rootDir)
    val diff = "git diff".runCommand(project.rootDir)
    if (!diff.isNullOrBlank()) { res += "-dirty" }
    println(res)
}
我试过了

。。或者更为惯用的说法:

inline
fun String.runCommand(workingDir: File): String {
    val stdout = ByteArrayOutputStream()
    project.exec {
        this.workingDir = workingDir
        this.commandLine = this@runCommand.split(" ")
        this.standardOutput = stdout
    }
    return String(stdout.toByteArray()).trim()
}

val commitHash by lazy { "git rev-parse --short HEAD".runCommand(project.rootDir) }
val workingCopyDiff by lazy { "git diff".runCommand(project.rootDir) }

val gitHash by tasks.creating {
    println(commitHash + if (workingCopyDiff.isBlank()) "" else "-dirty")
}

我在kotlin方面还不够熟练,但也许这个例子会对你有所帮助:@Opal不幸的是,不,我找到的最接近的是,但我想做的正好相反。这一个在Groovy端设置了额外功能,而在Kotlin端读取它。我试着在Kotlin端设置,在Groovy端阅读。好的,看起来设置值是这样做的:
var gitHash:String by extra
gitHash=“works”
问题仍然是,如何处理闭包…我看到很少有帮助,但现在
var gitHash:Closure by extra;gitHash=closureOf({gitHash()})
应根据此操作来完成工作。但是它不是,而是
var-gitHash:extra闭包;gitHash=closureOf{};gitHash.delegate={gitHash()}println gitHash.invoke()
中,调用就像一个符咒。总比没有好,但我不相信这是最好的方法:)第一个抛出这样的错误。。。指定为非null的参数为null:method org.gradle.script.lang.kotlin.KotlinClosure.doCall,参数itI在kotlin方面不够熟练,但这个示例可能会对您有所帮助:@Opal不幸的是,不,我找到的最接近的一个是,但我想做完全相反的事情。这一个在Groovy端设置了额外功能,而在Kotlin端读取它。我试着在Kotlin端设置,在Groovy端阅读。好的,看起来设置值是这样做的:
var gitHash:String by extra
gitHash=“works”
问题仍然是,如何处理闭包…我看到很少有帮助,但现在
var gitHash:Closure by extra;gitHash=closureOf({gitHash()})
应根据此操作来完成工作。但是它不是,而是
var-gitHash:extra闭包;gitHash=closureOf{};gitHash.delegate={gitHash()}println gitHash.invoke()
中,调用就像一个符咒。总比没有好,但我不相信这是最好的方法:)第一个抛出这样的错误。。。指定为非NULL的参数为NULL:方法Or.GrdL.Script .Lang.ktLink .KToLeCuturul.DoCurl,参数,也许您应该考虑使用插件来代替。它包含您突出显示的所有功能,并允许您将结果配置为ext属性或输出文件,可以使用spring actuator info端点呈现。我已经找到了一个使用buildSrc功能的讨厌的解决方案,而无需处理kotlin gradle插件问题。也许你应该考虑使用插件代替。它包含您突出显示的所有功能,并允许您将结果配置为ext属性或输出文件,可以使用spring actuator info端点呈现。我已经找到了一个使用buildSrc功能的讨厌的解决方案,而无需处理kotlin gradle插件问题。
inline
fun String.runCommand(workingDir: File): String {
    val stdout = ByteArrayOutputStream()
    project.exec {
        this.workingDir = workingDir
        this.commandLine = this@runCommand.split(" ")
        this.standardOutput = stdout
    }
    return String(stdout.toByteArray()).trim()
}

val commitHash by lazy { "git rev-parse --short HEAD".runCommand(project.rootDir) }
val workingCopyDiff by lazy { "git diff".runCommand(project.rootDir) }

val gitHash by tasks.creating {
    println(commitHash + if (workingCopyDiff.isBlank()) "" else "-dirty")
}