add specs and exported types where needed
This patch exports types that are needed and adds specs where they did not previously exist. It also cleans up and expands the specs where they needed to be cleaned up.
This commit is contained in:
parent
2508e1ed82
commit
07a54bb518
3 changed files with 63 additions and 20 deletions
|
@ -22,7 +22,10 @@
|
|||
consult/1
|
||||
]).
|
||||
|
||||
-export_type([path/0]).
|
||||
-export_type([
|
||||
path/0,
|
||||
option/0
|
||||
]).
|
||||
|
||||
-include_lib("kernel/include/file.hrl").
|
||||
|
||||
|
@ -41,12 +44,13 @@
|
|||
%% Types
|
||||
%%============================================================================
|
||||
-type path() :: string().
|
||||
-type option() :: [atom()].
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
%% @doc copy an entire directory to another location.
|
||||
-spec copy(path(), path(), Options::list()) -> ok.
|
||||
-spec copy(path(), path(), Options::[option()]) -> ok.
|
||||
copy(From, To, []) ->
|
||||
copy(From, To);
|
||||
copy(From, To, [recursive] = Options) ->
|
||||
|
@ -77,7 +81,7 @@ md5sum(Value) ->
|
|||
%% <pre>
|
||||
%% Example: remove("./tmp_dir", [recursive]).
|
||||
%% </pre>
|
||||
-spec remove(path(), Options::list()) -> ok | {error, Reason::term()}.
|
||||
-spec remove(path(), Options::[option()]) -> ok | {error, Reason::term()}.
|
||||
remove(Path, Options) ->
|
||||
try
|
||||
ok = ec_file_remove(Path, Options)
|
||||
|
@ -132,7 +136,7 @@ mkdir_path(Path) ->
|
|||
|
||||
%% @doc consult an erlang term file from the file system.
|
||||
%% Provide user readible exeption on failure.
|
||||
-spec consult(FilePath::string()) -> term().
|
||||
-spec consult(FilePath::path()) -> term().
|
||||
consult(FilePath) ->
|
||||
case file:consult(FilePath) of
|
||||
{ok, [Term]} ->
|
||||
|
@ -181,7 +185,7 @@ write_term(FileName, Term) ->
|
|||
|
||||
%% @doc Finds files and directories that match the regexp supplied in
|
||||
%% the TargetPattern regexp.
|
||||
-spec find(FromDir::path(), TargetPattern::string()) -> list().
|
||||
-spec find(FromDir::path(), TargetPattern::string()) -> [path()].
|
||||
find([], _) ->
|
||||
[];
|
||||
find(FromDir, TargetPattern) ->
|
||||
|
@ -202,6 +206,7 @@ find(FromDir, TargetPattern) ->
|
|||
%%%===================================================================
|
||||
%%% Internal Functions
|
||||
%%%===================================================================
|
||||
-spec find_in_subdirs(path(), string()) -> [path()].
|
||||
find_in_subdirs(FromDir, TargetPattern) ->
|
||||
lists:foldl(fun(CheckFromDir, Acc)
|
||||
when CheckFromDir == FromDir ->
|
||||
|
@ -215,6 +220,7 @@ find_in_subdirs(FromDir, TargetPattern) ->
|
|||
[],
|
||||
filelib:wildcard(filename:join(FromDir, "*"))).
|
||||
|
||||
-spec ec_file_remove(path(), [{atom(), any()}]) -> ok.
|
||||
ec_file_remove(Path, Options) ->
|
||||
case lists:member(recursive, Options) of
|
||||
false -> file:delete(Path);
|
||||
|
@ -233,6 +239,7 @@ remove_recursive(Path, Options) ->
|
|||
ok = file:del_dir(Path)
|
||||
end.
|
||||
|
||||
-spec tmp() -> path().
|
||||
tmp() ->
|
||||
case erlang:system_info(system_architecture) of
|
||||
"win32" ->
|
||||
|
@ -243,6 +250,7 @@ tmp() ->
|
|||
end.
|
||||
|
||||
%% Copy the subfiles of the From directory to the to directory.
|
||||
-spec copy_subfiles(path(), path(), [option()]) -> ok.
|
||||
copy_subfiles(From, To, Options) ->
|
||||
Fun =
|
||||
fun(ChildFrom) ->
|
||||
|
@ -251,11 +259,13 @@ copy_subfiles(From, To, Options) ->
|
|||
end,
|
||||
lists:foreach(Fun, filelib:wildcard(filename:join(From, "*"))).
|
||||
|
||||
-spec ec_file_copy(path(), path()) -> ok.
|
||||
ec_file_copy(From, To) ->
|
||||
{ok, _} = file:copy(From, To),
|
||||
{ok, FileInfo} = file:read_file_info(From),
|
||||
ok = file:write_file_info(To, FileInfo).
|
||||
|
||||
-spec make_dir_if_dir(path()) -> ok.
|
||||
make_dir_if_dir(File) ->
|
||||
case filelib:is_dir(File) of
|
||||
true -> ok;
|
||||
|
@ -263,6 +273,7 @@ make_dir_if_dir(File) ->
|
|||
end.
|
||||
|
||||
%% @doc convert a list of integers into hex.
|
||||
-spec hex(string() | non_neg_integer()) -> string().
|
||||
hex(L) when is_list (L) ->
|
||||
lists:flatten([hex(I) || I <- L]);
|
||||
hex(I) when I > 16#f ->
|
||||
|
|
|
@ -11,6 +11,20 @@
|
|||
compare/2
|
||||
]).
|
||||
|
||||
-export_type([
|
||||
semvar/0
|
||||
]).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Public Types
|
||||
%%%===================================================================
|
||||
|
||||
-type semvar() :: string().
|
||||
-type parsed_semvar() :: {MajorVsn::string(),
|
||||
MinorVsn::string(),
|
||||
PatchVsn::string(),
|
||||
PathString::string()}.
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
|
@ -26,15 +40,20 @@ compare(VsnA, VsnB) ->
|
|||
%%%===================================================================
|
||||
%%% Internal Functions
|
||||
%%%===================================================================
|
||||
-spec tokens(semvar()) -> parsed_semvar().
|
||||
tokens(Vsn) ->
|
||||
[MajorVsn, MinorVsn, RawPatch] = string:tokens(Vsn, "."),
|
||||
{PatchVsn, PatchString} = split_patch(RawPatch),
|
||||
{MajorVsn, MinorVsn, PatchVsn, PatchString}.
|
||||
|
||||
-spec split_patch(string()) ->
|
||||
{PatchVsn::string(), PatchStr::string()}.
|
||||
split_patch(RawPatch) ->
|
||||
{PatchVsn, PatchStr} = split_patch(RawPatch, {"", ""}),
|
||||
{lists:reverse(PatchVsn), PatchStr}.
|
||||
|
||||
-spec split_patch(string(), {AccPatchVsn::string(), AccPatchStr::string()}) ->
|
||||
{PatchVsn::string(), PatchStr::string()}.
|
||||
split_patch([], Acc) ->
|
||||
Acc;
|
||||
split_patch([Dig|T], {PatchVsn, PatchStr}) when Dig >= $0 andalso Dig =< $9 ->
|
||||
|
@ -42,10 +61,12 @@ split_patch([Dig|T], {PatchVsn, PatchStr}) when Dig >= $0 andalso Dig =< $9 ->
|
|||
split_patch(PatchStr, {PatchVsn, ""}) ->
|
||||
{PatchVsn, PatchStr}.
|
||||
|
||||
-spec compare_toks(parsed_semvar(), parsed_semvar()) -> boolean().
|
||||
compare_toks({MajA, MinA, PVA, PSA}, {MajB, MinB, PVB, PSB}) ->
|
||||
compare_toks2({to_int(MajA), to_int(MinA), to_int(PVA), PSA},
|
||||
{to_int(MajB), to_int(MinB), to_int(PVB), PSB}).
|
||||
|
||||
-spec compare_toks2(parsed_semvar(), parsed_semvar()) -> boolean().
|
||||
compare_toks2({MajA, _MinA, _PVA, _PSA}, {MajB, _MinB, _PVB, _PSB}) when MajA > MajB ->
|
||||
true;
|
||||
compare_toks2({_Maj, MinA, _PVA, _PSA}, {_Maj, MinB, _PVB, _PSB}) when MinA > MinB ->
|
||||
|
@ -61,6 +82,7 @@ compare_toks2({_Maj, _Min, _PV, PSA}, {_Maj, _Min, _PV, PSB}) when PSA > PSB ->
|
|||
compare_toks2(_ToksA, _ToksB) ->
|
||||
false.
|
||||
|
||||
-spec to_int(string()) -> integer().
|
||||
to_int(String) ->
|
||||
case catch list_to_integer(String) of
|
||||
Integer when is_integer(Integer) ->
|
||||
|
|
|
@ -34,6 +34,7 @@ compare_versions(VsnA, VsnB) ->
|
|||
%%% Internal Functions
|
||||
%%%===================================================================
|
||||
|
||||
-spec compare(string(), string()) -> boolean().
|
||||
compare([Str|TA], [Str|TB]) ->
|
||||
compare(TA, TB);
|
||||
compare([StrA|TA], [StrB|TB]) ->
|
||||
|
@ -50,6 +51,7 @@ compare([_,_|_], []) ->
|
|||
compare([], []) ->
|
||||
false.
|
||||
|
||||
-spec compare_against_nothing(string()) -> boolean().
|
||||
compare_against_nothing(Str) ->
|
||||
case split_numeric_alpha(Str) of
|
||||
{_StrDig, ""} -> true;
|
||||
|
@ -57,6 +59,9 @@ compare_against_nothing(Str) ->
|
|||
{_StrDig, _StrAlpha} -> true
|
||||
end.
|
||||
|
||||
-spec fine_compare({string(), string()}, string(),
|
||||
{string(), string()}, string()) ->
|
||||
boolean().
|
||||
fine_compare({_StrDigA, StrA}, TA, {_StrDigB, _StrB}, _TB) when StrA /= "", TA /= [] ->
|
||||
throw(invalid_version_string);
|
||||
fine_compare({_StrDigA, _StrA}, _TA, {_StrDigB, StrB}, TB) when StrB /= "", TB /= [] ->
|
||||
|
@ -76,10 +81,15 @@ fine_compare({StrDigA, _StrA}, _TA, {StrDigB, _StrB}, _TB) ->
|
|||
|
||||
%% In the case of a version sub part with a numeric then an alpha,
|
||||
%% split out the numeric and alpha "24alpha" becomes {"24", "alpha"}
|
||||
-spec split_numeric_alpha(string()) ->
|
||||
{PatchVsn::string(), PatchStr::string()}.
|
||||
split_numeric_alpha(RawVsn) ->
|
||||
{Num, Str} = split_numeric_alpha(RawVsn, {"", ""}),
|
||||
{lists:reverse(Num), Str}.
|
||||
|
||||
-spec split_numeric_alpha(string(), {PatchVsnAcc::string(),
|
||||
PatchStrAcc::string()}) ->
|
||||
{PatchVsn::string(), PatchStr::string()}.
|
||||
split_numeric_alpha([], Acc) ->
|
||||
Acc;
|
||||
split_numeric_alpha([Dig|T], {PatchVsn, PatchStr})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue