Add support for command-specific env for hooks

This commit is contained in:
Tuncer Ayaz 2011-04-05 16:04:03 +02:00
parent 85eb2957c3
commit 20dfd32c85
2 changed files with 26 additions and 17 deletions

View file

@ -234,12 +234,15 @@ execute(Command, Modules, Config, ModuleFile) ->
%% responds to this command
erlang:put(operations, erlang:get(operations) + 1),
%% Check for and get command specific environments
Env = setup_envs(Config, Modules),
%% Run the available modules
apply_hooks(pre_hooks, Config, Command),
apply_hooks(pre_hooks, Config, Command, Env),
case catch(run_modules(TargetModules, Command,
Config, ModuleFile)) of
ok ->
apply_hooks(post_hooks, Config, Command),
apply_hooks(post_hooks, Config, Command, Env),
ok;
{error, failed} ->
?FAIL;
@ -304,14 +307,19 @@ run_modules([Module | Rest], Command, Config, File) ->
{Module, Error}
end.
apply_hooks(Mode, Config, Command) ->
apply_hooks(Mode, Config, Command, Env) ->
Hooks = rebar_config:get_local(Config, Mode, []),
lists:foreach(fun apply_hook/1,
[Hook || Hook <- Hooks, element(1, Hook) =:= Command]).
[{Env, Hook} || Hook <- Hooks, element(1, Hook) =:= Command]).
apply_hook({Command, Hook}) ->
apply_hook({Env, {Command, Hook}}) ->
Msg = lists:flatten(io_lib:format("Command [~p] failed!~n", [Command])),
rebar_utils:sh(Hook, [{abort_on_error, Msg}]).
rebar_utils:sh(Hook, [{env, Env}, {abort_on_error, Msg}]).
setup_envs(Config, Modules) ->
lists:flatten([M:setup_env(Config) ||
M <- Modules,
erlang:function_exported(M, setup_env, 1)]).
acc_modules(Modules, Command, Config, File) ->
acc_modules(select_modules(Modules, Command, []),

View file

@ -27,7 +27,8 @@
-module(rebar_port_compiler).
-export([compile/2,
clean/2]).
clean/2,
setup_env/1]).
-include("rebar.hrl").
@ -94,14 +95,7 @@ compile(Config, AppFile) ->
[] ->
ok;
_ ->
%% Extract environment values from the config (if specified) and
%% merge with the default for this operating system. This enables
%% max flexibility for users.
DefaultEnvs = filter_envs(default_env(), []),
PortEnvs = rebar_config:get_list(Config, port_envs, []),
OverrideEnvs = filter_envs(PortEnvs, []),
RawEnv = DefaultEnvs ++ OverrideEnvs ++ os_env(),
Env = expand_vars_loop(merge_each_var(RawEnv, [])),
Env = setup_env(Config),
%% One or more files are available for building.
%% Run the pre-compile hook, if necessary.
@ -152,8 +146,15 @@ clean(Config, AppFile) ->
%% Run the cleanup script, if it exists
run_cleanup_hook(Config).
setup_env(Config) ->
%% Extract environment values from the config (if specified) and
%% merge with the default for this operating system. This enables
%% max flexibility for users.
DefaultEnvs = filter_envs(default_env(), []),
PortEnvs = rebar_config:get_list(Config, port_envs, []),
OverrideEnvs = filter_envs(PortEnvs, []),
RawEnv = DefaultEnvs ++ OverrideEnvs ++ os_env(),
expand_vars_loop(merge_each_var(RawEnv, [])).
%% ===================================================================
%% Internal functions