summaryrefslogtreecommitdiff
path: root/src/rebar_hooks.erl
blob: 2599d8eb1f47815690864e7911889c16899c9145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-module(rebar_hooks).

-export([run_compile_hooks/4]).

run_compile_hooks(Dir, Type, Command, State) ->
    Hooks = rebar_state:get(State, Type, []),
    lists:foreach(fun({_, C, _}=Hook) when C =:= Command ->
                          apply_hook(Dir, [], Hook);
                     ({C, _}=Hook) when C =:= Command ->
                          apply_hook(Dir, [], Hook);
                     (_) ->
                          continue
                  end, Hooks).

apply_hook(Dir, Env, {Arch, Command, Hook}) ->
    case rebar_utils:is_arch(Arch) of
        true ->
            apply_hook(Dir, Env, {Command, Hook});
        false ->
            ok
    end;
apply_hook(Dir, Env, {Command, Hook}) ->
    Msg = lists:flatten(io_lib:format("Hook for ~p failed!~n", [Command])),
    rebar_utils:sh(Hook, [{cd, Dir}, {env, Env}, {abort_on_error, Msg}]).