mad_utils.erl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -module(mad_utils).
  2. -copyright('Sina Samavati').
  3. -compile(export_all).
  4. -type directory() :: string().
  5. -spec cwd() -> directory().
  6. cwd() -> {ok, Cwd} = file:get_cwd(), Cwd.
  7. exec(Cmd, Opts) -> os:cmd([Cmd," ",string:join(Opts," ")]).
  8. -spec home() -> directory().
  9. home() -> {ok, [[H|_]]} = init:get_argument(home), H.
  10. -spec consult(file:name_all()) -> [term()].
  11. consult(File) ->
  12. AbsFile = filename:absname(File),
  13. case file:consult(AbsFile) of
  14. {ok, V} ->
  15. V;
  16. _ ->
  17. []
  18. end.
  19. -spec src(directory()) -> directory().
  20. src(Dir) -> filename:join(Dir, "src").
  21. -spec include(directory()) -> directory().
  22. include(Dir) -> filename:join(Dir, "include").
  23. -spec ebin(directory()) -> directory().
  24. ebin(Dir) -> filename:join(Dir, "ebin").
  25. -spec deps(file:name_all()) -> [term()].
  26. deps(File) -> get_value(deps, consult(File), []).
  27. -spec get_value(term(), [{term(), term()}], Default) -> term() | Default.
  28. get_value(Key, Opts, Default) ->
  29. case lists:keyfind(Key, 1, Opts) of
  30. {Key, Value} ->
  31. Value;
  32. _ -> Default end.
  33. -spec script(file:name(), [term()]) -> [term()].
  34. script(ConfigFile, Conf) ->
  35. File = ConfigFile ++ ".script",
  36. Filename = filename:basename(File),
  37. case file:script(File, [{'CONFIG', Conf}, {'SCRIPT', Filename}]) of
  38. {ok, {error,_}} -> Conf;
  39. {ok, Out} -> Out;
  40. {error, _} -> Conf
  41. end.
  42. -spec sub_dirs(directory(), file:filename(), [term()]) -> [directory()].
  43. sub_dirs(Cwd, ConfigFile, Conf) ->
  44. sub_dirs(Cwd, ConfigFile, get_value(sub_dirs, Conf, []), []).
  45. -spec sub_dirs(directory(), file:filename(), [term()], [term()]) -> [directory()].
  46. sub_dirs(_, _, [], Acc) -> Acc;
  47. sub_dirs(Cwd, ConfigFile, [Dir|T], Acc) ->
  48. SubDir = filename:join(Cwd, Dir),
  49. ConfigFile1 = filename:join(SubDir, ConfigFile),
  50. Conf = consult(ConfigFile1),
  51. Conf1 = script(ConfigFile1, Conf),
  52. Acc1 = sub_dirs(SubDir, ConfigFile, get_value(sub_dirs, Conf1, []),
  53. Acc ++ [SubDir]),
  54. sub_dirs(Cwd, ConfigFile, T, Acc1).
  55. -spec lib_dirs(directory(), [term()]) -> [directory()].
  56. lib_dirs(Cwd, Conf) -> lib_dirs(Cwd, get_value(lib_dirs, Conf, []), []).
  57. -spec lib_dirs(directory(), [term()], [term()]) -> [directory()].
  58. lib_dirs(_, [], Acc) -> Acc;
  59. lib_dirs(Cwd, [H|T], Acc) ->
  60. Dirs = filelib:wildcard(filename:join([Cwd, H, "*", "ebin"])),
  61. lib_dirs(Cwd, T, Acc ++ Dirs).
  62. -spec last_modified(file:name_all()) -> Seconds :: non_neg_integer().
  63. last_modified(File) ->
  64. case filelib:last_modified(File) of
  65. 0 -> 0;
  66. Else -> calendar:datetime_to_gregorian_seconds(Else) end.
  67. to_atom(X) when is_atom(X) -> X;
  68. to_atom(X) when is_list(X) -> list_to_atom(X);
  69. to_atom(X) when is_binary(X) -> to_atom(binary_to_list(X));
  70. to_atom(X) -> X.