【问题标题】:How to create custom build step based on existing one in TeamCity Kotlin DSL?如何根据 TeamCity Kotlin DSL 中的现有步骤创建自定义构建步骤?
【发布时间】:2018-06-30 18:35:39
【问题描述】:

我使用 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 构造中做错了,但无法找出究竟是什么问题。

【问题讨论】:

    标签: kotlin teamcity dsl


    【解决方案1】:

    我明白了。

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

    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 基于 Kotlin 的类型安全构建器机制。您可以阅读有关此机制的文档:kotlinlang.org/docs/type-safe-builders.html 如果您 google 一下,相信您也可以找到一些示例。
    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2012-02-22
    • 1970-01-01
    相关资源
    最近更新 更多