Skip to content

Commit

Permalink
feat: Use _sbt2_3 suffix
Browse files Browse the repository at this point in the history
**Problem**
The extra attribute is a vestige from the days when sbt plugins were published on Ivy repos.

**Solution**
This uses normal suffix system.
  • Loading branch information
eed3si9n committed Sep 16, 2024
1 parent 2cb36bc commit 803e575
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions main/src/main/scala/sbt/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2725,10 +2725,17 @@ object Defaults extends BuildCommon {
}

def sbtPluginExtra(m: ModuleID, sbtV: String, scalaV: String): ModuleID =
m.extra(
PomExtraDependencyAttributes.SbtVersionKey -> sbtV,
PomExtraDependencyAttributes.ScalaVersionKey -> scalaV
).withCrossVersion(Disabled())
partialVersion(sbtV) match
case Some((0, _)) | Some((1, _)) =>
m.extra(
PomExtraDependencyAttributes.SbtVersionKey -> sbtV,
PomExtraDependencyAttributes.ScalaVersionKey -> scalaV
).withCrossVersion(Disabled())
case Some(_) =>
// this produces a normal suffix like _sjs1_2.13
val prefix = s"sbt${binarySbtVersion(sbtV)}_"
m.cross(CrossVersion.binaryWith(prefix, ""))
case None => sys.error(s"unknown sbt version $sbtV")

def discoverSbtPluginNames: Initialize[Task[PluginDiscovery.DiscoveredNames]] =
(Def.task { sbtPlugin.value }).flatMapTask { case p =>
Expand Down Expand Up @@ -3073,16 +3080,18 @@ object Classpaths {
Defaults.globalDefaults(
Seq(
publishMavenStyle :== true,
sbtPluginPublishLegacyMavenStyle := true,
sbtPluginPublishLegacyMavenStyle :== true,
publishArtifact :== true,
(Test / publishArtifact) :== false
)
)

private lazy val publishSbtPluginMavenStyle = Def.task(sbtPlugin.value && publishMavenStyle.value)
private lazy val packagedDefaultArtifacts = packaged(defaultArtifactTasks)
private lazy val emptyArtifacts = Def.task(Map.empty[Artifact, HashedVirtualFileRef])

private lazy val sbt2Plus: Def.Initialize[Boolean] = Def.setting {
val sbtV = (pluginCrossBuild / sbtBinaryVersion).value
sbtV != "1.0" && !sbtV.startsWith("0.")
}
val jvmPublishSettings: Seq[Setting[_]] = Seq(
artifacts := artifactDefs(defaultArtifactTasks).value,
packagedArtifacts := Def
Expand All @@ -3102,13 +3111,39 @@ object Classpaths {
* valid POM file, that is a POM file that Maven can resolve.
*/
private def mavenArtifactsOfSbtPlugin: Def.Initialize[Task[Map[Artifact, HashedVirtualFileRef]]] =
Def.task {
// This is a conditional task. The top-level must be an if expression.
if (sbt2Plus.value) {
// Both POMs and JARs are Maven-compatible in sbt 2.x, so ignore the workarounds
packagedDefaultArtifacts.value
} else {
val crossVersion = sbtCrossVersion.value
val legacyPomArtifact = (makePom / artifact).value
val converter = fileConverter.value
def addSuffix(a: Artifact): Artifact = a.withName(crossVersion(a.name))
Map(
addSuffix(legacyPomArtifact) -> converter.toVirtualFile(
makeMavenPomOfSbtPlugin.value.toPath()
)
) ++
pomConsistentArtifactsForLegacySbt.value ++
legacyPackagedArtifacts.value
}
}

private def legacyPackagedArtifacts: Def.Initialize[Task[Map[Artifact, HashedVirtualFileRef]]] =
Def.task {
// This is a conditional task. The top-level must be an if expression.
if (sbtPluginPublishLegacyMavenStyle.value) packagedDefaultArtifacts.value
else Map.empty[Artifact, HashedVirtualFileRef]
}

private def pomConsistentArtifactsForLegacySbt
: Def.Initialize[Task[Map[Artifact, HashedVirtualFileRef]]] =
Def.task {
val crossVersion = sbtCrossVersion.value
val legacyArtifact = (makePom / artifact).value
val converter = fileConverter.value
val pom = converter.toVirtualFile(makeMavenPomOfSbtPlugin.value.toPath)
val legacyPackages = packaged(defaultPackages).value
def addSuffix(a: Artifact): Artifact = a.withName(crossVersion(a.name))
val converter = fileConverter.value
def copyArtifact(
artifact: Artifact,
fileRef: HashedVirtualFileRef
Expand All @@ -3120,11 +3155,9 @@ object Classpaths {
IO.copyFile(file, targetFile)
artifact.withName(nameWithSuffix) -> converter.toVirtualFile(targetFile.toPath)
}
val packages = legacyPackages.map { case (artifact, file) => copyArtifact(artifact, file) }
val legacyPackagedArtifacts = Def
.ifS(sbtPluginPublishLegacyMavenStyle.toTask)(packagedDefaultArtifacts)(emptyArtifacts)
.value
packages + (addSuffix(legacyArtifact) -> pom) ++ legacyPackagedArtifacts
legacyPackages.map { case (artifact, file) =>
copyArtifact(artifact, file);
}
}

private def sbtCrossVersion: Def.Initialize[String => String] = Def.setting {
Expand Down

0 comments on commit 803e575

Please sign in to comment.