Skip to content

Commit

Permalink
Try to design a StrategyMatrix API #368
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfayard committed Oct 30, 2022
1 parent 931e4cd commit b878a7f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.krzeminski.githubactions.dsl.expressions.contexts

import it.krzeminski.githubactions.dsl.expressions.expr
import kotlin.reflect.KProperty


public open class StrategyMatrix(
private val map: MutableMap<String, List<String>> = mutableMapOf()
): Map<String, List<String>> by map {

public fun iterate(vararg value: String): Property {
return Property(value.toList())
}

public inner class Property(public val values: List<String>) {
public operator fun getValue(strategyMatrixContext: StrategyMatrix, property: KProperty<*>): String {
map[property.name] = values
return expr("matrix.${property.name}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package it.krzeminski.githubactions.dsl.expressions

import it.krzeminski.githubactions.actions.actions.SetupNodeV3
import it.krzeminski.githubactions.domain.RunnerType
import it.krzeminski.githubactions.domain.triggers.Push
import it.krzeminski.githubactions.dsl.expressions.contexts.StrategyMatrix
import it.krzeminski.githubactions.dsl.workflow
import it.krzeminski.githubactions.yaml.toYaml
import java.nio.file.Path

fun main() {
println(expectedWorkflow.toYaml())
}

val expectedWorkflow = workflow(
name = "Test matrix",
on = listOf(Push()),
sourceFile = Path.of("/tmp/some_workflow.main.kts"),
) {
val strategyMatrix = object : StrategyMatrix() {
val os by iterate("ubuntu-latest", "windows-latest")
val node by iterate("14", "16")
}
job(
id = "test_job",
name = "Test Job",
strategyMatrix = strategyMatrix,
runsOn = RunnerType.Custom(strategyMatrix.os),
) {
uses(SetupNodeV3(
nodeVersion = strategyMatrix.node
))
}
}

val expectedMatrixYaml = """
name: Test matrix
on: push
jobs:
build:
runs-on: ${expr("matrix.os")}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [14, 16]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${expr("matrix.node")}
""".trimIndent()

0 comments on commit b878a7f

Please sign in to comment.