From a01e7dcd309af1ee434f4b26495481b0fe2d81e2 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 2 Dec 2009 05:15:35 -0700 Subject: Adding first crack at reltool support --- ebin/rebar.app | 4 +- src/rebar_core.erl | 78 +++++++++++++++++++-------------------- src/rebar_rel_utils.erl | 9 +++-- src/rebar_reltool.erl | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 45 deletions(-) create mode 100644 src/rebar_reltool.erl diff --git a/ebin/rebar.app b/ebin/rebar.app index c889e62..70e3383 100644 --- a/ebin/rebar.app +++ b/ebin/rebar.app @@ -25,7 +25,9 @@ {app_modules, [ rebar_protobuffs_compiler, rebar_erlc_compiler, rebar_port_compiler, - rebar_otp_app ]} + rebar_otp_app ]}, + + {rel_modules, [ rebar_reltool ]} ]} ]} ]}. diff --git a/src/rebar_core.erl b/src/rebar_core.erl index 9911626..868a3e7 100644 --- a/src/rebar_core.erl +++ b/src/rebar_core.erl @@ -32,7 +32,7 @@ %% Public API %% =================================================================== -run(Args) -> +run(Args) -> %% Filter all the flags (i.e. string of form key=value) from the %% command line arguments. What's left will be the commands to run. Commands = filter_flags(Args, []), @@ -100,6 +100,7 @@ find_targets(_Files, _Root, Acc, 10) -> Acc; find_targets([F | Rest], Root, Acc, Depth) -> AbsName = filename:join([Root, F]), + ?DEBUG("find_targets ~s ~s\n", [Root, F]), case target_type(AbsName) of undefined -> case filelib:is_dir(AbsName) of @@ -123,8 +124,8 @@ target_type(AbsName) -> {app, AppFile}; false -> case rebar_rel_utils:is_rel_dir(AbsName) of - true -> - {rel, ""}; + {true, ReltoolFile} -> + {rel, ReltoolFile}; false -> undefined end @@ -132,15 +133,6 @@ target_type(AbsName) -> %% -%% Given a command and target type, determine if the command is applicable -%% -valid_command(compile, app) -> true; -valid_command(install, app) -> true; -valid_command(clean, _Any) -> true; -valid_command(_, _) -> false. - - -%% %% Add all application targets to the front of the code path %% update_code_path([]) -> @@ -148,6 +140,7 @@ update_code_path([]) -> update_code_path([{app, Dir, _} | Rest]) -> EbinDir = filename:join([Dir, "ebin"]), true = code:add_patha(EbinDir), + ?DEBUG("Adding ~s to code path\n", [EbinDir]), update_code_path(Rest); update_code_path([_ | Rest]) -> update_code_path(Rest). @@ -159,10 +152,7 @@ apply_commands(Targets, [CommandStr | Rest]) -> %% Convert the command into an atom for convenience Command = list_to_atom(CommandStr), - %% Filter out all the targets, based on the specified command. - FilteredTargets = [{Type, Dir, Filename} || {Type, Dir, Filename} <- Targets, - valid_command(Command, Type) == true], - case catch(apply_command(FilteredTargets, Command)) of + case catch(apply_command(Targets, Command)) of ok -> apply_commands(Targets, Rest); Other -> @@ -180,6 +170,7 @@ apply_command([{Type, Dir, File} | Rest], Command) -> [] -> ok; Subdirs -> + ?DEBUG("Subdirs: ~p\n", [Subdirs]), update_code_path(Subdirs), case apply_command(Subdirs, Command) of ok -> @@ -189,43 +180,50 @@ apply_command([{Type, Dir, File} | Rest], Command) -> end end, - %% Provide some info on where we are - ?CONSOLE("==> ~s (~s)\n", [filename:basename(Dir), Command]), %% Pull the list of modules that are associated with Type operations. Each module %% will be inspected for a function matching Command and if found, will execute that. - Modules = rebar_config:get_modules(Config, Type), - case catch(run_modules(Modules, Command, Config, File)) of - ok -> + Modules = select_modules(rebar_config:get_modules(Config, Type), Command, []), + case Modules of + [] -> + %% None of the modules implement the command; move on to next target apply_command(Rest, Command); - Other -> - ?ERROR("~p failed while processing ~s: ~p", [Command, Dir, Other]), - error + _ -> + %% Provide some info on where we are + ?CONSOLE("==> ~s (~s)\n", [filename:basename(Dir), Command]), + + %% Run the available modules + case catch(run_modules(Modules, Command, Config, File)) of + ok -> + apply_command(Rest, Command); + Other -> + ?ERROR("~p failed while processing ~s: ~p", [Command, Dir, Other]), + error + end end. -subdirs([], Acc) -> - Acc; -subdirs([Dir | Rest], Acc) -> - Subdir = filename:join([rebar_utils:get_cwd(), Dir]), - subdirs(Rest, Acc ++ find_targets(Subdir)). +subdirs(Dirs, Acc) -> + lists:reverse(find_targets(Dirs, rebar_utils:get_cwd(), [], 1)). + +select_modules([], _Command, Acc) -> + lists:reverse(Acc); +select_modules([Module | Rest], Command, Acc) -> + Exports = Module:module_info(exports), + case lists:member({Command, 2}, Exports) of + true -> + select_modules(Rest, Command, [Module | Acc]); + false -> + select_modules(Rest, Command, Acc) + end. + run_modules([], _Command, _Config, _File) -> ok; run_modules([Module | Rest], Command, Config, File) -> - case invoke_command(Module, Command, Config, File) of + case Module:Command(Config, File) of ok -> run_modules(Rest, Command, Config, File); {error, Reason} -> {error, Reason} end. - -invoke_command(Module, Command, Config, File) -> - Exports = Module:module_info(exports), - case lists:member({Command, 2}, Exports) of - true -> - Module:Command(Config, File); - false -> - ?DEBUG("Skipping ~s:~s/2 (not exported)\n", [Module, Command]), - ok - end. diff --git a/src/rebar_rel_utils.erl b/src/rebar_rel_utils.erl index 0b12d66..99d12f7 100644 --- a/src/rebar_rel_utils.erl +++ b/src/rebar_rel_utils.erl @@ -30,9 +30,10 @@ is_rel_dir() -> is_rel_dir(rebar_util:get_cwd()). is_rel_dir(Dir) -> - case filelib:wildcard("*.rel") of - [_File] -> - true; - _ -> + Fname = filename:join([Dir, "reltool.config"]), + case filelib:is_file(Fname) of + true -> + {true, Fname}; + false -> false end. diff --git a/src/rebar_reltool.erl b/src/rebar_reltool.erl new file mode 100644 index 0000000..dfd75f3 --- /dev/null +++ b/src/rebar_reltool.erl @@ -0,0 +1,97 @@ +%% ------------------------------------------------------------------- +%% +%% rebar: Erlang Build Tools +%% +%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com) +%% +%% Permission is hereby granted, free of charge, to any person obtaining a copy +%% of this software and associated documentation files (the "Software"), to deal +%% in the Software without restriction, including without limitation the rights +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%% copies of the Software, and to permit persons to whom the Software is +%% furnished to do so, subject to the following conditions: +%% +%% The above copyright notice and this permission notice shall be included in +%% all copies or substantial portions of the Software. +%% +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +%% THE SOFTWARE. +%% ------------------------------------------------------------------- +-module(rebar_reltool). + +-export([generate/2, + clean/2]). + +-include("rebar.hrl"). + +%% =================================================================== +%% Public API +%% =================================================================== + +generate(Config, ReltoolFile) -> + %% Load the reltool configuration from the file + case file:consult(ReltoolFile) of + {ok, [{sys, ReltoolConfig}]} -> + ok; + Other -> + ReltoolConfig = undefined, + ?ERROR("Failed to load expected config from ~s: ~p\n", [ReltoolFile, Other]), + ?FAIL + end, + + case catch(run_reltool(Config, ReltoolConfig)) of + ok -> + ok; + {error, failed} -> + ?FAIL; + Other2 -> + ?ERROR("Unexpected error: ~p\n", [Other2]), + ?FAIL + end. + + +clean(Config, ReltoolFile) -> + ok. + + + +%% =================================================================== +%% Internal functions +%% =================================================================== + +%% +%% Determine the name of the target directory; try the user provided name +%% first, or fall back to the release name if that's available. If neither +%% is available, just use "target" +%% +target_dir(Config, ReltoolConfig) -> + case rebar_config:get(Config, target_name, undefined) of + undefined -> + case lists:keysearch(rel, 1, ReltoolConfig) of + {value, {rel, Name, _Vsn, _Apps}} -> + Name; + false -> + "target" + end; + Name -> + Name + end. + +run_reltool(Config, ReltoolConfig) -> + {ok, Server} = reltool:start_server([{sys, ReltoolConfig}]), + {ok, Spec} = reltool:get_target_spec(Server), + TargetDir = target_dir(Config, ReltoolConfig), + ok = file:make_dir(TargetDir), + case reltool:eval_target_spec(Spec, code:root_dir(), TargetDir) of + ok -> + ok; + {error, Reason} -> + ?ERROR("Failed to generate target from spec: ~p\n", [Reason]), + ?FAIL + end. + -- cgit v1.1