我使用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
构造上做错了,但找不到确切的错。
答案 0 :(得分:5)
我明白了。
实际上,我很近。以下代码可以按我的意愿工作:
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"
}
...
}
}
}