mirror of
https://github.com/correl/rebar.git
synced 2024-12-24 03:00:16 +00:00
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:
parent
2e1b4da71d
commit
e76770511a
5 changed files with 78 additions and 1 deletions
5
inttest/thooks/fish.erl
Normal file
5
inttest/thooks/fish.erl
Normal file
|
@ -0,0 +1,5 @@
|
|||
-module(fish).
|
||||
|
||||
-compile(export_all).
|
||||
|
||||
fish() -> fish.
|
7
inttest/thooks/rebar.config
Normal file
7
inttest/thooks/rebar.config
Normal 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"}]}.
|
40
inttest/thooks/thooks_rt.erl
Normal file
40
inttest/thooks/thooks_rt.erl
Normal 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]).
|
|
@ -138,3 +138,12 @@
|
|||
|
||||
%% Subdirectories?
|
||||
{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"}]}.
|
||||
|
|
|
@ -223,7 +223,7 @@ execute(Command, Modules, Config, ModuleFile) ->
|
|||
case select_modules(Modules, Command, []) of
|
||||
[] ->
|
||||
?WARN("'~p' command does not apply to directory ~s\n",
|
||||
[Command, rebar_utils:get_cwd()]);
|
||||
[Command, rebar_utils:get_cwd()]);
|
||||
|
||||
TargetModules ->
|
||||
%% Provide some info on where we are
|
||||
|
@ -235,9 +235,11 @@ execute(Command, Modules, Config, ModuleFile) ->
|
|||
erlang:put(operations, erlang:get(operations) + 1),
|
||||
|
||||
%% Run the available modules
|
||||
apply_hooks(pre_hooks, Config, Command),
|
||||
case catch(run_modules(TargetModules, Command,
|
||||
Config, ModuleFile)) of
|
||||
ok ->
|
||||
apply_hooks(post_hooks, Config, Command),
|
||||
ok;
|
||||
{error, failed} ->
|
||||
?FAIL;
|
||||
|
@ -302,6 +304,20 @@ run_modules([Module | Rest], Command, Config, File) ->
|
|||
{Module, Error}
|
||||
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(select_modules(Modules, Command, []),
|
||||
|
|
Loading…
Reference in a new issue