feat: plugin cross building as Scala cross building

**Problem**
Since Scala cross building works better than the plugin cross building `^^`,
it was common for plugin authors to encode plugin cross building as Scala cross building
given that we usually have zero or one sbt release in one Scala version.

**Solution**
This brings in the setting into SbtPlugin so plugin authors can cross build
using sbt 2.x.
This commit is contained in:
Eugene Yokota 2024-09-22 17:25:08 -04:00
parent 354dd25988
commit 01d5f9c050
4 changed files with 48 additions and 8 deletions

View File

@ -332,9 +332,6 @@ object Defaults extends BuildCommon {
envVars :== Map.empty,
sbtVersion := appConfiguration.value.provider.id.version,
sbtBinaryVersion := binarySbtVersion(sbtVersion.value),
// `pluginCrossBuild` scoping is based on sbt-cross-building plugin.
// The idea here is to be able to define a `sbtVersion in pluginCrossBuild`, which
// directs the dependencies of the plugin to build to the specified sbt plugin version.
pluginCrossBuild / sbtVersion := sbtVersion.value,
onLoad := idFun[State],
onUnload := idFun[State],
@ -3102,7 +3099,7 @@ object Classpaths {
Defaults.globalDefaults(
Seq(
publishMavenStyle :== true,
sbtPluginPublishLegacyMavenStyle :== true,
sbtPluginPublishLegacyMavenStyle :== false,
publishArtifact :== true,
(Test / publishArtifact) :== false
)

View File

@ -10,12 +10,19 @@ package sbt
package plugins
import sbt.Def.Setting
import sbt.Keys._
import sbt.Keys.*
import sbt.SlashSyntax0.*
object SbtPlugin extends AutoPlugin {
object SbtPlugin extends AutoPlugin:
override def requires = ScriptedPlugin
override lazy val projectSettings: Seq[Setting[_]] = Seq(
sbtPlugin := true
sbtPlugin := true,
pluginCrossBuild / sbtVersion := {
scalaBinaryVersion.value match
case "3" => sbtVersion.value
case "2.12" => "1.5.8"
case "2.10" => "0.13.18"
},
)
}
end SbtPlugin

View File

@ -0,0 +1,28 @@
val scala3 = "3.3.3"
val scala212 = "2.12.19"
organization := "com.example"
version := "0.1.0-SNAPSHOT"
lazy val checkSbt2Plugin = taskKey[Unit]("")
lazy val plugin = (projectMatrix in file("plugin"))
.enablePlugins(SbtPlugin)
.settings(
organization := "com.example",
name := "sbt-example",
// TODO: Once 2.0 is released we can move this check to the `test` file
checkSbt2Plugin := {
val repo = (ThisBuild / baseDirectory).value / "repo"
val sbtV = sbtBinaryVersion.value
val expected = (repo / "com" / "example" /
s"sbt-example_sbt${sbtV}_3" /
"0.1.0-SNAPSHOT" /
s"sbt-example_sbt${sbtV}_3-0.1.0-SNAPSHOT.pom")
assert(expected.exists, s"$expected did not exist")
},
)
.jvmPlatform(scalaVersions = Seq(scala3, scala212))
publishMavenStyle := true
publishTo := Some(Resolver.file("test-publish", (ThisBuild / baseDirectory).value / "repo/"))

View File

@ -0,0 +1,8 @@
> compile
> publish
$ exists repo/com/example/sbt-example_2.12_1.0/0.1.0-SNAPSHOT/sbt-example_2.12_1.0-0.1.0-SNAPSHOT.pom
$ absent repo/com/example/sbt-example_2.12_1.0/0.1.0-SNAPSHOT/sbt-example-0.1.0-SNAPSHOT.pom
> checkSbt2Plugin