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

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

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

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

@Serializable
class RuleDsl : DslBase() {
    @SerialName("if")
    var ifCondition: String? = null
    var changes: MutableSet? = null
    var exists: MutableSet? = null

    @SerialName("allow_failure")
    var allowFailure: Boolean? = null
    @SerialName("when")
    var whenRun: WhenRunType? = null

    @SerialName("start_in")
    var startIn: Duration? = null

    fun changes(vararg elements: String) = changes(elements.toList())
    fun changes(elements: Iterable) = ensureChanges().addAll(elements)

    fun exists(vararg elements: String) = exists(elements.toList())
    fun exists(elements: Iterable) = ensureExists().addAll(elements)

    override fun validate(errors: MutableList) {
        addError(errors, startIn != null && whenRun != WhenRunType.DELAYED, "[rule] startIn can be used only with when=delayed jobs")
    }

    private fun ensureChanges() = changes ?: mutableSetOf().also { changes = it }
    private fun ensureExists() = exists ?: mutableSetOf().also { exists = it }

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

fun createRule(block: RuleDsl.() -> Unit = {}) = RuleDsl().apply(block)

@Serializable(with = RuleListDsl.RuleListDslSerializer::class)
class RuleListDsl : DslBase() {
    private val rules: MutableList = mutableListOf()

    fun rule(block: RuleDsl.() -> Unit) = addAndReturn(rules, RuleDsl().apply(block))
    operator fun RuleDsl.unaryPlus() = [email protected](this)

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

    object RuleListDslSerializer : ValueSerializer>(RuleDsl.serializer().list, RuleListDsl::rules)
    companion object {
        init {
            addSerializer(RuleListDsl::class, serializer())
        }
    }
}

fun createRules(block: RuleListDsl.() -> Unit) = RuleListDsl().apply(block)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy