如何在TeamCity的Kotlin DSL中引用全局表达式?

时间:2019-12-13 00:35:28

标签: kotlin teamcity dsl

我正尝试使用myList(如下)中一个全局位置的名为settings.kts的字符串(选项)列表,因为我试图使其更易于维护,但我一直坚持收到此错误Expression is inaccessible from a nested class 'MySubProject'。我一直在阅读有关Kotlin以及如何使用它的信息,但我一直无法弄清楚。也许有Kotlin或TeamCity DSL经验的人可以帮助我?

import jetbrains.buildServer.configs.kotlin.v2019_2.project
import jetbrains.buildServer.configs.kotlin.v2019_2.Project

var myList = listOf("Option 1", "Option 2")

project {

        params {
            select("system.MyOption",
                    myList[0],
                    label = "My Option",
                    description = "It's an option.",
                    options = myList)
    }

    subProject(MySubProject)
}

object MySubProject : Project ({
    params {
        select("system.MySubOption",
                myList[0],
                label = "My Sub Option",
                description = "It's another option.",
                options = myList)
    }
})

1 个答案:

答案 0 :(得分:0)

所以我最终要做的是像这样进行对象声明:

object MyGlobalConfig {
    var myList = listOf("Option 1", "Option 2")
}

然后引用脚本其他部分中的值,如下所示:

project {

        params {
            select("system.MyOption",
                    MyGlobalConfig.myList[0],
                    label = "My Option",
                    description = "It's an option.",
                    options = MyGlobalConfig.myList)
    }

    subProject(MySubProject)
}

object MySubProject : Project ({
    params {
        select("system.MySubOption",
                MyGlobalConfig.myList[0],
                label = "My Sub Option",
                description = "It's another option.",
                options = MyGlobalConfig.myList)
    }
})

它有效,这是我能想到的最好的方法。