Skip to content

Commit

Permalink
Generalized Use to allow referencing arbitrary refs
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed Apr 13, 2020
1 parent 49e46f9 commit ae63f66
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 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.4"
ThisBuild / baseVersion := "0.6"

ThisBuild / organization := "com.codecommit"
ThisBuild / publishGithubUser := "djspiewak"
Expand Down
24 changes: 12 additions & 12 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ ${indent(rendered.mkString("\n"), 1)}"""

renderedShell + "run: " + wrap(s"$sbt ++$${{ matrix.scala }} ${safeCommands.mkString(" ")}")

case Use(owner, repo, version, params, _, _, _, _) =>
case Use(owner, repo, ref, params, _, _, _, _) =>
val renderedParamsPre = compileEnv(params, prefix = "with")
val renderedParams = if (renderedParamsPre.isEmpty)
""
else
"\n" + renderedParamsPre

s"uses: $owner/$repo@v$version" + renderedParams
s"uses: $owner/$repo@$ref" + renderedParams
}

indent(preamble + body, 1).updated(0, '-')
Expand Down Expand Up @@ -324,7 +324,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"upload-artifact",
1,
"v1",
name = Some(s"Upload target directory '$target' ($${{ matrix.scala }})"),
params = Map(
"name" -> s"target-$${{ matrix.os }}-$${{ matrix.scala }}-$${{ matrix.java }}-${sanitizeTarget(target)}",
Expand All @@ -334,7 +334,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
mainSteps :+ WorkflowStep.Use(
"actions",
"upload-artifact",
1,
"v1",
name = Some(s"Upload target directory 'project/target'"),
params = Map(
"name" -> s"target-$${{ matrix.os }}-$${{ matrix.java }}-project_target",
Expand All @@ -347,7 +347,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"download-artifact",
1,
"v1",
name = Some(s"Download target directory '$target' ($v)"),
params = Map("name" -> s"target-$${{ matrix.os }}-$v-$${{ matrix.java }}-${sanitizeTarget(target)}"))
}
Expand All @@ -356,7 +356,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
mainSteps :+ WorkflowStep.Use(
"actions",
"download-artifact",
1,
"v1",
name = Some(s"Download target directory 'project/target'"),
params = Map("name" -> s"target-$${{ matrix.os }}-$${{ matrix.java }}-project_target"))
},
Expand All @@ -372,7 +372,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"cache",
1,
"v1",
name = Some("Cache ivy2"),
params = Map(
"path" -> "~/.ivy2/cache",
Expand All @@ -381,7 +381,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"cache",
1,
"v1",
name = Some("Cache coursier (generic)"),
params = Map(
"path" -> "~/.coursier/cache/v1",
Expand All @@ -390,7 +390,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"cache",
1,
"v1",
name = Some("Cache coursier (linux)"),
cond = Some(s"contains(runner.os, 'linux')"),
params = Map(
Expand All @@ -400,7 +400,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"cache",
1,
"v1",
name = Some("Cache coursier (macOS)"),
cond = Some(s"contains(runner.os, 'macos')"),
params = Map(
Expand All @@ -410,7 +410,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"cache",
1,
"v1",
name = Some("Cache coursier (windows)"),
cond = Some(s"contains(runner.os, 'windows')"),
params = Map(
Expand All @@ -420,7 +420,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
WorkflowStep.Use(
"actions",
"cache",
1,
"v1",
name = Some("Cache sbt"),
params = Map(
"path" -> "~/.sbt",
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/sbtghactions/WorkflowStep.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ sealed trait WorkflowStep extends Product with Serializable {

object WorkflowStep {

val CheckoutFull: WorkflowStep = Use("actions", "checkout", 1, name = Some("Checkout current branch (full)"))
val Checkout: WorkflowStep = Use("actions", "checkout", 2, name = Some("Checkout current branch (fast)"))
val CheckoutFull: WorkflowStep = Use("actions", "checkout", "v1", name = Some("Checkout current branch (full)"))
val Checkout: WorkflowStep = Use("actions", "checkout", "v2", name = Some("Checkout current branch (fast)"))

val SetupScala: WorkflowStep = Use("olafurpg", "setup-scala", 5, name = Some("Setup Java and Scala"), params = Map("java-version" -> s"$${{ matrix.java }}"))
val SetupScala: WorkflowStep = Use("olafurpg", "setup-scala", "v5", name = Some("Setup Java and Scala"), params = Map("java-version" -> s"$${{ matrix.java }}"))

val Tmate: WorkflowStep = Use("mxschmitt", "action-tmate", 2, name = Some("Setup tmate session"))
val Tmate: WorkflowStep = Use("mxschmitt", "action-tmate", "v2", name = Some("Setup tmate session"))

def ComputeVar(name: String, cmd: String): WorkflowStep =
Run(List(s"echo ::set-env name=$name::$$($cmd)"), name = Some(s"Export $name"))

final case class Run(commands: List[String], id: Option[String] = None, name: Option[String] = None, cond: Option[String] = None, env: Map[String, String] = Map()) extends WorkflowStep
final case class Sbt(commands: List[String], id: Option[String] = None, name: Option[String] = None, cond: Option[String] = None, env: Map[String, String] = Map()) extends WorkflowStep
final case class Use(owner: String, repo: String, version: Int, params: Map[String, String] = Map(), id: Option[String] = None, name: Option[String] = None, cond: Option[String] = None, env: Map[String, String] = Map()) extends WorkflowStep
final case class Use(owner: String, repo: String, ref: String, params: Map[String, String] = Map(), id: Option[String] = None, name: Option[String] = None, cond: Option[String] = None, env: Map[String, String] = Map()) extends WorkflowStep
}
16 changes: 12 additions & 4 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,19 @@ class GenerativePluginSpec extends Specification {
Use(
"repo",
"slug",
0),
"v0"),
"",
true) mustEqual "- uses: repo/slug@v0"
}

"preserve wonky version in Use" in {
compileStep(Use("hello", "world", "v4.0.0"), "", true) mustEqual "- uses: hello/world@v4.0.0"
}

"drop Use version prefix on anything that doesn't start with a number" in {
compileStep(Use("hello", "world", "master"), "", true) mustEqual "- uses: hello/world@master"
}

"compile sbt using the command provided" in {
compileStep(
Sbt(List("show scalaVersion", "compile", "test")),
Expand All @@ -200,13 +208,13 @@ class GenerativePluginSpec extends Specification {

"compile use without parameters" in {
compileStep(
Use("olafurpg", "setup-scala", 5),
Use("olafurpg", "setup-scala", "v5"),
"") mustEqual "- uses: olafurpg/setup-scala@v5"
}

"compile use with two parameters" in {
compileStep(
Use("olafurpg", "setup-scala", 5, params = Map("abc" -> "def", "cafe" -> "@42")),
Use("olafurpg", "setup-scala", "v5", params = Map("abc" -> "def", "cafe" -> "@42")),
"") mustEqual "- uses: olafurpg/setup-scala@v5\n with:\n abc: def\n cafe: '@42'"
}

Expand All @@ -215,7 +223,7 @@ class GenerativePluginSpec extends Specification {
Use(
"derp",
"nope",
0,
"v0",
params = Map("teh" -> "schizzle", "think" -> "positive"),
env = Map("hi" -> "there")),
"") mustEqual "- env:\n hi: there\n uses: derp/nope@v0\n with:\n teh: schizzle\n think: positive"
Expand Down

0 comments on commit ae63f66

Please sign in to comment.