My exposure to Kotlin is via Teamcity DSL. I hope the questions are the on-topic here. If not, please let me know.
This article on DSL refactoring advises that teamcity parameters can be moved to a Kotlin Singleton - https://blog.jetbrains.com/teamcity/2021/04/kotlin-dsl-for-beginners-recommended-refactorings/
I have some parameters which are set and used as environment variables. How can I refactor them to Kotlin singletons and use them from environment variables
params {
param("env.NOTARIZE_MAC_BUILD_DIR", "/Users/apple/build")
param("env.NOTARIZE_MAC_GON_PATH", "/Users/apple/bin/gon")
}
The example uses a parameter with a small set of possible values which is best represented as an enum in Kotlin.
For an example that will better match what you are looking for:
Replace
//Definition
enum class DockerImagePostfix {
DEV, STAGING, PROD
}
//Usage
namesAndTags = "myName-${DockerImagePostfix.STAGING}:latest"
with just a val on its own
//Definition
val dockerImagePostfix = "STAGING"
//Usage
namesAndTags = "myName-$dockerImagePostfix:latest"
or values grouped into an object (Kotlin’s singleton):
//Definition
object DockerImage {
const val POSTFIX = "STAGING"
}
//Usage
namesAndTags = "myName-${DockerImage.POSTFIX}:latest"
Note: If you are wondering about the casing, Kotlin constants are UPPER_CASE with underlines and other properties are camelCase by convention but it’s only a convention, it has no behavior impact.