Skip to content

Commit

Permalink
Replace hacky crossVersion mechanism with a command alias to cross …
Browse files Browse the repository at this point in the history
…publish compiler plugins and scalalib artifacts. Previous approach was publishing compiler plugins at least twice
  • Loading branch information
WojciechMazur committed Feb 28, 2024
1 parent 037d754 commit a1900cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 90 deletions.
27 changes: 0 additions & 27 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ object Build {
}.value
}

val crossPublish = taskKey[Unit](
"Cross publish project without signing and excluding currently used version"
)
val crossPublishSigned = taskKey[Unit](
"Cross publish signed project excluding currently used version"
)

lazy val root: Project =
Project(id = "scala-native", base = file("."))
.settings(
Expand All @@ -105,17 +98,8 @@ object Build {
Seq(Compile / compile, Test / compile).map(
setDepenencyForCurrentBinVersion(_, allMultiScalaProjects)
),
crossPublish := {},
crossPublishSigned := {},
Seq(publish, publishSigned, publishLocal).map(
setDepenencyForCurrentBinVersion(_, publishedMultiScalaProjects)
),
Seq(crossPublish, crossPublishSigned).map(
setDepenencyForCurrentBinVersion(
_,
crossPublishedMultiScalaProjects,
includeNoCrossProjects = false
)
)
)

Expand Down Expand Up @@ -593,17 +577,6 @@ object Build {
}
)
}
.mapBinaryVersions { version =>
// Compiling both nscplugins and scalalib might lead to dataraces and missing classfiles
_.settings(
crossPublish := crossPublish
.dependsOn(nscPlugin.forBinaryVersion(version) / crossPublish)
.value,
crossPublishSigned := crossPublish
.dependsOn(nscPlugin.forBinaryVersion(version) / crossPublishSigned)
.value
)
}
.dependsOn(auxlib)

// Tests ------------------------------------------------
Expand Down
45 changes: 28 additions & 17 deletions project/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ object Commands {
testMima,
testScripted,
publishLocalDev,
publishRelease,
publishReleaseForVersion
)

Expand Down Expand Up @@ -182,23 +181,35 @@ object Commands {

lazy val publishReleaseForVersion =
projectVersionCommand("publish-release-for-version") {
case (version, state) =>
val scalaVersion = version match {
case "2.12" => ScalaVersions.scala212
case "2.13" => ScalaVersions.scala213
case "3" => ScalaVersions.scala3PublishVersion
case _ => sys.error(s"Invalid Scala binary version: '$version'")
case (binVersion, state) =>
val (scalaVersion, crossScalaVersions) = binVersion match {
case "2.12" => ScalaVersions.scala212 -> ScalaVersions.crossScala212
case "2.13" => ScalaVersions.scala213 -> ScalaVersions.crossScala213
case "3" =>
ScalaVersions.scala3PublishVersion -> ScalaVersions.crossScala3
case _ => sys.error(s"Invalid Scala binary version: '$binVersion'")
}
"clean" :: s"++$scalaVersion; publishSigned; crossPublishSigned" :: state
}

lazy val publishRelease = Command.command("publish-release") { state =>
import ScalaVersions._
val publishEachVersion = for {
version <- List(scala212, scala213, scala3PublishVersion)
} yield s"++$version; publishSigned; crossPublishSigned"
val publishCommand = "publishSigned"
val publishBaseVersion = s"++$scalaVersion; $publishCommand"
val publishCrossVersions = crossScalaVersions
.diff(scalaVersion :: Nil) // exclude already published base version
.toList
.map { crossVersion =>
Build.crossPublishedMultiScalaProjects
.map(_.forBinaryVersion(binVersion))
.map(project => s"${project.id}/$publishCommand")
.mkString(s"++ $crossVersion; all ", " ", "")
}
val crossPublish = ScalaVersions
val commandsToExecute =
"clean" :: publishBaseVersion :: publishCrossVersions

"clean" :: publishEachVersion ::: state
}
println(
s"Publish for Scala $binVersion would execute following commands:"
)
commandsToExecute.foreach(println)
println("")

commandsToExecute ::: state
}
}
46 changes: 0 additions & 46 deletions project/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.jsuereth.sbtpgp.PgpKeys

import scala.collection.mutable
import scala.scalanative.build.Platform
import Build.{crossPublish, crossPublishSigned}
import MyScalaNativePlugin.isGeneratingForIDE

import java.io.File
Expand Down Expand Up @@ -582,52 +581,8 @@ object Settings {
publishSettings(None),
mavenPublishSettings,
exportJars := true,
crossPublishSettings
)

lazy val crossPublishSettings = Def.settings(
crossPublish := crossPublishProject(publish).value,
crossPublishSigned := crossPublishProject(publishSigned).value
)

/** Builds a given project across all crossScalaVersion values. It does not
* modify the value of scalaVersion outside of it's scope. This allows to
* build multiple projects in parallel.
*/
def crossPublishProject(publishKey: TaskKey[Unit]) = Def.task {
val currentVersion = scalaVersion.value
val binVersion = CrossVersion.binaryScalaVersion(currentVersion)
val s = state.value
val log = s.log
val extracted = sbt.Project.extract(s)
val id = thisProjectRef.value.project
val selfRef = thisProjectRef.value
val _ = crossScalaVersions.value.foreach {
case `currentVersion` =>
log.info(
s"Skip publish $id ${currentVersion} - it should be already published"
)
case crossVersion =>
log.info(s"Try publish $id ${crossVersion}")
val (_, result) = sbt.Project
.runTask(
selfRef / publishKey,
state = extracted.appendWithoutSession(
Build.allMultiScalaProjects
.map(
_.forBinaryVersion(binVersion) / scalaVersion := crossVersion
),
s
)
)
.get
result.toEither match {
case Left(failure) => throw new RuntimeException(failure)
case Right(_) => System.gc()
}
}
}

lazy val sbtPluginSettings = Def.settings(
commonSettings,
toolSettings,
Expand Down Expand Up @@ -738,7 +693,6 @@ object Settings {
def commonScalalibSettings(libraryName: String): Seq[Setting[_]] = {
Def.settings(
version := scalalibVersion(scalaVersion.value, nativeVersion),
crossPublishSettings,
mavenPublishSettings,
disabledDocsSettings,
recompileAllOrNothingSettings,
Expand Down

0 comments on commit a1900cb

Please sign in to comment.