summaryrefslogtreecommitdiff
path: root/src/rebar_hooks.erl
blob: e144a8edd4d10edcfa3aa850b5ea490ae4bdb4d1 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-module(rebar_hooks).

-export([run_all_hooks/5]).

-spec run_all_hooks(file:filename_all(), pre | post,
                   atom() | {atom(), atom()} | string(),
                   [providers:t()], rebar_state:t()) -> ok.
run_all_hooks(Dir, Type, Command, Providers, State) ->
    run_provider_hooks(Dir, Type, Command, Providers, State),
    run_hooks(Dir, Type, Command, State).

run_provider_hooks(Dir, Type, Command, Providers, State) ->
    State1 = rebar_state:providers(rebar_state:dir(State, Dir), Providers),
    AllHooks = rebar_state:get(State1, provider_hooks, []),
    TypeHooks = proplists:get_value(Type, AllHooks, []),
    HookProviders = proplists:get_all_values(Command, TypeHooks),
    rebar_core:do(HookProviders, State1).

run_hooks(Dir, Type, Command, State) ->
    Hooks = case Type of
                pre ->
                    rebar_state:get(State, pre_hooks, []);
                post ->
                    rebar_state:get(State, post_hooks, []);
                _ ->
                    []
            end,
    Env = [{"REBAR_DEPS_DIR", filename:absname(rebar_dir:deps_dir(State))}],
    lists:foreach(fun({_, C, _}=Hook) when C =:= Command ->
                          apply_hook(Dir, Env, Hook);
                     ({C, _}=Hook) when C =:= Command ->
                          apply_hook(Dir, Env, 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, [use_stdout, {cd, Dir}, {env, Env}, {abort_on_error, Msg}]).