Skip to content

Commit

Permalink
parse Scala 3 nighlty versions
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Dec 28, 2024
1 parent a18fe12 commit 6e65aac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -126,22 +142,34 @@ object ScalaVersion {
}

private val intPattern = """\d{1,2}"""
private val datePattern = """\d{8}"""
private val shortSha1Pattern = """[0-9a-f]{7}"""

private val FullVersion =
raw"""($intPattern)\.($intPattern)\.($intPattern)""".r
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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,31 @@ 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") {
val scala3 = ScalaVersion.from("3.0.0-RC3")
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)
Expand Down

0 comments on commit 6e65aac

Please sign in to comment.