Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import sbt.Keys._
import sbt.{given, _}

import java.nio.file.FileSystems
import scala.annotation.tailrec
import scala.concurrent.duration.FiniteDuration

object GenerativePlugin extends AutoPlugin {
Expand Down Expand Up @@ -638,10 +639,38 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
pathStr.replace(PlatformSep, "/") // *force* unix separators
}

/**
* The nearest ancestor of `target` which is not specific to `scalaVersion`, or `target`
* itself when it does not sit inside a Scala version directory.
*
* sbt 2 lays a project's target out as `target/out/<platform>/scala-<scalaVersion>/<module>`,
Comment thread
eed3si9n marked this conversation as resolved.
Resolved
* so only the currently loaded Scala version's directory exists. Aggregating it verbatim
* would bake that version into the generated workflow, which `githubWorkflowCheck` would
* then reject on every other row of the build matrix.
*
* Targets which are already independent of the Scala version are returned unchanged: sbt 1's
* `<project>/target`, and sbt 2's `target/out/<platform>/u/<module>`, which is the layout
* used when `crossPaths` is `false`.
*/
private[sbtghactions] def scalaVersionIndependentTarget(target: File, scalaVersion: String): File = {
val versionDirName = s"scala-$scalaVersion"

@tailrec
def parentOfVersionDir(candidate: File): Option[File] =
if (candidate == null)
None
else if (candidate.getName == versionDirName)
Option(candidate.getParentFile)
else
parentOfVersionDir(candidate.getParentFile)

parentOfVersionDir(target).getOrElse(target)
}

