Skip to content

Commit

Permalink
Added global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pcimcioch committed May 6, 2023
1 parent 00fb257 commit bf30d3e
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/main/kotlin/pcimcioch/gitlabci/dsl/GitlabCiDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import pcimcioch.gitlabci.dsl.default.DefaultDsl
import pcimcioch.gitlabci.dsl.include.IncludeDsl
import pcimcioch.gitlabci.dsl.job.GlobalVariableDsl
import pcimcioch.gitlabci.dsl.job.GlobalVariablesDsl
import pcimcioch.gitlabci.dsl.job.JobDsl
import pcimcioch.gitlabci.dsl.job.VariablesDsl
import pcimcioch.gitlabci.dsl.serializer.ValueSerializer
Expand All @@ -24,7 +26,7 @@ class GitlabCiDsl : DslBase() {
private var default: DefaultDsl? = null
private var workflow: WorkflowDsl? = null
private var include: IncludeDsl? = null
private var variables: VariablesDsl? = null
private var variables: GlobalVariablesDsl? = null

/**
* creates a Job, adds it to the GitlabCi and returns it.
Expand All @@ -48,8 +50,8 @@ class GitlabCiDsl : DslBase() {
fun include(vararg elements: String) = include(elements.toList())
fun include(elements: Iterable<String>) = ensureInclude().apply { elements.forEach { local(it) } }

fun variables(block: VariablesDsl.() -> Unit = {}) = ensureVariables().apply(block)
fun variables(elements: Map<String, Any>, block: VariablesDsl.() -> Unit = {}) =
fun variables(block: GlobalVariablesDsl.() -> Unit = {}) = ensureVariables().apply(block)
fun variables(elements: Map<String, GlobalVariableDsl>, block: GlobalVariablesDsl.() -> Unit = {}) =
ensureVariables().apply { elements.forEach { add(it.key, it.value) } }.apply(block)

fun pages(block: JobDsl.() -> Unit) = job("pages") {
Expand All @@ -66,7 +68,7 @@ class GitlabCiDsl : DslBase() {
private fun ensureDefault() = default ?: DefaultDsl().also { default = it }
private fun ensureWorkflow() = workflow ?: WorkflowDsl().also { workflow = it }
private fun ensureInclude() = include ?: IncludeDsl().also { include = it }
private fun ensureVariables() = variables ?: VariablesDsl().also { variables = it }
private fun ensureVariables() = variables ?: GlobalVariablesDsl().also { variables = it }

private fun asMap(): Map<String, DslBase> {
val map = mutableMapOf<String, DslBase>()
Expand Down
102 changes: 102 additions & 0 deletions src/main/kotlin/pcimcioch/gitlabci/dsl/job/GlobalVariablesDsl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package pcimcioch.gitlabci.dsl.job

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

@Serializable(with = GlobalVariablesDsl.GlobalVariablesDslSerializer::class)
class GlobalVariablesDsl : DslBase() {
var variables: MutableMap<String, GlobalVariableDsl> = mutableMapOf()

fun add(name: String, block: GlobalVariableDsl.() -> Unit) = variables.put(name, createGlobalVariable(block))
fun add(name: String, value: GlobalVariableDsl) = variables.put(name, value)
fun add(name: String, value: Any) = variables.put(name, createGlobalVariable(value.toString()))

fun <T : Enum<T>> add(name: T, block: GlobalVariableDsl.() -> Unit) = variables.put(name.toString(), createGlobalVariable(block))
fun <T : Enum<T>> add(name: T, value: GlobalVariableDsl) = variables.put(name.toString(), value)
fun <T : Enum<T>> add(name: T, value: Any) = variables.put(name.toString(), createGlobalVariable(value.toString()))

infix fun String.to(block: GlobalVariableDsl.() -> Unit) = add(this, createGlobalVariable(block))
infix fun String.to(value: GlobalVariableDsl) = add(this, value)
infix fun String.to(value: Any) = add(this, value)

infix fun <T : Enum<T>> T.to(block: GlobalVariableDsl.() -> Unit) = add(this, createGlobalVariable(block))
infix fun <T : Enum<T>> T.to(value: GlobalVariableDsl) = add(this, value)
infix fun <T : Enum<T>> T.to(value: Any) = add(this, value)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as GlobalVariablesDsl

if (variables != other.variables) return false

return true
}

override fun hashCode(): Int {
return variables.hashCode()
}

object GlobalVariablesDslSerializer : ValueSerializer<GlobalVariablesDsl, Map<String, GlobalVariableDsl>>(
MapSerializer(String.serializer(), GlobalVariableDsl.serializer()),
GlobalVariablesDsl::variables
)

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

fun createGlobalVariables(block: GlobalVariablesDsl.() -> Unit = {}) = GlobalVariablesDsl().apply(block)

@Serializable
class GlobalVariableDsl : DslBase() {
var description: String? = null
var value: String? = null
var options: MutableList<String>? = null
var expand: Boolean? = null

fun options(vararg elements: String) = options(elements.toList())
fun options(elements: Iterable<String>) = ensureOptions().addAll(elements)

private fun ensureOptions() = options ?: mutableListOf<String>().also { options = it }

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as GlobalVariableDsl

if (description != other.description) return false
if (value != other.value) return false
if (options != other.options) return false
if (expand != other.expand) return false

return true
}

override fun hashCode(): Int {
var result = description?.hashCode() ?: 0
result = 31 * result + (value?.hashCode() ?: 0)
result = 31 * result + (options?.hashCode() ?: 0)
result = 31 * result + (expand?.hashCode() ?: 0)
return result
}

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

fun createGlobalVariable(block: GlobalVariableDsl.() -> Unit = {}) = GlobalVariableDsl().apply(block)
fun createGlobalVariable(value: String, block: GlobalVariableDsl.() -> Unit = {}) = GlobalVariableDsl().apply {
this.value = value
}.apply(block)
1 change: 1 addition & 0 deletions src/main/kotlin/pcimcioch/gitlabci/dsl/job/RetryDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class RetryDsl(
}

private fun ensureWhenRetry() = whenRetry ?: mutableSetOf<WhenRetryType>().also { whenRetry = it }

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
19 changes: 13 additions & 6 deletions src/test/kotlin/pcimcioch/gitlabci/dsl/GitlabCiDslTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import pcimcioch.gitlabci.dsl.job.WhenRunType
import pcimcioch.gitlabci.dsl.job.createGlobalVariable
import pcimcioch.gitlabci.dsl.job.createJob
import java.io.StringWriter

Expand Down Expand Up @@ -100,7 +101,8 @@ internal class GitlabCiDslTest {
image:
name: ""
"variables":
"TEST": "value"
"TEST":
value: "value"
"image":
script:
- "test"
Expand Down Expand Up @@ -228,15 +230,18 @@ internal class GitlabCiDslTest {
// when
gitlabCi(writer = writer) {
variables {
"TEST" to "value"
"TEST" to {
description = "test description"
}
}
}

// then
assertThat(writer.toString()).isEqualTo(
"""
"variables":
"TEST": "value"
"TEST":
description: "test description"
""".trimIndent()
)
}
Expand All @@ -245,14 +250,15 @@ internal class GitlabCiDslTest {
fun `should add variable from map`() {
// when
gitlabCi(writer = writer) {
variables(mutableMapOf("TEST" to "value"))
variables(mutableMapOf("TEST" to createGlobalVariable("value")))
}

// then
assertThat(writer.toString()).isEqualTo(
"""
"variables":
"TEST": "value"
"TEST":
value: "value"
""".trimIndent()
)
}
Expand Down Expand Up @@ -390,7 +396,8 @@ internal class GitlabCiDslTest {
after_script:
- "after command"
"variables":
"TEST1": "123"
"TEST1":
value: "123"
"build app":
stage: "build"
script:
Expand Down
165 changes: 165 additions & 0 deletions src/test/kotlin/pcimcioch/gitlabci/dsl/job/GlobalVariablesDslTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package pcimcioch.gitlabci.dsl.job

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import pcimcioch.gitlabci.dsl.DslTestBase

internal class GlobalVariablesDslTest : DslTestBase<GlobalVariablesDsl>(GlobalVariablesDsl.serializer()) {

@Test
fun `should create from block`() {
// given
val testee = createGlobalVariables {
add("key1", "value 1")
}

// then
assertDsl(
testee,
"""
"key1":
value: "value 1"
"""
)
}

@Test
fun `should create empty variables`() {
// given
val testee = createGlobalVariables {}

// then
assertDsl(
testee,
"""
{}
"""
)
}

@Test
fun `should allow multiple types of adding`() {
// given
val testee = createGlobalVariables {
add("key1", "value1")
add("key2", createGlobalVariable("value2"))
add("key3") {
description = "value 3 description"
options = mutableListOf("t1", "t2")
}

add(RunnerSettingsVariables.GIT_CLONE_PATH, "value4")
add(RunnerSettingsVariables.ARTIFACT_DOWNLOAD_ATTEMPTS, createGlobalVariable("value5"))
add(RunnerSettingsVariables.CACHE_FALLBACK_KEY) {
description = "value 6 description"
expand = false
}

"key7" to "value7"
"key8" to createGlobalVariable("value8")
"key9" to {
description = "value 9 description"
}

RunnerSettingsVariables.GIT_DEPTH to "value10"
RunnerSettingsVariables.GIT_STRATEGY to createGlobalVariable("value11")
RunnerSettingsVariables.GIT_SUBMODULE_STRATEGY to {
description = "value 12 description"
}
}

// then
assertDsl(
testee,
"""
"key1":
value: "value1"
"key2":
value: "value2"
"key3":
description: "value 3 description"
options:
- "t1"
- "t2"
"GIT_CLONE_PATH":
value: "value4"
"ARTIFACT_DOWNLOAD_ATTEMPTS":
value: "value5"
"CACHE_FALLBACK_KEY":
description: "value 6 description"
expand: false
"key7":
value: "value7"
"key8":
value: "value8"
"key9":
description: "value 9 description"
"GIT_DEPTH":
value: "value10"
"GIT_STRATEGY":
value: "value11"
"GIT_SUBMODULE_STRATEGY":
description: "value 12 description"
"""
)
}

@Test
fun `should create direct access`() {
// given
val map = mutableMapOf(
"key1" to createGlobalVariable("value 1"),
"key2" to createGlobalVariable("value 2")
)

val testee = createGlobalVariables {
variables = map
}

// then
assertDsl(
testee,
"""
"key1":
value: "value 1"
"key2":
value: "value 2"
"""
)
}

@Test
fun `should override on add`() {
// given
val testee = createGlobalVariables {
add("key1", "value 1")
add("key1", "value 2")
}

// then
assertDsl(
testee,
"""
"key1":
value: "value 2"
"""
)
}

@Test
fun `should be equal`() {
// given
val testee = createGlobalVariables {
add("one", "one")
add("two", "two")
}

val expected = createGlobalVariables {
add("one", "one")
add("two", "two")
}

// then
assertEquals(expected, testee)
}
}

0 comments on commit bf30d3e

Please sign in to comment.