Skip to content

Commit

Permalink
Merge pull request #82 from arktekk/sbt/1.9
Browse files Browse the repository at this point in the history
Support both old and new plugin layouts
  • Loading branch information
hamnis authored Aug 11, 2023
2 parents 2948e47 + 97eb0b4 commit a3882d2
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ object SignedAetherPlugin extends AutoPlugin {
override def requires = AetherPlugin && SbtPgp
override def projectSettings = Seq(
aetherArtifact := {
AetherPlugin.createArtifact((Compile / PgpKeys.signedArtifacts).value, aetherCoordinates.value, aetherPackageMain.value)
AetherPlugin.createArtifact(
(Compile / PgpKeys.signedArtifacts).value,
(aetherLegacyPluginStyle ?? true).value,
aetherCoordinates.value,
aetherPackageMain.value
)
}
)

Expand Down
2 changes: 2 additions & 0 deletions aether-deploy/src/main/scala/aether/AetherArtifact.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ case class MavenCoordinates(
if (ext == extension) this else copy(extension = ext)
}

def isPlugin = props.contains(MavenCoordinates.SbtPlugin)

def withProp(name: String, value: String) = copy(props = props.updated(name, value))
}

Expand Down
63 changes: 47 additions & 16 deletions aether-deploy/src/main/scala/aether/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ object AetherKeys {
val aetherLocalRepo = settingKey[File]("Local maven repository.")
val aetherOldVersionMethod = settingKey[Boolean]("Flag for using the old method of getting the version")
val aetherCustomHttpHeaders = settingKey[Map[String, String]]("Add these headers to the http request")
val aetherLegacyPluginStyle =
SettingKey[Boolean](
"sbtPluginPublishLegacyMavenStyle",
"Configuration for generating the legacy pom of sbt plugins, to publish to Maven"
)
}

