Skip to content

Instantly share code, notes, and snippets.

@cmullaparthi
Created October 10, 2016 15:17
Show Gist options
  • Save cmullaparthi/496972b2e04e83dee99aa70f4ee797d6 to your computer and use it in GitHub Desktop.
Save cmullaparthi/496972b2e04e83dee99aa70f4ee797d6 to your computer and use it in GitHub Desktop.
-module(debug_relx).
-export([go/0, go/1]).
go() ->
go(".").
go(Dir) ->
Fun = fun collect_app_file/2,
{_, Files} = filelib:fold_files(Dir, ".*.app.src", true, Fun, {[], []}),
Graph = digraph:new(),
_ = [process_app_file(X, Graph) || X <- Files],
order_from_graph(Graph).
collect_app_file(Filename, {File_acc, Full_acc} = Acc) ->
Basename = filename:basename(Filename),
case lists:member(Basename, File_acc) of
true ->
Acc;
false ->
{[Basename | File_acc], [Filename | Full_acc]}
end.
process_app_file(Filename, Graph) ->
io:format("Processing file: ~s...~n", [Filename]),
{ok, [{application, App, App_TVL}]} = file:consult(Filename),
Apps_depend = proplists:get_value(applications, App_TVL),
lists:foreach(fun(X_app) ->
V1 = digraph:add_vertex(Graph, App),
V2 = digraph:add_vertex(Graph, X_app),
digraph:add_edge(Graph, V1, V2)
end, Apps_depend).
order_from_graph(G) ->
order_from_graph(digraph:vertices(G), G, []).
order_from_graph([], _G, Acc) ->
digraph:delete(_G),
[kernel, stdlib, sasl] ++ (Acc -- [kernel, stdlib, sasl]);
order_from_graph(Vertices, G, Acc) ->
Leaves = lists:filter(fun(X) ->
case digraph:out_neighbours(G, X) of
[] ->
digraph:del_vertex(G, X),
true;
_ ->
false
end
end, Vertices),
case Leaves of
[] ->
io:format("Possibly cyclic graph. Cannot order.~n"
"Vertices : ~p~n"
"Leaves : []~n"
"Acc : ~p~n", [Vertices, Acc]),
G;
_ ->
order_from_graph(digraph:vertices(G), G, Acc ++ Leaves)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment