[英]NullPointerException in unit tests after migrating from groovy to kotlin gradle script
[英]Migrating Groovy gradle to Kotlin gradle (ext missing, or closure conversion, not sure)
我有一些用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())
}
主腳本包括以下內容:
//apply from: 'git.gradle'
apply from: 'git.gradle.kts'
println gitHash() // works with Groovy, doesn't with Kotlin
現在,問題是,主腳本無法識別gitHash()方法,很可能是因為我無法通過ext閉包來暴露它。 與Groovy腳本相同,此方法似乎是該文件中的私有(或本地)。
據我所知,ext closure是我試圖整合的'project.extra'的簡寫。 而且,似乎典型的Groovy閉包在Kotlin中沒有等效物。 我卡在這里,不知道我還能嘗試什么。 歡迎任何想法。
UPDATE
附:
var gitHash: Closure<Any?> by extra
gitHash = closureOf<String> { }
gitHash.delegate = { gitHash() }
我可以在Groovy中使用它,如:
println gitHash.invoke()
但它不適用於Kotlin腳本...因為invoke()指向call()( https://github.com/gradle/gradle-script-kotlin/blob/master/src/main/kotlin/org/ gradle / script / lang / kotlin / GroovyInteroperability.kt這里的擴展方法)。 雖然我試圖將它用作閉包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")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.