Add pre and post script support for all commands

This change makes it possible to assign pre/post scripts to all
rebar commands. This allows users fine grained control over
when scripts and/or shell commands should be executed, where such
extensions are absolutely needed.

Several examples have been added to the rebar.config.sample file.
This commit is contained in:
Tim Watson 2011-01-26 01:50:33 +00:00 committed by Tuncer Ayaz
parent 2e1b4da71d
commit e76770511a
5 changed files with 78 additions and 1 deletions

5
inttest/thooks/fish.erl Normal file
View file

@ -0,0 +1,5 @@
-module(fish).
-compile(export_all).
fish() -> fish.

View file

@ -0,0 +1,7 @@
%% pre-scripts
{pre_hooks, [{clean, "echo preclean >> preclean.out"},
{compile, "echo precompile >> precompile.out"}]}.
%% post-scripts
{post_hooks, [{clean, "echo postclean >> postclean.out"},
{compile, "echo postcompile >> postcompile.out"}]}.

View file

@ -0,0 +1,40 @@
-module(thooks_rt).
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
files() ->
[
%% dummy lfe files
{copy, "../../rebar", "rebar"},
{copy, "rebar.config", "rebar.config"},
{copy, "fish.erl", "src/fish.erl"},
{create, "ebin/fish.app", app(fish, [fish])}
].
run(_Dir) ->
?assertMatch({ok, _}, retest_sh:run("./rebar -v clean compile", [])),
ensure_command_ran_only_once("preclean"),
ensure_command_ran_only_once("precompile"),
ensure_command_ran_only_once("postclean"),
ensure_command_ran_only_once("postcompile"),
ok.
ensure_command_ran_only_once(Command) ->
File = Command ++ ".out",
?assert(filelib:is_regular(File)),
%% ensure that this command only ran once (not for each module)
{ok, Content} = file:read_file(File),
?assertEqual(Command ++ "\n", binary_to_list(Content)).
%%
%% Generate the contents of a simple .app file
%%
app(Name, Modules) ->
App = {application, Name,
[{description, atom_to_list(Name)},
{vsn, "1"},
{modules, Modules},
{registered, []},
{applications, [kernel, stdlib]}]},
io_lib:format("~p.\n", [App]).

View file

@ -138,3 +138,12 @@
%% Subdirectories? %% Subdirectories?
{sub_dirs, ["dir1", "dir2"]}. {sub_dirs, ["dir1", "dir2"]}.
%% == Pre/Post Command Hooks ==
{pre_hooks, [{clean, "./prepare_package_files.sh"},
{compile, "escript generate_headers"}]}.
{post_hooks, [{clean, "touch file1.out"},
{eunit, "touch file2.out"},
{compile, "touch postcompile.out"}]}.

View file

@ -235,9 +235,11 @@ execute(Command, Modules, Config, ModuleFile) ->
erlang:put(operations, erlang:get(operations) + 1), erlang:put(operations, erlang:get(operations) + 1),
%% Run the available modules %% Run the available modules
apply_hooks(pre_hooks, Config, Command),
case catch(run_modules(TargetModules, Command, case catch(run_modules(TargetModules, Command,
Config, ModuleFile)) of Config, ModuleFile)) of
ok -> ok ->
apply_hooks(post_hooks, Config, Command),
ok; ok;
{error, failed} -> {error, failed} ->
?FAIL; ?FAIL;
@ -302,6 +304,20 @@ run_modules([Module | Rest], Command, Config, File) ->
{Module, Error} {Module, Error}
end. end.
apply_hooks(Mode, Config, Command) ->
case rebar_config:get_local(Config, Mode, []) of
[] ->
skip;
Hooks when is_list(Hooks) ->
lists:foreach(fun apply_hook/1,
[{Command, Hook} || Hook <- Hooks])
end.
apply_hook({Command, {Command, Hook}}) ->
Msg = lists:flatten(io_lib:format("Command [~p] failed!~n", [Command])),
rebar_utils:sh(Hook, [{abort_on_error, Msg}]);
apply_hook({Command, {HookCmd, _}}) when Command =/= HookCmd ->
skip.
acc_modules(Modules, Command, Config, File) -> acc_modules(Modules, Command, Config, File) ->
acc_modules(select_modules(Modules, Command, []), acc_modules(select_modules(Modules, Command, []),