Skip to content

Commit

Permalink
add a setting for repositories to fetch artifact metadata from
Browse files Browse the repository at this point in the history
  • Loading branch information
huajiang-tubi committed Jun 25, 2024
1 parent 40231d9 commit 274f6cf
Show file tree
Hide file tree
Showing 24 changed files with 71 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sbtversionpolicy

import scala.collection.JavaConverters.*
import scala.util.control.NoStackTrace
import sbt.{AutoPlugin, Def, Keys, settingKey, taskKey}
import sbt.{AutoPlugin, Def, Keys, PluginTrigger, Plugins, settingKey, taskKey}
import sbt.KeyRanks.Invisible
import sbt.librarymanagement.{CrossVersion, ModuleID}
import coursier.version.{Previous, Version, VersionCompatibility}
Expand All @@ -11,12 +11,17 @@ import MimaPlugin.autoImport.mimaPreviousArtifacts

object SbtVersionPolicyMima extends AutoPlugin {

override def trigger = allRequirements
override def requires = MimaPlugin
override def trigger: PluginTrigger = allRequirements
override def requires: Plugins = MimaPlugin && SbtVersionPolicyPlugin

object autoImport {
val versionPolicyPreviousVersions = settingKey[Either[Throwable, Seq[String]]]("Previous versions to check compatibility against.")
val versionPolicyFirstVersion = settingKey[Option[String]]("First version this module was or will be published for.")
val versionPolicyPreviousVersionRepositories = settingKey[PreviousVersionRepositories]("Repositories from which to look for previous versions of the artifact. The value can be either `CoursierDefaultRepositories` or `SbtResolvers(Set(resolverNames))`")

sealed trait PreviousVersionRepositories
case object CoursierDefaultRepositories extends PreviousVersionRepositories
case class SbtResolvers(resolverNames: Set[String]) extends PreviousVersionRepositories

private[sbtversionpolicy] val getVersionPolicyPreviousVersions =
taskKey[Seq[String]]("Get previous versions or throw the error if it failed to resolve at load time").withRank(Invisible)
Expand All @@ -36,27 +41,52 @@ object SbtVersionPolicyMima extends AutoPlugin {

private lazy val previousVersionsFromRepo = Def.setting[Either[Throwable, Seq[String]]] {

val version = Keys.version.value
val projId = Keys.projectID.value
val sv = Keys.scalaVersion.value
val sbv = Keys.scalaBinaryVersion.value
val log = Keys.sLog.value
val name = moduleName(projId, sv, sbv)

val ivyProps = sbtversionpolicy.internal.Resolvers.defaultIvyProperties(Keys.ivyPaths.value.ivyHome)
val repos = Keys.resolvers.value.flatMap { res =>
val repoOpt = sbtversionpolicy.internal.Resolvers.repository(res, ivyProps, s => System.err.println(s))
if (repoOpt.isEmpty)
System.err.println(s"Warning: ignoring repository ${res.name} to get previous version")
repoOpt.toSeq

val repos = versionPolicyPreviousVersionRepositories.?.value match {
case None =>
// when it is necessary to fetch the artifact metadata but the repositories is not configured
// we throw instead of fallback to some defaults, because a mis-configuration of the setting
// may let the version policy check pass when it should not.
sys.error(
s"""Previous version cannot be calculated from the the current version $name:$version.
|Please set `versionPolicyPreviousVersionRepositories` for where to fetch the artifact metadata.
|Run `inspect versionPolicyPreviousVersionRepositories` for description of valid values.
|""".stripMargin
)

case Some(CoursierDefaultRepositories) =>
val repositories = coursierapi.Repository.defaults().asScala
log.info(s"Getting previous versions of $name from coursier default repositories: ${repositories.mkString("\n", "\n", "")}")
repositories

case Some(SbtResolvers(resolverNames)) =>
log.info(s"Getting previous versions of $name from resolvers: ${resolverNames.mkString("\n", "\n", "")}")
val resolvers = Keys.resolvers.value.map(res => res.name -> res).toMap
val previousVersionResolvers = resolverNames.collect(resolvers).toSeq
val unrecognizedResolvers = resolverNames -- previousVersionResolvers.map(_.name)
if (unrecognizedResolvers.nonEmpty) {
sys.error(s"""Unrecognized resolver names: ${unrecognizedResolvers.mkString(", ")}""")
}
previousVersionResolvers.flatMap { res =>
val repoOpt = sbtversionpolicy.internal.Resolvers.repository(res, ivyProps, s => System.err.println(s))
if (repoOpt.isEmpty)
sys.error(s"Warning: ignoring repository ${res.name} to get previous version")
repoOpt
}
}
// Can't reference Keys.fullResolvers, which is a task.
// So we're using the usual default repositories from coursier here…
val fullRepos = coursierapi.Repository.defaults().asScala ++ repos

val start = System.nanoTime()

val res = coursierapi.Versions.create()
.withRepositories(fullRepos: _*)
.withRepositories(repos: _*)
.withModule(coursierapi.Module.of(projId.organization, name))
.versions()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

// A project with two modules (“a” and “b”) and a root module that aggregates them

val v1_a =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lazy val a = project
inThisBuild(List(
scalaVersion := "2.12.11",
organization := "io.github.alexarchambault.sbtversionpolicy.test",
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
))

lazy val check = taskKey[Unit]("")

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionScheme := Some("semver-spec")
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val checkTasks = Seq(
TaskKey[Unit]("checkAssessedCompatibilityIsBinaryAndSourceCompatible") := {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ThisBuild / versionPolicyIntention := Compatibility.BinaryCompatible
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

lazy val v1 = project
.settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "ch.epfl.scala"
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val b1 = project
.settings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ inThisBuild(List(
scalaVersion := "2.12.11",
libraryDependencySchemes += "org.typelevel" %% "cats-kernel" % "semver-spec",
organization := "io.github.alexarchambault.sbtversionpolicy.test",
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
))
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ inThisBuild(List(
)
),
scalaVersion := "3.0.1",
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
versionPolicyIntention := Compatibility.BinaryAndSourceCompatible
))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ThisBuild / versionPolicyIntention := Compatibility.None
// Don’t check dependencies to internal modules when the version is like `1.2.3+4-abcd1234` (which
// is typically the version value generated by sbt-dynver in-between releases)
ThisBuild / versionPolicyIgnoredInternalDependencyVersions := Some("^\\d+\\.\\d+\\.\\d+\\+\\d+".r)
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val a =
project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ inThisBuild(List(
url("https://github.com/julienrf")
)
),
scalaVersion := "3.0.1"
scalaVersion := "3.0.1",
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
))

val module =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ inThisBuild(List(
url("https://github.com/julienrf")
)
),
scalaVersion := "3.0.1"
scalaVersion := "3.0.1",
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
))

val module = project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val v1_a =
project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

lazy val a = project
.settings(
Expand All @@ -22,4 +23,3 @@ lazy val b = project
)

lazy val check = taskKey[Unit]("")

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ThisBuild / scalaVersion := "2.12.19"
ThisBuild / organization := "com.example"
ThisBuild / versionPolicyIntention := Compatibility.BinaryAndSourceCompatible
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val a_1 =
project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ inThisBuild(List(
scalaVersion := "2.12.11",
organization := "io.github.alexarchambault.sbtversionpolicy.test2",
versionPolicyIntention := Compatibility.BinaryCompatible,
libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % "early-semver"
libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % "early-semver",
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
))

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ThisBuild / scalaVersion := "2.13.8"
ThisBuild / resolvers += Resolver.typesafeIvyRepo("releases")
ThisBuild / versionPolicyIntention := Compatibility.None
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val root =
project.in(file("."))
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val `v0-1-0` =
project.settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionScheme := Some("pvp")
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val `v0-1-0` =
project.settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionScheme := Some("semver-spec")
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val `v0-1-0` =
project.settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

lazy val core = project
.settings(
crossVersion := CrossVersion.disabled,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "3.0.0-RC3"
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val `v1-0-0` =
project.settings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ lazy val h = project
inThisBuild(List(
scalaVersion := "2.12.11",
organization := "io.github.alexarchambault.sbtversionpolicy.test",
versionPolicyIntention := Compatibility.BinaryCompatible
versionPolicyIntention := Compatibility.BinaryCompatible,
versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories,
))

lazy val check = taskKey[Unit]("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val v100 =
project.in(file("v1-0-0")).settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / versionPolicyPreviousVersionRepositories := CoursierDefaultRepositories

val `v0-1-0` =
project.settings(
Expand Down

0 comments on commit 274f6cf

Please sign in to comment.