Skip to content

Commit

Permalink
Support newer Graalvm JDK 17 distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Sep 14, 2023
1 parent 0bf2ef0 commit 6c78009
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 15 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest]
scala: [2.12.18]
java: [temurin@8, graal_22.3.0@17]
java: [temurin@8, graal_graalvm@17]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
Expand All @@ -40,12 +40,12 @@ jobs:
java-version: 8
cache: sbt

- name: Setup GraalVM (graal_22.3.0@17)
if: matrix.java == 'graal_22.3.0@17'
- name: Setup GraalVM (graal_graalvm@17)
if: matrix.java == 'graal_graalvm@17'
uses: graalvm/setup-graalvm@v1
with:
version: 22.3.0
java-version: 17
distribution: graalvm
components: native-image
github-token: ${{ secrets.GITHUB_TOKEN }}
cache: sbt
Expand Down Expand Up @@ -88,12 +88,12 @@ jobs:
java-version: 8
cache: sbt

- name: Setup GraalVM (graal_22.3.0@17)
if: matrix.java == 'graal_22.3.0@17'
- name: Setup GraalVM (graal_graalvm@17)
if: matrix.java == 'graal_graalvm@17'
uses: graalvm/setup-graalvm@v1
with:
version: 22.3.0
java-version: 17
distribution: graalvm
components: native-image
github-token: ${{ secrets.GITHUB_TOKEN }}
cache: sbt
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ThisBuild / scalaVersion := scala212
// Add windows-latest when https://github.com/sbt/sbt/issues/7082 is resolved
ThisBuild / githubWorkflowOSes := Seq("ubuntu-latest", "macos-latest")
ThisBuild / githubWorkflowBuild := Seq(WorkflowStep.Sbt(List("test", "scripted")))
ThisBuild / githubWorkflowJavaVersions += JavaSpec.graalvm("22.3.0", "17")
ThisBuild / githubWorkflowJavaVersions += JavaSpec.graalvm(Graalvm.Distribution("graalvm"), "17")

ThisBuild / githubWorkflowTargetTags ++= Seq("v*")
ThisBuild / githubWorkflowPublishTargetBranches :=
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ object GenerativePlugin extends AutoPlugin {

type PermissionValue = sbtghactions.PermissionValue
val PermissionValue = sbtghactions.PermissionValue

type Graalvm = sbtghactions.Graalvm
val Graalvm = sbtghactions.Graalvm
}

import autoImport._
Expand Down
49 changes: 46 additions & 3 deletions src/main/scala/sbtghactions/JavaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,58 @@ package sbtghactions

final case class JavaSpec(dist: JavaSpec.Distribution, version: String) {
def render: String = dist match {
case JavaSpec.Distribution.GraalVM(gversion) => s"graal_$gversion@$version"
case JavaSpec.Distribution.GraalVM(Graalvm.Version(gversion)) =>
s"graal_$gversion@$version"
case JavaSpec.Distribution.GraalVM(Graalvm.Distribution(distribution)) =>
s"graal_$distribution@$version"
case dist => s"${dist.rendering}@$version"
}
}

/**
* @see https://github.com/graalvm/setup-graalvm#migrating-from-graalvm-223-or-earlier-to-the-new-graalvm-for-jdk-17-and-later
*/
sealed trait Graalvm {
private[sbtghactions] def compile: String
}

object Graalvm {
/**
* For versions of Graalvm JDK 17 or earlier
*/
final case class Version(version: String) extends Graalvm {
override private[sbtghactions] val compile: String = version
}

/**
* For versions of Graalvm JDK 17 or later. Currently valid distributions are
* graalvm, graalvm-community or mandrel
*/
final case class Distribution(distribution: String) extends Graalvm {
override private[sbtghactions] val compile: String = distribution
}
}

object JavaSpec {

def temurin(version: String): JavaSpec = JavaSpec(Distribution.Temurin, version)
def graalvm(graal: String, version: String): JavaSpec = JavaSpec(Distribution.GraalVM(graal), version)

private[sbtghactions] object JavaExtractor {
def unapply(version: String): Option[Int] =
version.split("\\.").headOption.map(_.toInt)
}

def graalvm(graal: Graalvm, version: String): JavaSpec = {
(graal, version) match {
case (Graalvm.Version(_), JavaExtractor(javaVersion)) if javaVersion > 17 =>
throw new IllegalArgumentException("Please use Graalvm.Distribution for JDK's newer than 17")
case (Graalvm.Distribution(_), JavaExtractor(javaVersion)) if javaVersion < 17 =>
throw new IllegalArgumentException("GraalVm.Distribution is not compatible with JDK's older than 17")
case _ =>
}

JavaSpec(Distribution.GraalVM(graal), version)
}

sealed abstract class Distribution(val rendering: String) extends Product with Serializable

Expand All @@ -36,6 +79,6 @@ object JavaSpec {
case object Adopt extends Distribution("adopt-hotspot")
case object OpenJ9 extends Distribution("adopt-openj9")
case object Liberica extends Distribution("liberica")
final case class GraalVM(version: String) extends Distribution(version)
final case class GraalVM(graalvm: Graalvm) extends Distribution(graalvm.compile)
}
}
14 changes: 12 additions & 2 deletions src/main/scala/sbtghactions/WorkflowStep.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object WorkflowStep {

def SetupJava(versions: List[JavaSpec]): List[WorkflowStep] =
versions map {
case jv @ JavaSpec(JavaSpec.Distribution.GraalVM(graalVersion), version) =>
case jv @ JavaSpec(JavaSpec.Distribution.GraalVM(Graalvm.Version(graalVersion)), version) =>
WorkflowStep.Use(
UseRef.Public("graalvm", "setup-graalvm", "v1"),
name = Some(s"Setup GraalVM (${jv.render})"),
Expand All @@ -52,7 +52,17 @@ object WorkflowStep {
"components" -> "native-image",
"github-token" -> s"$${{ secrets.GITHUB_TOKEN }}",
"cache" -> "sbt"))

case jv @ JavaSpec(JavaSpec.Distribution.GraalVM(Graalvm.Distribution(distribution)), version) =>
WorkflowStep.Use(
UseRef.Public("graalvm", "setup-graalvm", "v1"),
name = Some(s"Setup GraalVM (${jv.render})"),
cond = Some(s"matrix.java == '${jv.render}'"),
params = ListMap(
"java-version" -> s"$version",
"distribution" -> distribution,
"components" -> "native-image",
"github-token" -> s"$${{ secrets.GITHUB_TOKEN }}",
"cache" -> "sbt"))
case jv @ JavaSpec(dist, version) =>
WorkflowStep.Use(
UseRef.Public("actions", "setup-java", "v3"),
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/sbtghactions/check-and-regenerate/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ThisBuild / scalaVersion := crossScalaVersions.value.head

ThisBuild / githubWorkflowTargetTags += "v*"

ThisBuild / githubWorkflowJavaVersions += JavaSpec.graalvm("22.3.0", "17")
ThisBuild / githubWorkflowJavaVersions += JavaSpec.graalvm(Graalvm.Version("22.3.0"), "17")
ThisBuild / githubWorkflowPublishTargetBranches += RefPredicate.Equals(Ref.Tag("test"))

ThisBuild / githubWorkflowBuildMatrixAdditions += "test" -> List("this", "is")
Expand Down
42 changes: 41 additions & 1 deletion src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ class GenerativePluginSpec extends Specification {
}

"compile a job with java setup, two JVMs and two Scalas" in {
val javas = List(JavaSpec.temurin("17"), JavaSpec.graalvm("22.3.0", "11"))
val javas = List(JavaSpec.temurin("17"), JavaSpec.graalvm(Graalvm.Version("22.3.0"), "11"))

val results = compileJob(
WorkflowJob(
Expand Down Expand Up @@ -582,6 +582,46 @@ class GenerativePluginSpec extends Specification {
cache: sbt"""
}

"compile a job with java setup using new Graalvm distribution scheme" in {
val javas = List(JavaSpec.graalvm(Graalvm.Distribution("graalvm"), "17"))

val results = compileJob(
WorkflowJob(
"abc",
"How to get to...",
WorkflowStep.SetupJava(javas),
scalas = List("2.12.17", "2.13.10"),
javas = javas),
"")

results mustEqual s"""abc:
name: How to get to...
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.17, 2.13.10]
java: [graal_graalvm@17]
runs-on: $${{ matrix.os }}
steps:
- name: Setup GraalVM (graal_graalvm@17)
if: matrix.java == 'graal_graalvm@17'
uses: graalvm/setup-graalvm@v1
with:
java-version: 17
distribution: graalvm
components: native-image
github-token: $${{ secrets.GITHUB_TOKEN }}
cache: sbt"""
}

"throw an exception when using Graalvm.Distribution for JDK's older than 17" in {
JavaSpec.graalvm(Graalvm.Distribution("graalvm"), "11") must throwA[IllegalArgumentException]
}

"throw an exception when using Graalvm.Version for JDK's newer than 17" in {
JavaSpec.graalvm(Graalvm.Version("22.3.0"), "20") must throwA[IllegalArgumentException]
}

"compile a job with environment variables, conditional, and needs with an sbt step" in {
val results = compileJob(
WorkflowJob(
Expand Down

0 comments on commit 6c78009

Please sign in to comment.