version (ex_stdlib v0.3.0)

View Source

Semantic versioning (https://semver.org), inspired by Elixir's Version module.

A version is a map with the keys major, minor, patch, pre (a list of pre-release identifiers, integers or binaries) and build (a binary or undefined):

   {ok, #{major := 2, minor := 0, patch := 1, pre := [<<"alpha">>, 1]}} =
       version:parse(<<"2.0.1-alpha.1">>),
   lt = version:compare(<<"1.0.0-beta">>, <<"1.0.0-rc1">>),
   true = version:match(<<"2.1.6">>, <<"~> 2.1.2">>).

Requirements

A requirement is made of one or more operator/version pairs joined by and or or, such as ">= 2.0.0 and < 2.1.0". The operators are ==, != (deprecated in Elixir), >, >=, <, <= and ~>. A version without an operator means ==.

~> allows the last given component to increase:

  • ~> 2.0.0 is >= 2.0.0 and < 2.1.0-0
  • ~> 2.1.2 is >= 2.1.2 and < 2.2.0-0
  • ~> 2.1 is >= 2.1.0 and < 3.0.0-0
  • ~> 2.1-beta is >= 2.1.0-beta and < 3.0.0-0

Build segments (+...) are ignored when comparing and matching.

Summary

Functions

Compares two versions, returning gt, eq or lt.

Compiles a requirement for faster matching. Requirements are already stored in their matching form, so this returns it unchanged; it exists for API compatibility.

Checks if the version matches the requirement, allowing pre-releases.

Checks if the version matches the requirement.

Parses a version string.

Parses a requirement string.

Converts a version to a string.

Types

match_option/0

-type match_option() :: {allow_pre, boolean()}.

pre_identifier/0

-type pre_identifier() :: non_neg_integer() | binary().

requirement/0

-opaque requirement()

version/0

-type version() ::
          #{major := non_neg_integer(),
            minor := non_neg_integer(),
            patch := non_neg_integer(),
            pre := [pre_identifier()],
            build := binary() | undefined}.

Functions

compare(Version1, Version2)

-spec compare(version() | binary() | string(), version() | binary() | string()) -> gt | eq | lt.

Compares two versions, returning gt, eq or lt.

Pre-releases are lower than their release; numeric pre-release identifiers are compared numerically and lower than alphanumeric ones. Build segments are ignored. Raises {invalid_version, V} if a version string cannot be parsed.

   gt = version:compare("2.0.1-alpha1", "2.0.0"),
   gt = version:compare("1.0.0-10", "1.0.0-2"),
   eq = version:compare("2.0.1+build0", "2.0.1").

compile_requirement(Requirement)

-spec compile_requirement(requirement()) -> requirement().

Compiles a requirement for faster matching. Requirements are already stored in their matching form, so this returns it unchanged; it exists for API compatibility.

match(Version, Requirement)

-spec match(version() | binary() | string(), requirement() | binary() | string()) -> boolean().

Checks if the version matches the requirement, allowing pre-releases.

match(Version, Requirement, Opts)

-spec match(version() | binary() | string(),
            requirement() | binary() | string(),
            [match_option()] | #{allow_pre => boolean()}) ->
               boolean().

Checks if the version matches the requirement.

With {allow_pre, false}, pre-release versions do not match the ~>, > and >= operators unless the requirement's version is itself a pre-release. Raises {invalid_version, V} or {invalid_requirement, R} if a string cannot be parsed.

   true = version:match("2.1.6-dev", "~> 2.1.2"),
   false = version:match("2.1.6-dev", "~> 2.1.2", [{allow_pre, false}]).

parse(String)

-spec parse(binary() | string()) -> {ok, version()} | error.

Parses a version string.

   {ok, #{major := 2, minor := 0, patch := 1, pre := [<<"alpha1">>]}} = version:parse("2.0.1-alpha1"),
   error = version:parse("2.0-alpha1").

parse_requirement(String)

-spec parse_requirement(binary() | string()) -> {ok, requirement()} | error.

Parses a requirement string.

   {ok, Req} = version:parse_requirement(">= 1.0.0 and < 2.0.0"),
   error = version:parse_requirement("== == 2.0.1").

to_string(Version)

-spec to_string(version() | requirement()) -> binary().

Converts a version to a string.

   <<"1.14.0-rc.0+build0">> = version:to_string(Version).