Skip to content

Commit

Permalink
Add support for loading template vars from external file
Browse files Browse the repository at this point in the history
  • Loading branch information
dizzyd committed Apr 10, 2011
1 parent 3e2baf1 commit c4907b6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/rebar_templater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,33 @@ create(_Config, _) ->
Context0 = dict:new()
end,

%% Load variables from disk file, if provided
Context1 = case rebar_config:get_global(template_vars, undefined) of
undefined ->
Context0;
File ->
case file:consult(File) of
{ok, Terms} ->
%% TODO: Cleanup/merge with similar code in rebar_reltool
M = fun(_Key, _Base, Override) -> Override end,
dict:merge(M, Context0, dict:from_list(Terms));
{error, Reason} ->
?ABORT("Unable to load template_vars from ~s: ~p\n",
[File, Reason])
end
end,

%% For each variable, see if it's defined in global vars -- if it is,
%% prefer that value over the defaults
Context1 = update_vars(dict:fetch_keys(Context0), Context0),
Context2 = update_vars(dict:fetch_keys(Context1), Context1),
?DEBUG("Template ~p context: ~p\n", [TemplateId, dict:to_list(Context1)]),

%% Handle variables that possibly include other variables in their
%% definition
Context = resolve_recursive_vars(dict:to_list(Context1), Context1),
Context = resolve_recursive_vars(dict:to_list(Context2), Context2),

?DEBUG("Resolved Template ~p context: ~p\n",
[TemplateId, dict:to_list(Context1)]),
[TemplateId, dict:to_list(Context)]),

%% Now, use our context to process the template definition -- this
%% permits us to use variables within the definition for filenames.
Expand Down

0 comments on commit c4907b6

Please sign in to comment.