provide the ability to format a version into a string as well as parse a version

Signed-off-by: Jordan Wilberding <diginux@gmail.com>
This commit is contained in:
Eric Merritt 2012-09-18 15:34:57 -07:00 committed by Jordan Wilberding
parent 0c34549901
commit e9161d8688
2 changed files with 52 additions and 4 deletions

View file

@ -2,7 +2,7 @@
%% These are all only compile time dependencies
{deps, [{neotoma, "",
{git, "https://github.com/seancribbs/neotoma.git", {tag, "1.5"}}},
{git, "https://github.com/seancribbs/neotoma.git", {tag, "1.5.1"}}},
{proper, "", {git, "https://github.com/manopapad/proper.git", {branch, master}}}]}.
{erl_first_files, ["ec_dictionary"]}.

View file

@ -8,6 +8,7 @@
-module(ec_semver).
-export([parse/1,
format/1,
eql/2,
gt/2,
gte/2,
@ -32,10 +33,11 @@
| {non_neg_integer(), non_neg_integer()}
| {non_neg_integer(), non_neg_integer(), non_neg_integer()}.
-type alpha_part() :: integer() | binary().
-type alpha_part() :: integer() | binary() | string().
-type alpha_info() :: {PreRelease::[alpha_part()],
BuildVersion::[alpha_part()]}.
-type semver() :: {major_minor_patch(), {PreReleaseVersion::[alpha_part()],
BuildVersion::[alpha_part()]}}.
-type semver() :: {major_minor_patch(), alpha_info()}.
-type version_string() :: string() | binary().
@ -54,6 +56,24 @@ parse(Version) when erlang:is_binary(Version) ->
parse(Version) ->
Version.
-spec format(semver()) -> iolist().
format({Maj, {AlphaPart, BuildPart}})
when erlang:is_integer(Maj) ->
[erlang:integer_to_list(Maj),
format_vsn_rest(<<"-">>, AlphaPart),
format_vsn_rest(<<"+">>, BuildPart)];
format({{Maj, Min}, {AlphaPart, BuildPart}}) ->
[erlang:integer_to_list(Maj), ".",
erlang:integer_to_list(Min),
format_vsn_rest(<<"-">>, AlphaPart),
format_vsn_rest(<<"+">>, BuildPart)];
format({{Maj, Min, Patch}, {AlphaPart, BuildPart}}) ->
[erlang:integer_to_list(Maj), ".",
erlang:integer_to_list(Min), ".",
erlang:integer_to_list(Patch),
format_vsn_rest(<<"-">>, AlphaPart),
format_vsn_rest(<<"+">>, BuildPart)].
%% @doc test for quality between semver versions
-spec eql(any_version(), any_version()) -> boolean().
eql(VsnA, VsnB) ->
@ -189,6 +209,19 @@ format_alpha_part([<<".">>, AlphaPart]) ->
%%%===================================================================
%%% Internal Functions
%%%===================================================================
-spec to_list(integer() | binary() | string()) -> string() | binary().
to_list(Detail) when erlang:is_integer(Detail) ->
erlang:integer_to_list(Detail);
to_list(Detail) when erlang:is_list(Detail); erlang:is_binary(Detail) ->
Detail.
-spec format_vsn_rest(binary() | string(), [integer() | binary()]) -> iolist().
format_vsn_rest(_TypeMark, []) ->
[];
format_vsn_rest(TypeMark, [Head | Rest]) ->
[TypeMark, Head |
[[".", to_list(Detail)] || Detail <- Rest]].
%% @doc normalize the semver so they can be compared
-spec normalize(semver()) -> semver().
normalize({Vsn, Rest})
@ -523,4 +556,19 @@ pes_test() ->
?assertMatch(true, not pes("2.7", "2.6.5")),
?assertMatch(true, not pes("2.5", "2.6.5")).
version_format_test() ->
?assertEqual(["1", [], []], format({1, {[],[]}})),
?assertEqual(["1", ".", "2", ".", "34", [], []], format({{1,2,34},{[],[]}})),
?assertEqual(<<"1">>, erlang:iolist_to_binary(format({1, {[],[]}}))),
?assertEqual(<<"1.2">>, erlang:iolist_to_binary(format({{1,2}, {[],[]}}))),
?assertEqual(<<"1.2.2">>, erlang:iolist_to_binary(format({{1,2,2}, {[],[]}}))),
?assertEqual(<<"1.99.2">>, erlang:iolist_to_binary(format({{1,99,2}, {[],[]}}))),
?assertEqual(<<"1.99.2-alpha">>, erlang:iolist_to_binary(format({{1,99,2}, {[<<"alpha">>],[]}}))),
?assertEqual(<<"1.99.2-alpha.1">>, erlang:iolist_to_binary(format({{1,99,2}, {[<<"alpha">>,1], []}}))),
?assertEqual(<<"1.99.2+build.1.a36">>,
erlang:iolist_to_binary(format({{1,99,2}, {[], [<<"build">>, 1, <<"a36">>]}}))),
?assertEqual(<<"1.99.2-alpha.1+build.1.a36">>,
erlang:iolist_to_binary(format({{1,99,2}, {[<<"alpha">>, 1], [<<"build">>, 1, <<"a36">>]}}))),
?assertEqual(<<"1">>, erlang:iolist_to_binary(format({1, {[],[]}}))).
-endif.