Version

Functions for parsing and matching versions against requirements.

A version is a string in a specific format or a Version.Schema generated after parsing via Version.parse/1.

Version parsing and requirements follow SemVer 2.0 schema.

Versions

In a nutshell, a version is given by three numbers:

MAJOR.MINOR.PATCH

Pre-releases are supported by appending -[0-9A-Za-z-\.]:

"1.0.0-alpha.3"

Build information can be added by appending +[0-9A-Za-z-\.]:

"1.0.0-alpha.3+20130417140000"

Requirements

Requirements allow you to specify which versions of a given dependency you are willing to work against. It supports common operators like >=, <=, >, == and friends that work as one would expect:

# Only version 2.0.0
"== 2.0.0"

# Anything later than 2.0.0
"> 2.0.0"

Requirements also support and and or for complex conditions:

# 2.0.0 and later until 2.1.0
">= 2.0.0 and < 2.1.0"

Since the example above is such a common requirement, it can be expressed as:

"~> 2.0.0"
Source

Summary

compare(vsn1, vsn2)

Compares two versions. Returns :gt if first version is greater than the second and :lt for vice versa. If the two versions are equal :eq is returned

match?(vsn, req)

Check if the given version matches the specification

parse(string)

Parse a version string into a Version.Schema

parse_requirement(string)

Parse a version requirement string into a Version.Schema

Types

matchable :: {major :: String.t | non_neg_integer, minor :: non_neg_integer | nil, patch :: non_neg_integer | nil, pre :: [String.t]}

Functions

compare(vsn1, vsn2)

Specs:

Compares two versions. Returns :gt if first version is greater than the second and :lt for vice versa. If the two versions are equal :eq is returned

Raises a Version.InvalidVersion exception if version is not parseable. If given an already parsed version this function won't raise.

Examples

iex> Version.compare("2.0.1-alpha1", "2.0.0")
:gt

iex> Version.compare("2.0.1+build0", "2.0.1")
:eq

iex> Version.compare("invalid", "2.0.1")
** (Version.InvalidVersion) invalid
Source
match?(vsn, req)

Specs:

Check if the given version matches the specification.

Returns true if version satisfies requirement, false otherwise. Raises a Version.InvalidRequirement exception if requirement is not parseable, or Version.InvalidVersion if version is not parseable. If given an already parsed version and requirement this function won't raise.

Examples

iex> Version.match?("2.0.0", ">1.0.0")
true

iex> Version.match?("2.0.0", "==1.0.0")
false

iex> Version.match?("foo", "==1.0.0")
** (Version.InvalidVersion) foo

iex> Version.match?("2.0.0", "== ==1.0.0")
** (Version.InvalidRequirement) == ==1.0.0
Source
parse(string)

Specs:

Parse a version string into a Version.Schema.

Examples

iex> Version.parse("2.0.1-alpha1") |> elem(1)
#Version.Schema<2.0.1-alpha1>

iex> Version.parse("2.0-alpha1")
:error
Source
parse_requirement(string)

Specs:

Parse a version requirement string into a Version.Schema.

Examples

iex> Version.parse_requirement("== 2.0.1") |> elem(1)
#Version.Requirement<== 2.0.1>

iex> Version.parse_requirement("== == 2.0.1")
:error
Source