Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide impl of matchesSemVer #125

Closed
da-tubi opened this issue Apr 2, 2022 · 2 comments · Fixed by #128
Closed

Provide impl of matchesSemVer #125

da-tubi opened this issue Apr 2, 2022 · 2 comments · Fixed by #128
Assignees
Labels
Milestone

Comments

@da-tubi
Copy link

da-tubi commented Apr 2, 2022

Task

Summary

See ThoughtWorksInc/enableIf.scala#72
We need a zero-dependency SemVer parsing and comparing library in enableIf.scala.

It would be nice to maintain the impl of matchesSemVer (from sbt) in just-semver.

Project Details

Version: latest

Description

SemVer.parse("2.13.0").matches("=2.13")
SemVer.parse("2.13.0").matches(">=2.13")
...
@kevin-lee
Copy link
Owner

Oh, I see. Yeah that seems like a good method to add. I'll take care of it.

@kevin-lee
Copy link
Owner

@da-tubi OK, it's done. I still need to clean up little bit but I think I can release it soon. It took me quite a while to write tests. It works similar to sbt's but not the same. How the matcher combination is evaluated might be the same I think.

>=A // comparison
>A <B // comparison AND comparison
>A || =B // comparison OR comparison 
A - B // range. Same as >=A <=B
>A <B || >=C <=D // (comparison AND comparison) OR (comparison AND comparison)
A - B || >=C <=D // range OR (comparison AND comparison)
SemVer.parseUnsafe("2.13.0").unsafeMatches(">=2.13.0") // true
SemVer.parseUnsafe("2.13.0").unsafeMatches(">2.13.0") // false
SemVer.parseUnsafe("2.13.0").unsafeMatches(">=2.13.0") // true

SemVer.parseUnsafe("2.12.0").unsafeMatches("2.12.0 - 2.13.0") // true
SemVer.parseUnsafe("2.13.0").unsafeMatches("2.12.0 - 2.13.0") // true
SemVer.parseUnsafe("2.13.1").unsafeMatches("2.12.0 - 2.13.0") // false

SemVer.parseUnsafe("2.12.0").unsafeMatches(">=2.12.0 <2.14.0") // true
SemVer.parseUnsafe("2.13.0").unsafeMatches(">=2.12.0 <2.14.0") // true
SemVer.parseUnsafe("2.13.8").unsafeMatches(">=2.12.0 <2.14.0") // true

SemVer.parseUnsafe("2.11.0").unsafeMatches("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // true
SemVer.parseUnsafe("2.12.9").unsafeMatches("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // false
SemVer.parseUnsafe("2.12.8").unsafeMatches("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // true

SemVer.parseUnsafe("2.13.0").unsafeMatches("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // true
SemVer.parseUnsafe("3.0.0").unsafeMatches("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // true
SemVer.parseUnsafe("3.1.0").unsafeMatches("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // false

NOTE: just-semver supports only proper SemVer requiring major.minor.patch so do decimal versioning (x.y) is supported (at least not yet).


FYI, Safe matches (.matches()) requires SemVerMatchers instead of String.
e.g.)

SemVer.parseUnsafe("2.13.0").matches(SemVerMatchers.unsafeParse("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0"))

Or a completely safe way would be

val matchers =
  SemVerMatchers.parse("2.11.0 - 2.12.8 || >=2.13.0 <3.1.0") // Either[List[SemVerMatcher.ParseError], SemVerMatchers]
    .left.map(_.map(_.render).mkString(",")) // Either[String, SemVerMatchers]
//            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I'll do something about it.
 
SemVer.parse("3.1.0") // Either[ParseError, SemVer]
  .left.map(_.render) // Either[String, SemVer]
  .flatMap { v =>
    matchers.map(v.matches) // Either[String, Boolean]
  } // Either[String, Boolean]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants