Skip to content

Commit

Permalink
Parse versions prefixed with "v"
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimush committed Jun 27, 2021
1 parent df07595 commit 7341f9d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
17 changes: 10 additions & 7 deletions src/main/scala/com/timushev/sbt/updates/versions/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,20 @@ object Version {
}

object VersionParser extends RegexParsers {
private val token = """[^-+.]+""".r
private val number = """\d{1,18}(?=[-+.]|$)""".r ^^ (_.toLong)
private val plusAsPatchValue = """\+""".r ^^ (_ => Long.MaxValue)
private val token = """[^-+.]+""".r
private val number = """\d{1,18}(?=[-+.]|$)""".r ^^ (_.toLong)
private val plusAsNumber = """\+""".r ^^ (_ => Long.MaxValue)

private val numericPart: Parser[List[Long]] =
number ~ ("." ~> (plusAsNumber ^^ (List(_)) | numericPart)).? ^^ {
case h ~ Some(t) => h :: t
case h ~ None => List(h)
}

private val numericPart: Parser[List[Long]] = number ~ ("." ~> number) ~ ("." ~> (number | plusAsPatchValue)).* ^^ {
case h ~ m ~ t => h :: m :: t
}
private val part: Parser[List[String]] = token ~ (("." | "-") ~> token).* ^^ { case h ~ t => h :: t }

private val version: Parser[(List[Long], List[String], List[String])] =
numericPart ~ (("." | "-") ~> part).? ~ ("+" ~> part).? ^^ { case a ~ b ~ c =>
"v".? ~> numericPart ~ (("." | "-") ~> part).? ~ ("+" ~> part).? ^^ { case a ~ b ~ c =>
(a, b.getOrElse(Nil), c.getOrElse(Nil))
}

Expand Down
24 changes: 7 additions & 17 deletions src/test/scala/com/timushev/sbt/updates/versions/VersionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ class VersionSpec extends AnyFreeSpec with Matchers {
b should equal("build" :: "10" :: Nil)
case _ => fail("not a build version")
}

Version("2.0.2+9-4e5b95f4-SNAPSHOT") match { // Sbt-Dynver style snapshot version
case SnapshotVersion(r, p, b) =>
r should equal(2 :: 0 :: 2 :: Nil)
case _ => fail("not a snaphshot version")
case _ => fail("not a snapshot version")
}
}
"should be ordered according to the semantic versioning spec" in {
Expand Down Expand Up @@ -88,21 +87,6 @@ class VersionSpec extends AnyFreeSpec with Matchers {
case other => fail(other.toString)
}
}

"should reject versions like 1.+.+, 1.+.0, +.0.0" in {
VersionParser.parse("1.+.+") match {
case VersionParser.Failure(_, _) =>
case other => fail(other.toString)
}
VersionParser.parse("1.+.0") match {
case VersionParser.Failure(_, _) =>
case other => fail(other.toString)
}
VersionParser.parse("+.0.0") match {
case VersionParser.Failure(_, _) =>
case other => fail(other.toString)
}
}
"should parse versions like 1.0.3m" in {
VersionParser.parse("1.0.3m") match {
case VersionParser.Success((1 :: 0 :: Nil, "3m" :: Nil, Nil), _) =>
Expand All @@ -119,6 +103,12 @@ class VersionSpec extends AnyFreeSpec with Matchers {
case other => fail(other.toString)
}
}
"should parse versions like v3-rev411-1.25.0" in {
VersionParser.parse("v3-rev411-1.25.0") match {
case VersionParser.Success((3 :: Nil, "rev411" :: "1" :: "25" :: "0" :: Nil, Nil), _) =>
case other => fail(other.toString)
}
}
}
}

Expand Down

0 comments on commit 7341f9d

Please sign in to comment.