Current section

Files

Jump to
rebar3_hex src rebar3_hex_utils.erl
Raw

src/rebar3_hex_utils.erl

-module(rebar3_hex_utils).
-export([hex_home/0
,binarify/1
,expand_paths/2]).
-define(DEFAULT_HEX_DIR, ".hex").
hex_home() ->
{ok, [[Home]]} = init:get_argument(home),
filename:join(Home, ?DEFAULT_HEX_DIR).
binarify(Term) when is_boolean(Term) ->
Term;
binarify(Term) when is_atom(Term) ->
atom_to_binary(Term, utf8);
binarify([]) ->
[];
binarify(Term) when is_list(Term) ->
case io_lib:printable_list(Term) of
true ->
list_to_binary(Term);
false ->
[binarify(X) || X <- Term]
end;
binarify({Key, Value}) ->
{binarify(Key), binarify(Value)};
binarify(Term) ->
Term.
expand_paths(Paths, Dir) ->
AbsDir = filename:absname(Dir),
Files = lists:flatmap(fun dir_files1/1, [filename:join(Dir, P) || P <- Paths]),
[F1 -- (AbsDir++"/") || F1 <- filter_regular(Files)].
dir_files1(Dir) ->
lists:flatmap(fun(Y) -> dir_files(Y) end, filelib:wildcard(Dir)).
filter_regular(Files) ->
lists:filter(fun filelib:is_regular/1, [filename:absname(F) || F <- Files]).
dir_files(Path) ->
case filelib:is_dir(Path) of
true ->
filelib:wildcard(filename:join(Path, "**"));
false ->
[Path]
end.