Configuration 如何在Kotlin for TeamCity的自定义构建步骤中隐藏参数?

Configuration 如何在Kotlin for TeamCity的自定义构建步骤中隐藏参数?,configuration,teamcity,kotlin,dsl,Configuration,Teamcity,Kotlin,Dsl,我正在尝试设置TeamCity,使用配置作为Kotlin的代码。我正在为buildsteps编写包装器,这样我就可以隐藏默认的公开配置,只公开重要的参数。这将允许我防止类的用户更改会导致生成错误的值 我想要这个: steps { step { name = "Restore NuGet Packages" type = "jb.nuget.installer" param("nuget.path", "%teamcity.tool.NuGe

我正在尝试设置TeamCity,使用配置作为Kotlin的代码。我正在为buildsteps编写包装器,这样我就可以隐藏默认的公开配置,只公开重要的参数。这将允许我防止类的用户更改会导致生成错误的值

我想要这个:

steps {
    step {
        name = "Restore NuGet Packages"
        type = "jb.nuget.installer"
        param("nuget.path", "%teamcity.tool.NuGet.CommandLine.3.3.0%")        
        param("nuget.updatePackages.mode", "sln")
        param("nuget.use.restore", "restore")
        param("sln.path", "path_to_solution") //parameter here
        param("toolPathSelector", "%teamcity.tool.NuGet.CommandLine.3.3.0%")
}
…就是这样:

MyBuildSteps.buildstep1("path_to_solution")
以下是步骤的函数签名:

public final class BuildSteps {
    public final fun step(base: BuildStep?, init: BuildStep.() -> Unit ): Unit { /* compiled code */ }
}
这就是我所尝试的:

class MyBuildSteps {
fun restoreNugetPackages(slnPath: String): kotlin.Unit {
    var step: BuildStep = BuildStep {
        name = "Restore NuGet Packages"
        type = "jb.nuget.installer"
    }

    var stepParams: List = Parametrized {
        param("build-file-path", slnPath)
        param("msbuild_version", "14.0")
        param("octopus_octopack_package_version", "1.0.0.%build.number%")
        param("octopus_run_octopack", "true")
        param("run-platform", "x86")
        param("toolsVersion", "14.0")
        param("vs.version", "vs2015")
    }

    return {
        step.name
        step.type
        stepParams
    } //how do I return this?
  }
}

任何建议都将不胜感激

我假设您想用参数
slnPath
步骤{…}
封装到函数
buildstep1

使用此函数签名并将
步骤{…}
零件复制粘贴到内部。添加您认为合适的任何参数:

fun BuildSteps.buildstep1(slnPath: String) {
    step {
        name = "Restore NuGet Packages"
        type = "jb.nuget.installer"
        param("nuget.path", "%teamcity.tool.NuGet.CommandLine.3.3.0%")        
        param("nuget.updatePackages.mode", "sln")
        param("nuget.use.restore", "restore")
        param("sln.path", slnPath) // your parameter here
        param("toolPathSelector", "%teamcity.tool.NuGet.CommandLine.3.3.0%")
    }
}
就这些!使用它而不是
步骤{…}
构造:

steps {
    buildstep1("path_to_solution")
}

此函数可以在配置文件中的任何位置声明(我通常将其放在底部)或在单独的
.kts
文件中声明并导入(理论上)。

步骤
属于哪个类?嗨,沃丹,它属于公共最终类构建步骤{谢谢,我更新了问题和答案非常感谢你,沃丹,这非常有效!感谢你的kotlin知识我还有一个问题,“BuildSteps.buildstep1”语法结构对我来说是新的,这里到底发生了什么?这是一个叫做“扩展函数”的东西。它将上下文从呼叫站点转移到函数中。这是Kotlin的一个基本功能,您可以从中学习。我没有在答案中包含解释,因为学习如何制作DSL占Kotlin的80%。您将比只掌握DSL功能更好地掌握这门语言。