如何基于TeamCity Kotlin DSL中的现有步骤创建自定义构建步骤? [英] How to create custom build step based on existing one in TeamCity Kotlin DSL?
问题描述
我使用TeamCity Kotlin DSL 2018.1设置构建配置.我的settings.kts文件如下所示:
I use TeamCity Kotlin DSL 2018.1 to set up build configuration. My settings.kts file looks like this:
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"
}
...
}
}
}
它可以按预期工作,但是我要避免为每一步重复编写相同的重复参数.
It works as expected, but I want to avoid writing the same repeating parameters for every step over and over again.
我尝试编写函数,该函数将构建预先填充有默认值的构建步骤:
I tried to write function, which would construct build step pre-filled with default values:
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只会忽略以这种方式定义的构建步骤.
It compiles but doesn't work: TeamCity just ignores build steps, defined in this way.
很遗憾,官方文档不包含任何信息关于定制和扩展DSL.可能是我在Kotlin的() -> Unit
构造上做错了,但找不到确切的错误.
Unfortunately, official documentation doesn't contain any information about customizing and extending DSL. Probably, I'm doing something wrong with Kotlin's () -> Unit
construction, but can't find out what exactly is wrong.
推荐答案
我明白了.
实际上,我很近.以下代码可以按我的意愿工作:
Actually, I was close. The following code works just as I wanted:
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"
}
...
}
}
}
这篇关于如何基于TeamCity Kotlin DSL中的现有步骤创建自定义构建步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!