import AetherKeys._
Expand All @@ -31,7 +36,12 @@ object AetherPlugin extends AutoPlugin {
override def requires = sbt.plugins.IvyPlugin
override def projectSettings = aetherBaseSettings ++ Seq(
aetherArtifact := {
createArtifact((Compile / packagedArtifacts).value, aetherCoordinates.value, aetherPackageMain.value)
createArtifact(
(Compile / packagedArtifacts).value,
(aetherLegacyPluginStyle ?? true).value,
aetherCoordinates.value,
aetherPackageMain.value
)
}
)

Expand Down Expand Up @@ -64,10 +74,17 @@ object AetherPlugin extends AutoPlugin {
val art = artifact.value
val theVersion = (aetherDeploy / version).value

val legacyPluginLayout = (aetherLegacyPluginStyle ?? true).value
val defaultArtifactId =
CrossVersion(crossVersion.value, scalaVersion.value, scalaBinaryVersion.value).map(_(art.name)) getOrElse art.name

val artifactId =
if (!sbtPlugin.value)
CrossVersion(crossVersion.value, scalaVersion.value, scalaBinaryVersion.value).map(_(art.name)) getOrElse art.name
else art.name
if (sbtPlugin.value)
if (legacyPluginLayout) art.name
else {
"%s_%s".format(defaultArtifactId, (pluginCrossBuild / sbtBinaryVersion).value)
}
else defaultArtifactId
val coords = MavenCoordinates(organization.value, artifactId, theVersion, None, art.extension)
if (sbtPlugin.value)
coords.sbtPlugin().withSbtVersion((pluginCrossBuild / sbtBinaryVersion).value).withScalaVersion(scalaBinaryVersion.value)
Expand All @@ -84,6 +101,7 @@ object AetherPlugin extends AutoPlugin {
publishTo.value,
aetherLocalRepo.value,
aetherArtifact.value,
(aetherLegacyPluginStyle ?? true).value,
sbtPlugin.value,
credentials.value,
aetherCustomHttpHeaders.value
Expand All @@ -98,24 +116,36 @@ object AetherPlugin extends AutoPlugin {

lazy val installTask = aetherInstall := Def
.task {
installIt(aetherArtifact.value, aetherLocalRepo.value)(streams.value)
installIt(aetherArtifact.value, aetherLocalRepo.value, (aetherLegacyPluginStyle ?? true).value)(streams.value)
}
.tag(Tags.Publish, Tags.Network)
.value

def createArtifact(artifacts: Map[Artifact, sbt.File], coords: MavenCoordinates, mainArtifact: File): AetherArtifact = {
val subArtifacts = artifacts
.filterNot { case (a, f) => a.classifier.isEmpty && f == mainArtifact }
.map { case (a, f) => AetherSubArtifact(f, a.classifier, a.extension) }
.toSeq
def createArtifact(
artifacts: Map[Artifact, sbt.File],
legacyPlugin: Boolean,
coords: MavenCoordinates,
mainArtifact: File
): AetherArtifact = {
val prefiltered = artifacts.filterNot { case (a, f) =>
(a.name == coords.artifactId && coords.isPlugin && legacyPlugin) || mainArtifact == f || (a.classifier.isEmpty && a.extension == "jar")
}

val subArtifacts = prefiltered.map { case (art, f) =>
AetherSubArtifact(f, art.classifier, art.extension)
}.toList

val realCoords = coords.withExtension(mainArtifact)

AetherArtifact(mainArtifact, realCoords, subArtifacts)
}

private def toRepository(repo: MavenRepository, plugin: Boolean, credentials: Option[DirectCredentials]): RemoteRepository = {
val builder: Builder = new Builder(repo.name, if (plugin) "sbt-plugin" else "default", repo.root)
private def toRepository(
repo: MavenRepository,
overridePluginType: Boolean,
credentials: Option[DirectCredentials]
): RemoteRepository = {
val builder: Builder = new Builder(repo.name, if (overridePluginType) "sbt-plugin" else "default", repo.root)
credentials.foreach { c =>
builder.setAuthentication(new AuthenticationBuilder().addUsername(c.userName).addPassword(c.passwd).build())
}
Expand All @@ -128,6 +158,7 @@ object AetherPlugin extends AutoPlugin {
repo: Option[Resolver],
localRepo: File,
artifact: AetherArtifact,
legacyPluginLayout: Boolean,
plugin: Boolean,
cred: Seq[Credentials],
customHeaders: Map[String, String]
Expand All @@ -151,27 +182,27 @@ object AetherPlugin extends AutoPlugin {
}.toOption.flatten

val request = new DeployRequest()
request.setRepository(toRepository(repository, plugin, maybeCred))
request.setRepository(toRepository(repository, plugin && legacyPluginLayout, maybeCred))
val parent = artifact.toArtifact
request.addArtifact(parent)
artifact.subartifacts.foreach(s => request.addArtifact(s.toArtifact(parent)))

Booter.deploy(localRepo, stream, artifact.coordinates, customHeaders, request) match {
Booter.deploy(legacyPluginLayout, localRepo, stream, artifact.coordinates, customHeaders, request) match {
case Success(_) => ()
case Failure(ex) =>
ex.printStackTrace()
throw ex
}
}

def installIt(artifact: AetherArtifact, localRepo: File)(implicit streams: TaskStreams) {
def installIt(artifact: AetherArtifact, localRepo: File, legacyPluginLayout: Boolean)(implicit streams: TaskStreams) {

val request = new InstallRequest()
val parent = artifact.toArtifact
request.addArtifact(parent)
artifact.subartifacts.foreach(s => request.addArtifact(s.toArtifact(parent)))

Booter.install(localRepo, streams, artifact.coordinates, request) match {
Booter.install(legacyPluginLayout, localRepo, streams, artifact.coordinates, request) match {
case Success(_) => ()
case Failure(ex) =>
ex.printStackTrace()
Expand Down
35 changes: 26 additions & 9 deletions aether-deploy/src/main/scala/aether/internal/Booter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ package internal
import java.io.File
import org.apache.maven.repository.internal.{
DefaultArtifactDescriptorReader,
DefaultModelCacheFactory,
DefaultVersionRangeResolver,
DefaultVersionResolver,
ModelCacheFactory,
OverrideSnapshotMetadataGeneratorFactory,
OverrideVersionsMetadataGeneratorFactory
OverrideVersionsMetadataGeneratorFactory,
SnapshotMetadataGeneratorFactory,
VersionsMetadataGeneratorFactory
}
import org.eclipse.aether.deployment.DeployRequest
import org.eclipse.aether.{ConfigurationProperties, DefaultRepositorySystemSession, RepositorySystem}
import org.eclipse.aether.impl.*
import org.eclipse.aether.installation.InstallRequest
import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory
import org.eclipse.aether.repository.{LocalRepository, ProxySelector}
import org.eclipse.aether.spi.connector.transport.TransporterFactory
import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory
Expand All @@ -29,19 +34,28 @@ import scala.util.Try
object Booter {

@nowarn("cat=deprecation")
private def newRepositorySystem(): RepositorySystem = {
private def newRepositorySystem(legacyPluginLayout: Boolean): RepositorySystem = {
val locator = new DefaultServiceLocator()
locator.addService(classOf[RepositoryLayoutFactory], classOf[SbtPluginLayoutFactory])
locator.addService(classOf[VersionResolver], classOf[DefaultVersionResolver])
locator.addService(classOf[VersionRangeResolver], classOf[DefaultVersionRangeResolver])
locator.addService(classOf[ArtifactDescriptorReader], classOf[DefaultArtifactDescriptorReader])
locator.addService(classOf[ModelCacheFactory], classOf[DefaultModelCacheFactory])

locator.addService(
classOf[RepositoryConnectorFactory],
classOf[org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory]
)
locator.setServices(classOf[ProxySelector], SystemPropertyProxySelector())
locator.addService(classOf[MetadataGeneratorFactory], classOf[OverrideSnapshotMetadataGeneratorFactory])
locator.addService(classOf[MetadataGeneratorFactory], classOf[OverrideVersionsMetadataGeneratorFactory])

if (legacyPluginLayout) {
locator.addService(classOf[MetadataGeneratorFactory], classOf[OverrideSnapshotMetadataGeneratorFactory])
locator.addService(classOf[MetadataGeneratorFactory], classOf[OverrideVersionsMetadataGeneratorFactory])
locator.addService(classOf[RepositoryLayoutFactory], classOf[SbtPluginLayoutFactory])
} else {
locator.addService(classOf[MetadataGeneratorFactory], classOf[SnapshotMetadataGeneratorFactory])
locator.addService(classOf[MetadataGeneratorFactory], classOf[VersionsMetadataGeneratorFactory])
locator.addService(classOf[RepositoryLayoutFactory], classOf[Maven2RepositoryLayoutFactory])
}
locator.addService(classOf[ArtifactDescriptorReader], classOf[DefaultArtifactDescriptorReader])

addTransporterFactories(locator)

Expand All @@ -58,22 +72,24 @@ object Booter {
}

private def init(
legacyPluginLayout: Boolean,
localRepoDir: File,
streams: TaskStreams[_],
coordinates: MavenCoordinates
): (RepositorySystem, DefaultRepositorySystemSession) = {
val system = newRepositorySystem()
val system = newRepositorySystem(legacyPluginLayout)
system -> newSession(system, localRepoDir, streams, coordinates)
}

def deploy(
legacyPluginLayout: Boolean,
localRepoDir: File,
streams: TaskStreams[_],
coordinates: MavenCoordinates,
customHeaders: Map[String, String],
request: DeployRequest
): Try[Unit] = Try {
val (system, session) = init(localRepoDir, streams, coordinates)
val (system, session) = init(legacyPluginLayout, localRepoDir, streams, coordinates)
if (customHeaders.nonEmpty) {
session
.setConfigProperty(ConfigurationProperties.HTTP_HEADERS + "." + request.getRepository.getId, mapAsJavaMap(customHeaders))
Expand All @@ -82,12 +98,13 @@ object Booter {
}

def install(
legacyPluginLayout: Boolean,
localRepoDir: File,
streams: TaskStreams[_],
coordinates: MavenCoordinates,
request: InstallRequest
): Try[Unit] = Try {
val (system, session) = init(localRepoDir, streams, coordinates)
val (system, session) = init(legacyPluginLayout, localRepoDir, streams, coordinates)
system.install(session, request)
}

Expand Down
13 changes: 13 additions & 0 deletions aether-deploy/src/sbt-test/deploy/sbt-plugin-legacy/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ThisBuild / version := "0.1"

name := "sbt-plugin-legacy"

organization := "legacy"

enablePlugins(SbtPlugin)

publishTo := Some("foo" at (file(".") / "target" / "repo").toURI.toURL.toString)

overridePublishSettings

aether.AetherKeys.aetherLegacyPluginStyle := true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
val pluginVersion = scala.util.Properties.propOrNone("plugin.version").getOrElse(
throw new RuntimeException("""
|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.
""".stripMargin))

addSbtPlugin("no.arktekk.sbt" % "aether-deploy" % pluginVersion)
13 changes: 13 additions & 0 deletions aether-deploy/src/sbt-test/deploy/sbt-plugin-legacy/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Deploy to file url
> publish
$ exists target/repo/legacy
$ exists target/repo/legacy/sbt-plugin-legacy_2.12_1.0/maven-metadata.xml
$ exists target/repo/legacy/sbt-plugin-legacy_2.12_1.0/0.1/
$ exists target/repo/legacy/sbt-plugin-legacy_2.12_1.0/0.1/sbt-plugin-legacy-0.1.jar
$ exists target/repo/legacy/sbt-plugin-legacy_2.12_1.0/0.1/sbt-plugin-legacy-0.1.pom
$ exists target/repo/legacy/sbt-plugin-legacy_2.12_1.0/0.1/sbt-plugin-legacy-0.1-sources.jar
$ exists target/repo/legacy/sbt-plugin-legacy_2.12_1.0/0.1/sbt-plugin-legacy-0.1-javadoc.jar
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1.jar
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1.pom
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1-sources.jar
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1-javadoc.jar
6 changes: 4 additions & 2 deletions aether-deploy/src/sbt-test/deploy/sbt-plugin/build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
ThisBuild / version := "0.1"
ThisBuild / version := "0.1"

name := "sbt-plugin"

organization := "sbt-plugin"

enablePlugins(SbtPlugin)

publishTo := Some("foo" at (file(".") / "target" / "repo").toURI.toURL.toString)
publishTo := Some("foo" at (file(".") / "target" / "repo").toURI.toURL.toString)

overridePublishSettings

aether.AetherKeys.aetherLegacyPluginStyle := false
12 changes: 8 additions & 4 deletions aether-deploy/src/sbt-test/deploy/sbt-plugin/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
$ exists target/repo/sbt-plugin
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/maven-metadata.xml
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1.jar
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1.pom
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1-sources.jar
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1-javadoc.jar
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1.jar
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1.pom
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1-sources.jar
$ exists target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin_2.12_1.0-0.1-javadoc.jar
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1.jar
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1.pom
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1-sources.jar
$ absent target/repo/sbt-plugin/sbt-plugin_2.12_1.0/0.1/sbt-plugin-0.1-javadoc.jar
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ lazy val aetherDeploy = (project in file("aether-deploy"))
.settings(
name := "aether-deploy",
libraryDependencies ++= {
val mavenVersion = "3.8.7"
val mavenResolverVersion = "1.9.2"
val mavenVersion = "3.9.4"
val mavenResolverVersion = "1.9.15"
Seq(
"org.apache.maven" % "maven-resolver-provider" % mavenVersion,
"org.apache.maven.resolver" % "maven-resolver-api" % mavenResolverVersion,
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.2
sbt.version=1.9.2

0 comments on commit a3882d2

Please sign in to comment.