mad_plan.erl 1.4 KB

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