Skip to content

Commit

Permalink
Added support for matrix additions
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed Apr 1, 2020
1 parent 366d7b9 commit b9ef4ee
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

name := "sbt-github-actions"

ThisBuild / baseVersion := "0.3"
ThisBuild / baseVersion := "0.4"

ThisBuild / organization := "com.codecommit"
ThisBuild / publishGithubUser := "djspiewak"
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/sbtghactions/GenerativeKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ trait GenerativeKeys {

lazy val githubWorkflowSbtCommand = settingKey[String]("The command which invokes sbt (default: sbt")

lazy val githubWorkflowBuildMatrixAdditions = settingKey[Map[String, List[String]]]("A map of additional matrix dimensions for the build job. Each list should be non-empty. (default: {})")
lazy val githubWorkflowBuildPreamble = settingKey[Seq[WorkflowStep]]("A list of steps to insert after base setup but before compiling and testing (default: [])")
lazy val githubWorkflowBuild = settingKey[WorkflowStep]("A workflow step which compiles and tests the project (default: Sbt(List(\"test\")))")

Expand Down
15 changes: 13 additions & 2 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,23 @@ ${indent(rendered.mkString("\n"), 1)}"""
else
"\n" + renderedEnvPre

val renderedMatricesPre = job.matrixAdds map {
case (key, values) => s"$key: ${values.map(wrap).mkString("[", ", ", "]")}"
} mkString "\n"

val renderedMatrices = if (renderedMatricesPre.isEmpty)
""
else
"\n" + indent(renderedMatricesPre, 2)

val declareShell = job.oses.exists(_.contains("windows"))

val body = s"""name: ${wrap(job.name)}${renderedNeeds}${renderedCond}
strategy:
matrix:
os: [${job.oses.mkString(", ")}]
scala: [${job.scalas.mkString(", ")}]
java: [${job.javas.mkString(", ")}]
java: [${job.javas.mkString(", ")}]${renderedMatrices}
runs-on: $${{ matrix.os }}${renderedEnv}
steps:
${indent(job.steps.map(compileStep(_, sbt, declareShell = declareShell)).mkString("\n\n"), 1)}"""
Expand Down Expand Up @@ -225,6 +234,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
val settingDefaults = Seq(
githubWorkflowSbtCommand := "sbt",

githubWorkflowBuildMatrixAdditions := Map(),
githubWorkflowBuildPreamble := Seq(),
githubWorkflowBuild := WorkflowStep.Sbt(List("test"), name = Some("Build project")),

Expand Down Expand Up @@ -493,7 +503,8 @@ git config --global alias.rm-symlink '!git rm-symlinks' # for back-compat."""
uploadStepsOpt,
oses = githubWorkflowOSes.value.toList,
scalas = crossScalaVersions.value.toList,
javas = githubWorkflowJavaVersions.value.toList)) ++ publishJobOpt ++ githubWorkflowAddedJobs.value
javas = githubWorkflowJavaVersions.value.toList,
matrixAdds = githubWorkflowBuildMatrixAdditions.value)) ++ publishJobOpt ++ githubWorkflowAddedJobs.value
})

private val generateCiContents = Def task {
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/sbtghactions/WorkflowJob.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ final case class WorkflowJob(
oses: List[String] = List("ubuntu-latest"),
scalas: List[String] = List("2.13.1"),
javas: List[String] = List("[email protected]"),
needs: List[String] = Nil)
needs: List[String] = Nil,
matrixAdds: Map[String, List[String]] = Map())
2 changes: 2 additions & 0 deletions src/sbt-test/sbtghactions/check-and-regenerate/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ ThisBuild / scalaVersion := crossScalaVersions.value.head

ThisBuild / githubWorkflowJavaVersions += "[email protected]"
ThisBuild / githubWorkflowPublishTargetBranches += RefPredicate.Equals(Ref.Tag("test"))

ThisBuild / githubWorkflowBuildMatrixAdditions += "test" -> List("this", "is")
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
os: [ubuntu-latest]
scala: [2.13.1, 2.12.10]
java: [[email protected], [email protected]]
test: [this, is]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (fast)
Expand Down
28 changes: 28 additions & 0 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ class GenerativePluginSpec extends Specification {
steps:
- run: csbt ++$${{ matrix.scala }} +compile"""
}


"compile a job with additional matrix components" in {
val results = compileJob(
WorkflowJob(
"bippy",
"Bippity Bop Around the Clock",
List(
WorkflowStep.Run(List("echo ${{ matrix.test }}")),
WorkflowStep.Checkout),
matrixAdds = Map("test" -> List("1", "2"))),
"")

results mustEqual s"""bippy:
name: Bippity Bop Around the Clock
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.1]
java: [[email protected]]
test: [1, 2]
runs-on: $${{ matrix.os }}
steps:
- run: echo $${{ matrix.test }}

- name: Checkout current branch (fast)
uses: actions/checkout@v2"""
}
}

"predicate compilation" >> {
Expand Down

0 comments on commit b9ef4ee

Please sign in to comment.