diff options
Diffstat (limited to 'src/rebar_hooks.erl')
-rw-r--r-- | src/rebar_hooks.erl | 77 |
1 files changed, 49 insertions, 28 deletions
diff --git a/src/rebar_hooks.erl b/src/rebar_hooks.erl index 9518542..857336c 100644 --- a/src/rebar_hooks.erl +++ b/src/rebar_hooks.erl @@ -1,6 +1,10 @@ -module(rebar_hooks). --export([run_all_hooks/5]). +-export([run_all_hooks/5 + ,format_error/1]). + +-include("rebar.hrl"). +-include_lib("providers/include/providers.hrl"). -spec run_all_hooks(file:filename_all(), pre | post, atom() | {atom(), atom()} | string(), @@ -10,21 +14,39 @@ run_all_hooks(Dir, Type, Command, Providers, State) -> run_hooks(Dir, Type, Command, State). run_provider_hooks(Dir, Type, Command, Providers, State) -> + case rebar_state:get(State, provider_hooks, []) of + [] -> + ok; + AllHooks -> + TypeHooks = proplists:get_value(Type, AllHooks, []), + run_provider_hooks(Dir, Type, Command, Providers, TypeHooks, State) + end. + +run_provider_hooks(_Dir, _Type, _Command, _Providers, [], _State) -> + ok; +run_provider_hooks(Dir, Type, Command, Providers, TypeHooks, State) -> PluginDepsPaths = rebar_state:code_paths(State, all_plugin_deps), code:add_pathsa(PluginDepsPaths), Providers1 = rebar_state:providers(State), State1 = rebar_state:providers(rebar_state:dir(State, Dir), Providers++Providers1), - AllHooks = rebar_state:get(State1, provider_hooks, []), - TypeHooks = proplists:get_value(Type, AllHooks, []), HookProviders = proplists:get_all_values(Command, TypeHooks), - State2 = rebar_core:do(HookProviders, State1), - rebar_utils:remove_from_code_path(PluginDepsPaths), - State2. + case rebar_core:do(HookProviders, State1) of + {error, ProviderName} -> + ?DEBUG(format_error({bad_provider, Type, Command, ProviderName}), []), + throw(?PRV_ERROR({bad_provider, Type, Command, ProviderName})); + {ok, _} -> + rebar_utils:remove_from_code_path(PluginDepsPaths) + end. + +format_error({bad_provider, Type, Command, {Name, Namespace}}) -> + io_lib:format("Unable to run ~s hooks for '~p', command '~p' in namespace '~p' not found.", [Type, Command, Namespace, Name]); +format_error({bad_provider, Type, Command, Name}) -> + io_lib:format("Unable to run ~s hooks for '~p', command '~p' not found.", [Type, Command, Name]). %% @doc The following environment variables are exported when running %% a hook (absolute paths): -%% +%% %% REBAR_DEPS_DIR = rebar_dir:deps_dir/1 %% REBAR_BUILD_DIR = rebar_dir:base_dir/1 %% REBAR_ROOT_DIR = rebar_dir:root_dir/1 @@ -36,7 +58,7 @@ run_provider_hooks(Dir, Type, Command, Providers, State) -> %% REBAR_APP_DIRS = rebar_dir:lib_dirs/1 %% REBAR_SRC_DIRS = rebar_dir:src_dirs/1 %% -%% autoconf compatible variables +%% autoconf compatible variables %% (see: http://www.gnu.org/software/autoconf/manual/autoconf.html#Erlang-Libraries): %% ERLANG_ERTS_VER = erlang:system_info(version) %% ERLANG_ROOT_DIR = code:root_dir/0 @@ -45,23 +67,24 @@ run_provider_hooks(Dir, Type, Command, Providers, State) -> %% ERL = ERLANG_ROOT_DIR/bin/erl %% ERLC = ERLANG_ROOT_DIR/bin/erl %% +run_hooks(Dir, pre, Command, State) -> + run_hooks(Dir, pre_hooks, Command, State); +run_hooks(Dir, post, Command, State) -> + run_hooks(Dir, post_hooks, Command, State); 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 = create_env(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). + case rebar_state:get(State, Type, []) of + [] -> + ok; + Hooks -> + Env = create_env(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) + end. apply_hook(Dir, Env, {Arch, Command, Hook}) -> case rebar_utils:is_arch(Arch) of @@ -100,8 +123,6 @@ join_dirs(BaseDir, Dirs) -> re_version(Path) -> case re:run(Path, "^.*-(?<VER>[^/-]*)$", [{capture, [1], list}]) of - nomatch -> ""; - {match, [Ver]} -> Ver + nomatch -> ""; + {match, [Ver]} -> Ver end. - - |