mad_utils.erl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. -module(mad_utils).
  2. -copyright('Sina Samavati').
  3. -compile(export_all).
  4. %% internal
  5. name_and_repo({Name, _, Repo}) when is_list(Name) -> {Name, Repo};
  6. name_and_repo({Name, _, Repo, _}) when is_list(Name) -> {Name, Repo};
  7. name_and_repo({Name, _, Repo}) -> {atom_to_list(Name), Repo};
  8. name_and_repo({Name, _, Repo, _}) -> {atom_to_list(Name), Repo};
  9. name_and_repo({Name, Version}) when is_list(Name) -> {Name, Version};
  10. name_and_repo({Name, Version}) -> {atom_to_list(Name), Version};
  11. name_and_repo(Name) -> {Name,Name}.
  12. cwd() -> {ok, Cwd} = file:get_cwd(), Cwd.
  13. home() -> {ok, [[H|_]]} = init:get_argument(home), H.
  14. consult(File) ->
  15. AbsFile = filename:absname(File),
  16. case file:consult(AbsFile) of
  17. {ok, V} ->
  18. V;
  19. _ ->
  20. []
  21. end.
  22. src(Dir) -> filename:join(Dir, "src").
  23. include(Dir) -> filename:join(Dir, "include").
  24. ebin(Dir) -> filename:join(Dir, "ebin").
  25. priv(Dir) -> filename:join(Dir, "priv").
  26. deps(File) -> get_value(deps, consult(File), []).
  27. get_value(Key, Opts, undefined) -> get_value(Key, Opts, []);
  28. get_value(Key, Opts, Default) ->
  29. case lists:keyfind(Key, 1, Opts) of
  30. {Key, Value} ->
  31. Value;
  32. _ -> Default end.
  33. script(ConfigFile, Conf, _Name) ->
  34. File = ConfigFile ++ ".script",
  35. case file:script(File, [{'CONFIG', Conf}, {'SCRIPT', File}]) of
  36. {ok, {error,_}} -> Conf;
  37. {ok, Out} -> Out;
  38. {error, _} -> Conf
  39. end.
  40. sub_dirs(Cwd, ConfigFile, Conf) ->
  41. sub_dirs(Cwd, ConfigFile, get_value(sub_dirs, Conf, []), []).
  42. sub_dirs(_, _, [], Acc) -> Acc;
  43. sub_dirs(Cwd, ConfigFile, [Dir|T], Acc) ->
  44. SubDir = filename:join(Cwd, Dir),
  45. ConfigFile1 = filename:join(SubDir, ConfigFile),
  46. Conf = consult(ConfigFile1),
  47. Conf1 = mad_script:script(ConfigFile1, Conf, Dir),
  48. Acc1 = sub_dirs(SubDir, ConfigFile, get_value(sub_dirs, Conf1, []),
  49. Acc ++ [SubDir]),
  50. sub_dirs(Cwd, ConfigFile, T, Acc1).
  51. lib_dirs(Cwd, Conf) -> lib_dirs(Cwd, get_value(lib_dirs, Conf, []), []).
  52. raw_deps(Deps) -> raw_deps(Deps,[]).
  53. raw_deps([],Res) -> Res;
  54. raw_deps([D|Deps],Res) when is_tuple(D) -> raw_deps([element(1,D)|Deps],Res);
  55. raw_deps([D|Deps],Res) -> raw_deps(Deps,[D|Res]).
  56. lib_dirs(_, [], Acc) -> Acc;
  57. lib_dirs(Cwd, [H|T], Acc) when is_tuple(H) -> lib_dirs(Cwd, [element(1,H)|T], Acc);
  58. lib_dirs(Cwd, [H|T], Acc) ->
  59. Dirs = filelib:wildcard(filename:join([Cwd, H, "*", "include"])),
  60. lib_dirs(Cwd, T, Acc ++ Dirs).
  61. last_modified(File) ->
  62. case filelib:last_modified(File) of
  63. 0 -> 0;
  64. Else -> calendar:datetime_to_gregorian_seconds(Else) end.
  65. to_atom(X) when is_atom(X) -> X;
  66. to_atom(X) when is_list(X) -> list_to_atom(X);
  67. to_atom(X) when is_binary(X) -> to_atom(binary_to_list(X));
  68. to_atom(X) -> X.
  69. verbose(Config,Message) ->
  70. case mad_utils:get_value(verbose, Config, 0) of
  71. 0 -> skip;
  72. _ -> mad:info("~s",[binary_to_list(Message)]) end.
  73. compile(_,_,_,_,_) -> false.
  74. configs() ->
  75. Cwd = try fs:path() catch _:_ -> cwd() end,
  76. ConfigFile = "rebar.config",
  77. ConfigFileAbs = filename:join(Cwd, ConfigFile),
  78. Conf = mad_utils:consult(ConfigFileAbs),
  79. Conf1 = mad_script:script(ConfigFileAbs, Conf, ""),
  80. {Cwd,ConfigFile,Conf1}.