gpt4 book ai didi

kotlin - 如何基于 TeamCity Kotlin DSL 中的现有构建步骤创建自定义构建步骤?

转载 作者:行者123 更新时间:2023-12-05 00:12:19 29 4
gpt4 key购买 nike

我使用 TeamCity Kotlin DSL 2018.1 来设置构建配置。我的 settings.kts 文件如下所示:

version = "2018.1"

project {
buildType {
id("some-id")
name = "name"
steps {
ant {
name = "Step1"
targets = "target1"
mode = antFile { path = "/some/path" }
workingDir = "/some/dir"
jdkHome = "some_jdk"
}
ant {
name = "Step2"
targets = "target2"
mode = antFile { path = "/some/path" }
workingDir = "/some/dir"
jdkHome = "some_jdk"
}
...
}
}
}

它按预期工作,但我想避免为每一步一遍又一遍地编写相同的重复参数。

我尝试编写函数,该函数将构建预填充默认值的构建步骤:
fun customAnt(init: AntBuildStep.() -> kotlin.Unit): AntBuildStep {
val ant_file = AntBuildStep.Mode.AntFile()
ant_file.path = "/some/path"

val ant = AntBuildStep()
ant.mode = ant_file
ant.workingDir = "/some/dir"
ant.jdkHome = "some_jdk"
return ant
}
project {
buildType {
id("some-id")
name = "name"
steps {
customAnt {
name = "Step1"
targets = "target1"
}
customAnt {
name = "Step2"
targets = "target2"
}
...
}
}
}

它编译但不起作用:TeamCity 只是忽略以这种方式定义的构建步骤。

不幸的是, official documentation不包含任何关于定制和扩展 DSL 的信息。可能,我对 Kotlin 的 () -> Unit 做错了构造,但无法找出究竟是什么错误。

最佳答案

我知道了。

事实上,我很接近。以下代码按我的意愿工作:

version = "2018.1"

fun BuildSteps.customAnt(init: AntBuildStep.() -> Unit): AntBuildStep {
val ant_file = AntBuildStep.Mode.AntFile()
ant_file.path = "/some/path"

val result = AntBuildStep(init)
result.mode = ant_file
result.workingDir = "/some/dir"
result.jdkHome = "some_jdk"
step(result)
return result
}

project {
buildType {
steps {
customAnt {
name = "step1"
targets = "target1"
}
customAnt {
name = "step2"
targets = "target2"
}
...
}
}
}

关于kotlin - 如何基于 TeamCity Kotlin DSL 中的现有构建步骤创建自定义构建步骤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117570/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com