From 8d88ba12863c53adaab6a761b014736a362a2d4b Mon Sep 17 00:00:00 2001 From: AI Date: Fri, 30 Jun 2023 11:09:51 +0800 Subject: [PATCH] Fixed cannot get release version --- project/Dependencies.scala | 1 + .../org/jmotor/sbt/artifact/Versions.scala | 50 +++++++++---- .../sbt/service/VersionServiceImpl.scala | 70 +++++++------------ .../jmotor/sbt/artifact/VersionsSpec.scala | 52 ++++++++++++++ .../org/jmotor/sbt/util/VersionSpec.scala | 35 ---------- 5 files changed, 116 insertions(+), 92 deletions(-) create mode 100644 src/test/scala/org/jmotor/sbt/artifact/VersionsSpec.scala delete mode 100644 src/test/scala/org/jmotor/sbt/util/VersionSpec.scala diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 6ccad79..c97eb81 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -12,6 +12,7 @@ object Dependencies { val okhttp = "4.11.0" val jacksonModuleScala = "2.15.2" val scalaXml = "2.1.0" + // val scalaXml = "1.3.0" val mavenArtifact = "3.9.3" } diff --git a/src/main/scala/org/jmotor/sbt/artifact/Versions.scala b/src/main/scala/org/jmotor/sbt/artifact/Versions.scala index b7230b6..df283aa 100644 --- a/src/main/scala/org/jmotor/sbt/artifact/Versions.scala +++ b/src/main/scala/org/jmotor/sbt/artifact/Versions.scala @@ -1,31 +1,55 @@ package org.jmotor.sbt.artifact -import org.apache.maven.artifact.versioning.ArtifactVersion - -import java.util.regex.Pattern +import org.apache.maven.artifact.versioning.{ArtifactVersion, DefaultArtifactVersion} /** * Component: Description: Date: 2018/2/8 * * @author - * AI + * AI */ object Versions { - final lazy val UNRELEASED: Seq[String] = Seq("pr", "m", "beta", "rc", "alpha", "snapshot", "snap") + private final lazy val ReleaseFlags: Set[String] = Set("prod") + private final lazy val UnReleaseFlags: Set[String] = Set("pr", "m", "beta", "rc", "alpha", "snapshot", "snap") private[this] final lazy val jrePattern = s"jre\\d+".r.pattern - private[this] final lazy val UnreleasedPatterns: Seq[Pattern] = - Versions.UNRELEASED.map(q => s"$q[_-]?\\d+.*".r.pattern) + // private[this] final lazy val UnreleasedPatterns: Set[Pattern] = + // Versions.UnReleaseFlags.map(q => s".*$q[_-]?\\d+.*".r.pattern) + + def isReleaseVersion(version: ArtifactVersion): Boolean = { + val fullVersion = version.toString.toLowerCase + if (UnReleaseFlags.exists(f => fullVersion.endsWith(f))) { + false + } else { + Option(version.getQualifier) match { + case None => true + case Some(qualifier) => + val q = qualifier.toLowerCase + if (ReleaseFlags.contains(q)) { + true + } else { + !(Versions.UnReleaseFlags.contains(q) || UnReleaseFlags.exists(f => q.contains(f))) + } + } + } + } - def isReleaseVersion(version: ArtifactVersion): Boolean = - Option(version.getQualifier) match { - case None => true - case Some(qualifier) => - val q = qualifier.toLowerCase - !(Versions.UNRELEASED.contains(q) || UnreleasedPatterns.exists(_.matcher(q).matches())) + def latestRelease(versions: Seq[ArtifactVersion]): Option[ArtifactVersion] = { + val releases = versions.collect { + case av if isReleaseVersion(av) => + Option(av.getQualifier).fold(av) { + case q if isJreQualifier(q) => new DefaultArtifactVersion(av.toString.replace(q, "")) + case _ => av + } } + if (releases.nonEmpty) { + Some(releases.max) + } else { + None + } + } def isJreQualifier(qualifier: String): Boolean = { jrePattern.matcher(qualifier).matches() diff --git a/src/main/scala/org/jmotor/sbt/service/VersionServiceImpl.scala b/src/main/scala/org/jmotor/sbt/service/VersionServiceImpl.scala index ed52f17..454b788 100644 --- a/src/main/scala/org/jmotor/sbt/service/VersionServiceImpl.scala +++ b/src/main/scala/org/jmotor/sbt/service/VersionServiceImpl.scala @@ -23,78 +23,60 @@ import scala.util.control.NonFatal * Component: Description: Date: 2018/2/9 * * @author - * AI + * AI */ class VersionServiceImpl(logger: Logger, scalaVersion: String, scalaBinaryVersion: String, resolvers: Seq[Resolver]) - extends VersionService { + extends VersionService { private[this] lazy val groups = getLoaderGroups(resolvers) override def checkForUpdates(module: ModuleID): Future[ModuleStatus] = check(module) override def checkPluginForUpdates( - module: ModuleID, - sbtBinaryVersion: String, - sbtScalaBinaryVersion: String - ): Future[ModuleStatus] = + module: ModuleID, + sbtBinaryVersion: String, + sbtScalaBinaryVersion: String + ): Future[ModuleStatus] = check(module, Option(sbtBinaryVersion -> sbtScalaBinaryVersion)) private[this] def check(module: ModuleID, sbtSettings: Option[(String, String)] = None): Future[ModuleStatus] = { - val mv = new DefaultArtifactVersion(module.revision) - val released = Versions.isReleaseVersion(mv) - val qualifierOpt = if (released && Option(mv.getQualifier).isDefined) Option(mv.getQualifier) else None + val mv = new DefaultArtifactVersion(module.revision) groups.foldLeft(Future.successful(Seq.empty[String] -> Option.empty[ModuleStatus])) { (future, group) => future.flatMap { - case (_, opt @ Some(_)) => Future.successful(Seq.empty[String] -> opt) + case (_, opt@Some(_)) => Future.successful(Seq.empty[String] -> opt) case (errors, _) => group.getVersions(module, sbtSettings).map { case Nil => errors -> None case versions => - val (max: ArtifactVersion, status: Status.Value) = getModuleStatus(mv, released, qualifierOpt, versions) + val (max: ArtifactVersion, status: Status.Value) = getModuleStatus(mv, versions) Seq.empty[String] -> Option(ModuleStatus(module, status, max.toString)) } recover { - case NonFatal(_: ArtifactNotFoundException) => errors -> None - case NonFatal(t: MultiException) => (errors ++ t.getMessages) -> None - case NonFatal(t) => (errors :+ t.getLocalizedMessage) -> None + case NonFatal(_: ArtifactNotFoundException) => errors -> None + case NonFatal(t: MultiException) => (errors ++ t.getMessages) -> None + case NonFatal(t) => (errors :+ t.getLocalizedMessage) -> None } } } map { - case (_, Some(status)) => status + case (_, Some(status)) => status case (errors, _) if errors.nonEmpty => ModuleStatus(module, Status.Error, errors) - case _ => ModuleStatus(module, Status.NotFound) + case _ => ModuleStatus(module, Status.NotFound) } } - private def getModuleStatus( - mv: DefaultArtifactVersion, - released: Boolean, - qualifierOpt: Option[String], - versions: Seq[ArtifactVersion] - ) = { - val releases = versions.filter(Versions.isReleaseVersion) - val matches = qualifierOpt match { - case None => - releases.filter { av => - Option(av.getQualifier) match { - case None => true - case Some(qualifier) => !Versions.isJreQualifier(qualifier) + private def getModuleStatus(mv: DefaultArtifactVersion, versions: Seq[ArtifactVersion]) = { + val latestOpt = Versions.latestRelease(versions) + latestOpt match { + case None => mv -> Status.NotFound + case Some(latest) => + val status = if (!Versions.isReleaseVersion(mv)) { + Status.Unreleased + } else { + mv.compareTo(latest) match { + case 0 | 1 => Status.Success + case _ => Status.Expired } } - case Some(q) => releases.filter(av => Option(av.getQualifier).isDefined && q == av.getQualifier) - } - if (matches.isEmpty) { - versions.head -> Status.Unreleased - } else { - val max = matches.max - val status = if (!released) { - Status.Unreleased - } else { - mv.compareTo(max) match { - case 0 | 1 => Status.Success - case _ => Status.Expired - } - } - (max, status) + (latest, status) } } diff --git a/src/test/scala/org/jmotor/sbt/artifact/VersionsSpec.scala b/src/test/scala/org/jmotor/sbt/artifact/VersionsSpec.scala new file mode 100644 index 0000000..2590711 --- /dev/null +++ b/src/test/scala/org/jmotor/sbt/artifact/VersionsSpec.scala @@ -0,0 +1,52 @@ +package org.jmotor.sbt.artifact + +import org.apache.maven.artifact.versioning.DefaultArtifactVersion +import org.scalatest.funsuite.AnyFunSuite + +class VersionsSpec extends AnyFunSuite { + test("test rc version") { + assert(Versions.isReleaseVersion(new DefaultArtifactVersion("3.9.0"))) + assert(!Versions.isReleaseVersion(new DefaultArtifactVersion("3.9.0-rc-1"))) + assert(!Versions.isReleaseVersion(new DefaultArtifactVersion("2.0.0-RC2-1"))) + } + + test("get latest release") { + val versions = Seq( + new DefaultArtifactVersion("3.9.0"), + new DefaultArtifactVersion("3.9.0-jre9"), + new DefaultArtifactVersion("3.8.0-jre10") + ) + assert(Versions.latestRelease(versions).get.toString == "3.9.0") + } + + test("get latest version in unstable versions") { + val versions = Seq( + new DefaultArtifactVersion("2.6.17+48-86b000c3-SNAPSHOT"), + new DefaultArtifactVersion("4.4.5+9-89b0e0d8-SNAPSHOT"), + new DefaultArtifactVersion("5.0.0.Final-SNAPSHOT"), + ) + assert(Versions.latestRelease(versions).isEmpty) + } + + test("is released version") { + val v1 = new DefaultArtifactVersion("3.2.0-SNAP10") + assert(!Versions.isReleaseVersion(v1)) + val v2 = new DefaultArtifactVersion("3.2.0-PROD") + assert(Versions.isReleaseVersion(v2)) + val v3 = new DefaultArtifactVersion("3.2.0-pr10") + assert(!Versions.isReleaseVersion(v3)) + val v4 = new DefaultArtifactVersion("3.2.0-M1") + assert(!Versions.isReleaseVersion(v4)) + } + + test("scala m version") { + val v = new DefaultArtifactVersion("2.13.0-M4-pre-20d3c21") + assert(!Versions.isReleaseVersion(v)) + } + + test("is jre qualifier") { + assert(Versions.isJreQualifier("jre7")) + assert(!Versions.isJreQualifier("jrep")) + } + +} diff --git a/src/test/scala/org/jmotor/sbt/util/VersionSpec.scala b/src/test/scala/org/jmotor/sbt/util/VersionSpec.scala deleted file mode 100644 index 98559ce..0000000 --- a/src/test/scala/org/jmotor/sbt/util/VersionSpec.scala +++ /dev/null @@ -1,35 +0,0 @@ -package org.jmotor.sbt.util - -import org.apache.maven.artifact.versioning.DefaultArtifactVersion -import org.jmotor.sbt.artifact.Versions -import org.scalatest.funsuite.AnyFunSuite - -/** Component: Description: Date: 2018/10/10 - * - * @author - * AI - */ -class VersionSpec extends AnyFunSuite { - - test("is released version") { - val v1 = new DefaultArtifactVersion("3.2.0-SNAP10") - assert(!Versions.isReleaseVersion(v1)) - val v2 = new DefaultArtifactVersion("3.2.0-PROD") - assert(Versions.isReleaseVersion(v2)) - val v3 = new DefaultArtifactVersion("3.2.0-pr10") - assert(!Versions.isReleaseVersion(v3)) - val v4 = new DefaultArtifactVersion("3.2.0-M1") - assert(!Versions.isReleaseVersion(v4)) - } - - test("scala m version") { - val v = new DefaultArtifactVersion("2.13.0-M4-pre-20d3c21") - assert(!Versions.isReleaseVersion(v)) - } - - test("is jre qualifier") { - assert(Versions.isJreQualifier("jre7")) - assert(!Versions.isJreQualifier("jrep")) - } - -}