How to create custom build step based on existing one in TeamCity Kotlin DSL?


How to create custom build step based on existing one in TeamCity Kotlin DSL?



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"
}
...
}
}
}



It compiles but doesn't work: TeamCity just ignores build steps, defined in this way.



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.


() -> Unit




1 Answer
1



I got it.



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"
}
...
}
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API