mad_plan.erl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. -module(mad_plan).
  2. -author('Maxim Sokhatsky').
  3. -compile(export_all).
  4. % dependency graph solver
  5. sort(Pairs) -> iterate(Pairs, [], lhs(Pairs) ++ rhs(Pairs)).
  6. lhs(L) -> [X || {X, _} <- L].
  7. rhs(L) -> [Y || {_, Y} <- L].
  8. rm_pairs(L1, L2) -> [All || All={X, _Y} <- L2, not lists:member(X, L1)].
  9. subtract(L1, L2) -> [X || X <- L1, not lists:member(X, L2)].
  10. iterate([], L, All) -> {ok,rm_dups(L ++ subtract(All, L))};
  11. iterate(P, L, All) -> case subtract(lhs(P), rhs(P)) of [] -> P; Lhs -> iterate(rm_pairs(Lhs, P), L ++ Lhs, All) end.
  12. rm_dups([]) -> [];
  13. rm_dups([H|T]) -> case lists:member(H, T) of true -> rm_dups(T); false -> [H|rm_dups(T)] end.
  14. orderapps() ->
  15. Pairs = lists:flatten([ case
  16. file:consult(F) of
  17. {ok,[{application,Name,Opt}]} ->
  18. Apps = proplists:get_value(applications,Opt,[]),
  19. [ case lists:member(A,mad_repl:system()) of
  20. false -> {A,Name};
  21. true -> [{A,Name}]++ system_deps(A) end || A <- Apps ];
  22. {error,_} ->
  23. io:format("AppName: ~p~n",[F]), skip
  24. end || F <- mad_repl:wildcards(["{apps,deps}/*/ebin/*.app","ebin/*.app"]), not filelib:is_dir(F) ]),
  25. case sort(lists:flatten(Pairs)) of
  26. {ok,Sorted} -> {ok,Sorted};
  27. Return -> {error,{cycling_apps,Return}} end.
  28. system_deps(A) ->
  29. case file:consult(code:where_is_file(lists:concat([A,".app"]))) of
  30. {ok,[{application,Name,Opt}]} -> [ {_A,Name} || _A <- proplists:get_value(applications,Opt,[]) ];
  31. {error,_} -> [] end.
  32. main(_) ->
  33. case orderapps() of
  34. {ok,Ordered} -> io:format("Ordered: ~p~n\r",[Ordered]),
  35. file:write_file(".applist",io_lib:format("~w",[Ordered])), false;
  36. {error,Reason} -> io:format("Ordering Error: ~p~n\r",[Reason]), true end.