private val pathStrs = Def setting {
val base = (ThisBuild / baseDirectory).value.toPath

internalTargetAggregation.value map { file =>
internalTargetAggregation.value.distinct map { file =>
val path = file.toPath

if (path.isAbsolute)
Expand Down Expand Up @@ -914,7 +943,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
override def projectSettings = Seq(
Global / internalTargetAggregation ++= {
if (githubWorkflowArtifactUpload.value)
Seq(target.value)
Seq(scalaVersionIndependentTarget(target.value, scalaVersion.value))
else
Seq()
},
Expand Down
4 changes: 3 additions & 1 deletion src/sbt-test/sbtghactions/check-and-regenerate/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ ThisBuild / githubWorkflowPublishTimeout := Some(1.hour)
TaskKey[Unit]("patchIfSbt2") := {
if (sbtBinaryVersion.value == "2") {
val yml = file("expected-ci.yml")
val targetPath = IO.relativize(baseDirectory.value, target.value).get.replace(java.io.File.separatorChar, '/')
// sbt 2 nests targets under `target/out/<platform>/scala-<version>/<module>`; the plugin
// aggregates the Scala-version-independent parent of that, i.e. `target/out/<platform>`
val targetPath = IO.relativize(baseDirectory.value, target.value.getParentFile.getParentFile).get.replace(java.io.File.separatorChar, '/')
IO.write(
yml,
IO.read(yml).replace(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
organization := "com.github.sbt"
version := "0.0.1"

ThisBuild / crossScalaVersions := Seq("2.13.10", "2.12.17")
ThisBuild / scalaVersion := crossScalaVersions.value.head

lazy val root = project.in(file(".")).aggregate(sub)

lazy val sub = project.in(file("sub"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("com.github.sbt" % "sbt-github-actions" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
7 changes: 7 additions & 0 deletions src/sbt-test/sbtghactions/cross-scala-version-stability/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The generated workflow must not depend on which cross Scala version sbt was
# loaded with: CI runs `githubWorkflowCheck` once per matrix row, each with a
# different `++`, against a single committed ci.yml.
> githubWorkflowGenerate
> githubWorkflowCheck
> ++2.12.17
> githubWorkflowCheck
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ ThisBuild / githubWorkflowOSes := Seq("windows-latest")
TaskKey[Unit]("patchIfSbt2") := {
if (sbtBinaryVersion.value == "2") {
val yml = file(".github/workflows/ci.yml")
val targetPath = IO.relativize(baseDirectory.value, target.value).get.replace(java.io.File.separatorChar, '/')
// sbt 2 nests targets under `target/out/<platform>/scala-<version>/<module>`; the plugin
// aggregates the Scala-version-independent parent of that, i.e. `target/out/<platform>`
val targetPath = IO.relativize(baseDirectory.value, target.value.getParentFile.getParentFile).get.replace(java.io.File.separatorChar, '/')
IO.write(
yml,
IO.read(yml).replace(
Expand Down
4 changes: 3 additions & 1 deletion src/sbt-test/sbtghactions/no-clean/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ ThisBuild / githubWorkflowIncludeClean := false
TaskKey[Unit]("patchIfSbt2") := {
if (sbtBinaryVersion.value == "2") {
val yml = file(".github/workflows/ci.yml")
val targetPath = IO.relativize(baseDirectory.value, target.value).get.replace(java.io.File.separatorChar, '/')
// sbt 2 nests targets under `target/out/<platform>/scala-<version>/<module>`; the plugin
// aggregates the Scala-version-independent parent of that, i.e. `target/out/<platform>`
val targetPath = IO.relativize(baseDirectory.value, target.value.getParentFile.getParentFile).get.replace(java.io.File.separatorChar, '/')
IO.write(
yml,
IO.read(yml).replace(
Expand Down
8 changes: 5 additions & 3 deletions src/sbt-test/sbtghactions/non-existent-target/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ lazy val root = project.in(file(".")).aggregate(withTarget, withoutTarget)
TaskKey[Unit]("patchIfSbt2") := {
if (sbtBinaryVersion.value == "2") {
val yml = file(".github/workflows/ci.yml")
val targetPath = IO.relativize(baseDirectory.value, target.value).get.replace(java.io.File.separatorChar, '/')
val withTargetPath = IO.relativize(baseDirectory.value, (withTarget / target).value).get.replace(java.io.File.separatorChar, '/')
// sbt 2 nests targets under `target/out/<platform>/scala-<version>/<module>`; the plugin
// aggregates the Scala-version-independent parent of that, i.e. `target/out/<platform>`,
// which is shared by every project of the build
val targetPath = IO.relativize(baseDirectory.value, target.value.getParentFile.getParentFile).get.replace(java.io.File.separatorChar, '/')
IO.write(
yml,
IO.read(yml).replace(
"run: tar cf targets.tar target withTarget/target project/target",
s"run: tar cf targets.tar ${targetPath} ${withTargetPath} project/target"
s"run: tar cf targets.tar ${targetPath} project/target"
)
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/sbt-test/sbtghactions/sbt-native-thin-client/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ ThisBuild / githubWorkflowUseSbtThinClient := true
TaskKey[Unit]("patchIfSbt2") := {
if (sbtBinaryVersion.value == "2") {
val yml = file(".github/workflows/ci.yml")
val targetPath = IO.relativize(baseDirectory.value, target.value).get.replace(java.io.File.separatorChar, '/')
// sbt 2 nests targets under `target/out/<platform>/scala-<version>/<module>`; the plugin
// aggregates the Scala-version-independent parent of that, i.e. `target/out/<platform>`
val targetPath = IO.relativize(baseDirectory.value, target.value.getParentFile.getParentFile).get.replace(java.io.File.separatorChar, '/')
IO.write(
yml,
IO.read(yml).replace(
Expand Down
4 changes: 3 additions & 1 deletion src/sbt-test/sbtghactions/suppressed-scala-version/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ ThisBuild / githubWorkflowPublishTargetBranches += RefPredicate.Equals(Ref.Tag("
TaskKey[Unit]("patchIfSbt2") := {
if (sbtBinaryVersion.value == "2") {
val yml = file("expected-ci.yml")
val targetPath = IO.relativize(baseDirectory.value, target.value).get.replace(java.io.File.separatorChar, '/')
// sbt 2 nests targets under `target/out/<platform>/scala-<version>/<module>`; the plugin
// aggregates the Scala-version-independent parent of that, i.e. `target/out/<platform>`
val targetPath = IO.relativize(baseDirectory.value, target.value.getParentFile.getParentFile).get.replace(java.io.File.separatorChar, '/')
IO.write(
yml,
IO.read(yml).replace(
Expand Down
29 changes: 29 additions & 0 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sbtghactions

import org.specs2.mutable.Specification

import java.io.File
import java.net.URI
import scala.concurrent.duration.DurationInt

Expand Down Expand Up @@ -1154,4 +1155,32 @@ class GenerativePluginSpec extends Specification {
expectedDiff mustEqual actualDiff
}
}

"target aggregation" should {
"produce the same path for an sbt 2 target regardless of the Scala version it was loaded at" in {
val at213 = new File("/build/target/out/jvm/scala-2.13.10/sub")
val at212 = new File("/build/target/out/jvm/scala-2.12.17/sub")

scalaVersionIndependentTarget(at213, "2.13.10") mustEqual
scalaVersionIndependentTarget(at212, "2.12.17")
}

"narrow an sbt 2 target no further than the Scala version directory" in {
scalaVersionIndependentTarget(
new File("/build/target/out/jvm/scala-2.13.10/sub"),
"2.13.10") mustEqual new File("/build/target/out/jvm")
}

"leave an sbt 2 target with crossPaths disabled untouched" in {
val target = new File("/build/target/out/jvm/u/sub")

scalaVersionIndependentTarget(target, "2.13.10") mustEqual target
}

"leave an sbt 1 target untouched" in {
val target = new File("/build/sub/target")

scalaVersionIndependentTarget(target, "2.13.10") mustEqual target
}
}
}