mad_utils.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. -module(mad_utils).
  2. -copyright('Sina Samavati').
  3. -compile(export_all).
  4. atomize("static") -> static;
  5. atomize("deploy") -> deploy;
  6. atomize("app"++_) -> app;
  7. atomize("dep") -> deps;
  8. atomize("deps") -> deps;
  9. atomize("cle"++_) -> clean;
  10. atomize("com"++_) -> compile;
  11. atomize("up") -> up;
  12. atomize("rel"++_) -> release;
  13. atomize("bun"++_) -> release;
  14. atomize("sta"++_) -> start;
  15. atomize("sto"++_) -> stop;
  16. atomize("att"++_) -> attach;
  17. atomize("sh") -> sh;
  18. atomize("rep"++_) -> sh;
  19. atomize("pla"++_) -> release;
  20. atomize(Else) -> Else.
  21. cwd() -> {ok, Cwd} = file:get_cwd(), Cwd.
  22. home() -> {ok, [[H|_]]} = init:get_argument(home), H.
  23. consult(File) ->
  24. AbsFile = filename:absname(File),
  25. case file:consult(AbsFile) of
  26. {ok, V} ->
  27. V;
  28. _ ->
  29. []
  30. end.
  31. src(Dir) -> filename:join(Dir, "src").
  32. include(Dir) -> filename:join(Dir, "include").
  33. ebin(Dir) -> filename:join(Dir, "ebin").
  34. deps(File) -> get_value(deps, consult(File), []).
  35. get_value(Key, Opts, undefined) -> get_value(Key, Opts, []);
  36. get_value(Key, Opts, Default) ->
  37. case lists:keyfind(Key, 1, Opts) of
  38. {Key, Value} ->
  39. Value;
  40. _ -> Default end.
  41. script(ConfigFile, Conf, _Name) ->
  42. File = ConfigFile ++ ".script",
  43. case file:script(File, [{'CONFIG', Conf}, {'SCRIPT', File}]) of
  44. {ok, {error,_}} -> Conf;
  45. {ok, Out} -> Out;
  46. {error, _} -> Conf
  47. end.
  48. sub_dirs(Cwd, ConfigFile, Conf) ->
  49. sub_dirs(Cwd, ConfigFile, get_value(sub_dirs, Conf, []), []).
  50. sub_dirs(_, _, [], Acc) -> Acc;
  51. sub_dirs(Cwd, ConfigFile, [Dir|T], Acc) ->
  52. SubDir = filename:join(Cwd, Dir),
  53. ConfigFile1 = filename:join(SubDir, ConfigFile),
  54. Conf = consult(ConfigFile1),
  55. Conf1 = mad_script:script(ConfigFile1, Conf, Dir),
  56. Acc1 = sub_dirs(SubDir, ConfigFile, get_value(sub_dirs, Conf1, []),
  57. Acc ++ [SubDir]),
  58. sub_dirs(Cwd, ConfigFile, T, Acc1).
  59. lib_dirs(Cwd, Conf) -> lib_dirs(Cwd, get_value(lib_dirs, Conf, []), []).
  60. raw_deps(Deps) -> raw_deps(Deps,[]).
  61. raw_deps([],Res) -> Res;
  62. raw_deps([D|Deps],Res) when is_tuple(D) -> raw_deps([element(1,D)|Deps],Res);
  63. raw_deps([D|Deps],Res) -> raw_deps(Deps,[D|Res]).
  64. lib_dirs(_, [], Acc) -> Acc;
  65. lib_dirs(Cwd, [H|T], Acc) when is_tuple(H) -> lib_dirs(Cwd, [element(1,H)|T], Acc);
  66. lib_dirs(Cwd, [H|T], Acc) ->
  67. Dirs = filelib:wildcard(filename:join([Cwd, H, "*", "include"])),
  68. lib_dirs(Cwd, T, Acc ++ Dirs).
  69. last_modified(File) ->
  70. case filelib:last_modified(File) of
  71. 0 -> 0;
  72. Else -> calendar:datetime_to_gregorian_seconds(Else) end.
  73. to_atom(X) when is_atom(X) -> X;
  74. to_atom(X) when is_list(X) -> list_to_atom(X);
  75. to_atom(X) when is_binary(X) -> to_atom(binary_to_list(X));
  76. to_atom(X) -> X.
  77. atomize_params_commands(Params) -> atomize_params_commands(Params,[]).
  78. atomize_params_commands([],New) -> New;
  79. atomize_params_commands([H|T], New) -> atomize_params_commands(T,[atomize(H)|New]).
  80. fold_params(Params) ->
  81. Atomized = atomize_params_commands(Params),
  82. lists:foldl(fun(X,{Current,Result}) ->
  83. case atomize(X) of
  84. X when is_atom(X) -> {[],[{X,Current}|Result]};
  85. E -> {[E|Current],Result} end
  86. end, {[],[]}, Atomized).
  87. verbose(Config,Message) ->
  88. case mad_utils:get_value(verbose, Config, 0) of
  89. 0 -> skip;
  90. _ -> mad:info("~s",[binary_to_list(Message)]) end.
  91. compile(_,_,_,_,_) -> false.
  92. configs() ->
  93. Cwd = try fs:path() catch _:_ -> cwd() end,
  94. ConfigFile = "rebar.config",
  95. ConfigFileAbs = filename:join(Cwd, ConfigFile),
  96. Conf = mad_utils:consult(ConfigFileAbs),
  97. Conf1 = mad_script:script(ConfigFileAbs, Conf, ""),
  98. {Cwd,ConfigFile,Conf1}.