diff options
Diffstat (limited to 'src/rebar_digraph.erl')
-rw-r--r-- | src/rebar_digraph.erl | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/rebar_digraph.erl b/src/rebar_digraph.erl index bbcb736..0ca5984 100644 --- a/src/rebar_digraph.erl +++ b/src/rebar_digraph.erl @@ -1,12 +1,45 @@ -module(rebar_digraph). --export([restore_graph/1 +-export([sort_apps/1 + ,restore_graph/1 ,solve/2 ,subgraph/2 ,format_error/1]). -include("rebar.hrl"). +sort_apps(Apps) -> + Graph = digraph:new(), + lists:foreach(fun(App) -> + Name = rebar_app_info:name(App), + Deps = rebar_app_info:deps(App), + add(Graph, {Name, Deps}) + end, Apps), + case digraph_utils:topsort(Graph) of + false -> + {error, no_sort}; + V -> + {ok, names_to_apps(lists:reverse(V), Apps)} + end. + +add(Graph, {PkgName, Deps}) -> + case digraph:vertex(Graph, PkgName) of + false -> + V = digraph:add_vertex(Graph, PkgName); + {V, []} -> + V + end, + + lists:foreach(fun(DepName) -> + V3 = case digraph:vertex(Graph, DepName) of + false -> + digraph:add_vertex(Graph, DepName); + {V2, []} -> + V2 + end, + digraph:add_edge(Graph, V, V3) + end, Deps). + restore_graph({Vs, Es}) -> Graph = digraph:new(), lists:foreach(fun({V, LastUpdated}) -> @@ -45,3 +78,17 @@ subgraph(Graph, Vertices) -> format_error(no_solution) -> io_lib:format("No solution for packages found.", []). + +%%==================================================================== +%% Internal Functions +%%==================================================================== + +-spec names_to_apps([atom()], [rebar_app_info:t()]) -> [rebar_app_info:t()]. +names_to_apps(Names, Apps) -> + [element(2, App) || App <- [find_app_by_name(Name, Apps) || Name <- Names], App =/= error]. + +-spec find_app_by_name(atom(), [rebar_app_info:t()]) -> {ok, rebar_app_info:t()} | error. +find_app_by_name(Name, Apps) -> + ec_lists:find(fun(App) -> + ec_cnv:to_atom(rebar_app_info:name(App)) =:= ec_cnv:to_atom(Name) + end, Apps). |