From 6e65aac53e66534cc424065747bcf8886046d1b4 Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Sat, 28 Dec 2024 10:57:31 +0100 Subject: [PATCH] parse Scala 3 nighlty versions --- .../internal/config/ScalaVersion.scala | 40 ++++++++++++++++--- .../tests/config/ScalaVersionSuite.scala | 19 ++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/scalafix-core/src/main/scala/scalafix/internal/config/ScalaVersion.scala b/scalafix-core/src/main/scala/scalafix/internal/config/ScalaVersion.scala index 3be088345..e5571422c 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/config/ScalaVersion.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/config/ScalaVersion.scala @@ -1,5 +1,8 @@ package scalafix.internal.config +import java.time.LocalDate +import java.time.format.DateTimeFormatter + import scala.util.Failure import scala.util.Success import scala.util.Try @@ -44,9 +47,20 @@ sealed trait ScalaVersion { case Minor(major, minorVersion) => s"${major.value}.${minorVersion}" case Patch(major, minorVersion, patchVersion) => s"${major.value}.${minorVersion}.$patchVersion" - case RC(major, minorVersion, patchVersion, rc) => - s"${major.value}.${minorVersion}.$patchVersion-RC$rc" - case Nightly(major, minorVersion, patchVersion, shortSha1) => + case RC(major, minorVersion, patchVersion, rcVersion) => + s"${major.value}.${minorVersion}.$patchVersion-RC$rcVersion" + // Scala 2 nightly + case Nightly( + major, + minorVersion, + patchVersion, + Some(rcVersion), + Some(dateVersion), + shortSha1 + ) => + s"${major.value}.${minorVersion}.$patchVersion-RC$rcVersion-bin-${dateVersion.format(yyyyMMdd)}-$shortSha1-NIGHTLY" + // Scala 3 nightly + case Nightly(major, minorVersion, patchVersion, _, _, shortSha1) => s"${major.value}.${minorVersion}.$patchVersion-bin-$shortSha1" } } @@ -92,6 +106,8 @@ object ScalaVersion { major: MajorVersion, minorVersion: Int, patchVersion: Int, + rcVersion: Option[Int], + dateVersion: Option[LocalDate], shortSha1Version: String ) extends ScalaVersion { override val minor: Some[Int] = Some(minorVersion) @@ -126,6 +142,7 @@ object ScalaVersion { } private val intPattern = """\d{1,2}""" + private val datePattern = """\d{8}""" private val shortSha1Pattern = """[0-9a-f]{7}""" private val FullVersion = @@ -133,15 +150,26 @@ object ScalaVersion { private val RcVersion = raw"""($intPattern)\.($intPattern)\.($intPattern)-RC($intPattern)""".r private val NightlyVersion = - raw"""($intPattern)\.($intPattern)\.($intPattern)-bin-($shortSha1Pattern)""".r + raw"""($intPattern)\.($intPattern)\.($intPattern)(?:-RC($intPattern))?-bin(?:-($datePattern))?-($shortSha1Pattern)(?:-NIGHTLY)?""".r private val MajorPattern = raw"""($intPattern)""".r private val PartialVersion = raw"""($intPattern)\.($intPattern)""".r + private val yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd") + def from(version: String): Try[ScalaVersion] = { version match { - case NightlyVersion(major, minor, patch, shortSha1) => + case NightlyVersion(major, minor, patch, rc, date, shortSha1) => MajorVersion.from(major.toLong).flatMap { major => - Success(Nightly(major, minor.toInt, patch.toInt, shortSha1)) + Success( + Nightly( + major, + minor.toInt, + patch.toInt, + Try(rc.toInt).toOption, + Try(LocalDate.parse(date, yyyyMMdd)).toOption, + shortSha1 + ) + ) } case RcVersion(major, minor, patch, rc) => MajorVersion.from(major.toLong).flatMap { major => diff --git a/scalafix-tests/unit/src/test/scala/scalafix/tests/config/ScalaVersionSuite.scala b/scalafix-tests/unit/src/test/scala/scalafix/tests/config/ScalaVersionSuite.scala index a21ddd123..380fe8af9 100644 --- a/scalafix-tests/unit/src/test/scala/scalafix/tests/config/ScalaVersionSuite.scala +++ b/scalafix-tests/unit/src/test/scala/scalafix/tests/config/ScalaVersionSuite.scala @@ -22,7 +22,8 @@ class ScalaVersionSuite extends munit.FunSuite { test("parse: 2.13.16-bin-ce78754") { val scala2 = ScalaVersion.from("2.13.16-bin-ce78754") - assert(scala2.get == Nightly(Major.Scala2, 13, 16, "ce78754")) + assert(scala2.get == Nightly(Major.Scala2, 13, 16, None, None, "ce78754")) + assert(scala2.get.value == "2.13.16-bin-ce78754") } test("parse: 3.0.0-RC3") { @@ -30,6 +31,22 @@ class ScalaVersionSuite extends munit.FunSuite { assert(scala3.get == RC(Major.Scala3, 0, 0, 3)) } + test("parse: 3.6.0-RC1-bin-20241008-3408ed7-NIGHTLY") { + val scala3 = ScalaVersion.from("3.6.0-RC1-bin-20241008-3408ed7-NIGHTLY") + assert( + scala3.get == Nightly( + Major.Scala3, + 6, + 0, + Some(1), + Some(java.time.LocalDate.parse("2024-10-08")), + "3408ed7" + ) + ) + println(scala3.get.value) + assert(scala3.get.value == "3.6.0-RC1-bin-20241008-3408ed7-NIGHTLY") + } + test("parse failure: 3.0.0RC3") { val scala3 = ScalaVersion.from("3.0.0RC3") assert(scala3.isFailure)