Refactor overlay_vars to enable overrides from command-line

This commit is contained in:
Dave Smith 2011-03-29 13:55:47 -06:00
parent 4be3631687
commit b7111dce85

View file

@ -139,24 +139,33 @@ target_dir(ReltoolConfig) ->
end. end.
%% %%
%% Look for overlay_vars file reference. The user can override this from the %% Look for overlay_vars file reference. If the user provides an overlay_vars on
%% command line (i.e. globals), so we check there first and then fall back to %% the command line (i.e. a global), the terms from that file OVERRIDE the one
%% what is present in the reltool.config file %% listed in reltool.config. To re-iterate, this means you can specify a
%% variable in the file from reltool.config and then override that value by
%% providing an additional file on the command-line.
%% %%
overlay_vars(ReltoolConfig) -> overlay_vars(Vars0, ReltoolConfig) ->
case rebar_config:get_global(overlay_vars, undefined) of BaseVars = load_vars_file(proplists:get_value(overlay_vars, ReltoolConfig)),
undefined -> OverrideVars = load_vars_file(rebar_config:get_global(overlay_vars, undefined)),
case lists:keyfind(overlay_vars, 1, ReltoolConfig) of M = fun(_Key, _Base, Override) -> Override end,
{overlay_vars, File} -> dict:merge(M, dict:merge(M, Vars0, BaseVars), OverrideVars).
File;
false -> %%
undefined %% If a filename is provided, construct a dict of terms
end; %%
File -> load_vars_file(undefined) ->
File dict:new();
load_vars_file(File) ->
case file:consult(File) of
{ok, Terms} ->
dict:from_list(Terms);
{error, Reason} ->
?ABORT("Unable to load overlay_vars from ~s: ~p\n", [File, Reason])
end. end.
validate_rel_apps(ReltoolServer, {sys, ReltoolConfig}) -> validate_rel_apps(ReltoolServer, {sys, ReltoolConfig}) ->
case lists:keyfind(rel, 1, ReltoolConfig) of case lists:keyfind(rel, 1, ReltoolConfig) of
false -> false ->
@ -211,22 +220,10 @@ run_reltool(Server, _Config, ReltoolConfig) ->
%% Initialize overlay vars with some basics %% Initialize overlay vars with some basics
%% (that can get overwritten) %% (that can get overwritten)
OverlayVars0 = [{erts_vsn, "erts-" ++ erlang:system_info(version)}], OverlayVars0 = dict:from_list([{erts_vsn, "erts-" ++ erlang:system_info(version)}]),
%% Load up any variables specified by overlay_vars %% Load up any variables specified by overlay_vars
OverlayVars = case overlay_vars(ReltoolConfig) of OverlayVars = overlay_vars(OverlayVars0, ReltoolConfig),
undefined ->
dict:from_list(OverlayVars0);
File ->
case file:consult(File) of
{ok, Terms} ->
dict:from_list(OverlayVars0 ++ Terms);
{error, Reason2} ->
?ABORT("Unable to load overlay_vars "
"from ~s: ~p\n",
[File, Reason2])
end
end,
%% Finally, overlay the files specified by the overlay section %% Finally, overlay the files specified by the overlay section
case lists:keyfind(overlay, 1, ReltoolConfig) of case lists:keyfind(overlay, 1, ReltoolConfig) of