You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.8 KiB

  1. -module(rebar_hooks).
  2. -export([run_all_hooks/5]).
  3. -spec run_all_hooks(file:filename_all(), pre | post,
  4. atom() | {atom(), atom()} | string(),
  5. [providers:t()], rebar_state:t()) -> ok.
  6. run_all_hooks(Dir, Type, Command, Providers, State) ->
  7. run_provider_hooks(Dir, Type, Command, Providers, State),
  8. run_hooks(Dir, Type, Command, State).
  9. run_provider_hooks(Dir, Type, Command, Providers, State) ->
  10. State1 = rebar_state:providers(rebar_state:dir(State, Dir), Providers),
  11. AllHooks = rebar_state:get(State1, provider_hooks, []),
  12. TypeHooks = proplists:get_value(Type, AllHooks, []),
  13. HookProviders = proplists:get_all_values(Command, TypeHooks),
  14. rebar_core:do(HookProviders, State1).
  15. run_hooks(Dir, Type, Command, State) ->
  16. Hooks = case Type of
  17. pre ->
  18. rebar_state:get(State, pre_hooks, []);
  19. post ->
  20. rebar_state:get(State, post_hooks, []);
  21. _ ->
  22. []
  23. end,
  24. Env = [{"REBAR_DEPS_DIR", filename:absname(rebar_dir:deps_dir(State))}],
  25. lists:foreach(fun({_, C, _}=Hook) when C =:= Command ->
  26. apply_hook(Dir, Env, Hook);
  27. ({C, _}=Hook) when C =:= Command ->
  28. apply_hook(Dir, Env, Hook);
  29. (_) ->
  30. continue
  31. end, Hooks).
  32. apply_hook(Dir, Env, {Arch, Command, Hook}) ->
  33. case rebar_utils:is_arch(Arch) of
  34. true ->
  35. apply_hook(Dir, Env, {Command, Hook});
  36. false ->
  37. ok
  38. end;
  39. apply_hook(Dir, Env, {Command, Hook}) ->
  40. Msg = lists:flatten(io_lib:format("Hook for ~p failed!~n", [Command])),
  41. rebar_utils:sh(Hook, [use_stdout, {cd, Dir}, {env, Env}, {abort_on_error, Msg}]).