All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pcimcioch.gitlabci.dsl.job.ServiceDsl.kt Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package pcimcioch.gitlabci.dsl.job

import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.list
import pcimcioch.gitlabci.dsl.DslBase
import pcimcioch.gitlabci.dsl.serializer.ValueSerializer

@Serializable
class ServiceDsl(
        var name: String? = null
) : DslBase() {
    var alias: String? = null
    var cmd: MutableList? = null
    var entrypoint: MutableList? = null

    fun cmd(vararg elements: String) = cmd(elements.toList())
    fun cmd(elements: Iterable) = ensureCmd().addAll(elements)

    fun entrypoint(vararg elements: String) = entrypoint(elements.toList())
    fun entrypoint(elements: Iterable) = ensureEntrypoint().addAll(elements)

    override fun validate(errors: MutableList) {
        addError(errors, isEmpty(name), "[service name='$name'] name '$name' is incorrect")
    }

    private fun ensureEntrypoint() = entrypoint ?: mutableListOf().also { entrypoint = it }
    private fun ensureCmd() = cmd ?: mutableListOf().also { cmd = it }

    companion object {
        init {
            addSerializer(ServiceDsl::class, serializer())
        }
    }
}

fun createService(name: String? = null, block: ServiceDsl.() -> Unit = {}) = ServiceDsl(name).apply(block)

@Serializable(with = ServiceListDsl.ServiceListDslSerializer::class)
class ServiceListDsl : DslBase() {
    private val services: MutableList = mutableListOf()

    fun service(name: String? = null, block: ServiceDsl.() -> Unit = {}) = addAndReturn(services, ServiceDsl(name).apply(block))
    operator fun ServiceDsl.unaryPlus() = [email protected](this)

    override fun validate(errors: MutableList) {
        addErrors(errors, "", services)
    }

    object ServiceListDslSerializer : ValueSerializer>(ServiceDsl.serializer().list, ServiceListDsl::services)
    companion object {
        init {
            addSerializer(ServiceListDsl::class, serializer())
        }
    }
}

fun createServices(block: ServiceListDsl.() -> Unit = {}) = ServiceListDsl().apply(block)
fun createServices(vararg elements: String, block: ServiceListDsl.() -> Unit = {}) = createServices(elements.toList(), block)
fun createServices(elements: Iterable, block: ServiceListDsl.() -> Unit = {}) = ServiceListDsl().apply { elements.forEach { service(it) } }.apply(block)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy