Giter VIP home page Giter VIP logo

jsemver's Introduction

jsemver

Kotlin implementation of the Semantic Versioning specification. Can be used from Java seamlessly.

The goal is to strictly comply with the SemVer parsing and precedence rules, and provide an easy-to-use API. Specifically it provides the ability to:

  • Instantiate/parse semantic versions
  • Compare/sort semantic versions
  • Check if a semantic version fits within a set of constraints

The grammar can be seen here; it is consistent with the BNF grammar in the specification.

Installation

You can find the latest version on Bintray. Download

I'll consider publishing to Maven Central and jcenter if people start using this library and find it useful.

Usage

Parse semantic versions

A valid semantic version is represented by the SemVer class. The easiest way to instantiate it is by parsing a string:

SemVer.parseStr("1.0.0")

SemVer comprises NormalVersion, and optionally, PreReleaseVersion and BuildMetadata. The objects are immutable but there are various builder methods that return new instances after appropriate modification.

More ways to instantiate a SemVer:

SemVer(NormalVersion(1, 0, 0))
SemVer(NormalVersion("1", "0", "0"))
SemVer(NormalVersion(1, 0, 0), PreReleaseVersion("alpha"), BuildMetadata(1))
v
  .withMajorVersion(2).withMinorVersion(1).withPatchVersion(1)
  .withPreReleaseVersion("alpha").withBuildMetadata(1)

v.withPreReleaseVersion() (empty) will remove the pre-release version. Same for v.withBuildMetadata().

See the KDoc for more details. This project has almost 100% test coverage, you're also welcome to look at the unit tests.

Compare/sort semantic versions

Other than BuildMetadata, the rest implement equals, hashCode, and Comparable.

From Kotlin code, you can use the usual comparison operators:

SemVer.parse("1.0.0") < SemVer.parse("1.0.1")
SemVer.parse("1.0.1") >= SemVer.parse("1.0.0")

From Java, use compareTo explicitly.

Check if a semantic version fits within a set of constraints

I don't see the point in providing a DSL for checking complicated constraints, because all of those can be rewritten using compareTo. For example:

v.satisfies(">1.2.2") can be rewritten as v > SemVer.parse("1.2.2")
v.satisfies("1.2.+") can be rewritten as v >= SemVer.parse("1.2.0")
v.satisfies("(,1.8.9]") can be rewritten as v <= SemVer.parse("1.8.9")
v.satisfies("1.0-2.0") can be rewritten as v >= SemVer.parse("1.0.0") && v <= SemVer.parse("2.0.0")

See Semver cheatsheet for more examples.

Contribute

This project is a volunteer effort. You are welcome to send pull requests, ask questions, or create issues. If you like it, you can help by spreading the word!

License

Copyright 2020 Abhijit Sarkar - Released under Apache License v2.0.

jsemver's People

Contributors

asarkar avatar

Stargazers

Alessio Stalla avatar ☁️dungsil avatar

Watchers

James Cloos avatar  avatar  avatar  avatar

jsemver's Issues

Logback should not be a runtime transitive dependency

As noted in the SLF4J documentation, libraries should not impose a logging framework on their end-users:

Basic rule: Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api.

This is so that the end-user can either choose the desired logging framework for their application, or they themselves can conform to this rule when writing a library. In my case, since I am writing an IntelliJ IDEA plugin, I cannot include a backing logging framework in my dependency graph. While I personally prefer logback, I must use the logging framework built-in to the application. When I use jsemver out of the box, I'm getting an SLF4J multiple bindings error. While this is solved by adding an exclude to a build, it does place an unnecessary burden on the user of the jsemver library.

The easiest solution would be to make logback a testRuntimeOnly dependency and move the logback.xml file to src\test\resources.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.