mad.erl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -module(mad).
  2. -copyright('Maxim Sokhatsky').
  3. -include("mad.hrl").
  4. -compile(export_all).
  5. -export([main/1]).
  6. main([]) -> halt(help());
  7. main(Params) ->
  8. % filter valid (atoms) from invalid (unparsed lists) commands
  9. { _Invalid, Valid } = lists:foldr(
  10. fun (X,{C,R}) when is_atom(X) -> {[],[{X,C}|R]};
  11. (X,{C,R}) -> {[X|C],R} end,
  12. {[],[]}, lists:map(fun atomize/1, Params)),
  13. % return any error if exists on flattened fold of profile runs
  14. % of functions (commands) with arguments
  15. halt(return(
  16. lists:any(fun({error,X}) -> mad:info("~s~n",[X]), true; (_) -> false end,
  17. lists:flatten([
  18. lists:foldl(
  19. fun ({Fun,Arg},ErrAcc) ->
  20. mad_hooks:run_hooks(pre, Fun),
  21. Errors = errors((profile()):Fun(Arg)),
  22. mad_hooks:run_hooks(post, Fun),
  23. Errors ++ ErrAcc
  24. end, [], Valid)])))).
  25. atomize("static") -> 'static';
  26. atomize("deploy") -> 'deploy';
  27. atomize("app"++_) -> 'app';
  28. atomize("dep") -> 'deps';
  29. atomize("deps") -> 'deps';
  30. atomize("cle"++_) -> 'clean';
  31. atomize("com"++_) -> 'compile';
  32. atomize("eunit") -> 'eunit';
  33. atomize("up") -> 'up';
  34. atomize("get") -> 'get';
  35. atomize("rel"++_) -> 'release';
  36. atomize("bun"++_) -> 'release';
  37. atomize("sta"++_) -> 'start';
  38. atomize("sto"++_) -> 'stop';
  39. atomize("att"++_) -> 'attach';
  40. atomize("sh") -> 'sh';
  41. atomize("rep"++_) -> 'sh';
  42. atomize("pla"++_) -> 'resolve';
  43. atomize("str"++_) -> 'strip';
  44. atomize(Else) -> Else.
  45. profile() -> application:get_env(mad,profile,mad_local).
  46. errors([]) -> [];
  47. errors(false) -> [];
  48. errors(true) -> [{error,"Unknown."}];
  49. errors({error,What}) -> info("ERROR~n"), [{error,What}];
  50. errors({ok,_}) -> info("OK~n",[]), [].
  51. return(true) -> 1;
  52. return(false) -> 0.
  53. host() -> try {ok,H} = inet:gethostname(), H catch _:_ -> <<>> end.
  54. info(Format) -> io:format(lists:concat([Format,"\r"])).
  55. info(Format,Args) -> io:format(lists:concat([Format,"\r"]),Args).
  56. help(Reason,D) -> help(io_lib:format("~s ~p", [Reason, D])).
  57. help(_Msg) -> help().
  58. help() -> info("MAD Manage Dependencies ~s~n",[?VERSION]),
  59. info("~n"),
  60. info(" invoke = mad | mad params~n"),
  61. info(" params = [] | command [options] params ~n"),
  62. info(" command = app <sample> | deps | clean | compile | strip~n"),
  63. info(" | bundle [beam|script] | get repo | up [name] ~n"),
  64. info(" | start | stop | attach | sh | static [watch|min] ~n"),
  65. return(false).