Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 643
Marat Safin Avatar asked Jun 30 '18 18:06

Marat Safin


1 Answers

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"
            }
            ...
        }
    }
}
like image 168
Marat Safin Avatar answered Nov 02 '22 14:11

Marat Safin