mad_utils.erl 3.4 KB

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