Skip to content

Commit

Permalink
Adding first crack at reltool support
Browse files Browse the repository at this point in the history
  • Loading branch information
dizzyd committed Dec 2, 2009
1 parent a8fd5bc commit a01e7dc
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 45 deletions.
4 changes: 3 additions & 1 deletion ebin/rebar.app
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]}
]}
]}
]}.
78 changes: 38 additions & 40 deletions src/rebar_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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, []),
Expand Down Expand Up @@ -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
Expand All @@ -123,23 +124,14 @@ target_type(AbsName) ->
{app, AppFile};
false ->
case rebar_rel_utils:is_rel_dir(AbsName) of
true ->
{rel, ""};
{true, ReltoolFile} ->
{rel, ReltoolFile};
false ->
undefined
end
end.


%%
%% 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
%%
Expand All @@ -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).
Expand All @@ -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 ->
Expand All @@ -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 ->
Expand All @@ -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.
9 changes: 5 additions & 4 deletions src/rebar_rel_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
97 changes: 97 additions & 0 deletions src/rebar_reltool.erl
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit a01e7dc

Please sign in to comment.