summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Reid <dreid@mochimedia.com>2010-11-11 09:41:13 -0800
committerTuncer Ayaz <tuncer.ayaz@gmail.com>2010-12-22 17:56:52 +0100
commit63d5ceb61d68af7f1847e3442a1d3f56b6808672 (patch)
tree26a5abcfb65b4b25c790fc8a2aaf2b02c4557e04 /src
parent9b0d35d9869801d6ee52fde8ba443b07d95892e8 (diff)
Support single level of nested template variables
Add support for defining template variables of the following form: {variables, [{appid, "mochiwebapp"}, {author, "Mochi Media <dev@mochimedia.com>"}, {year, "2010"}, {version, "0.1"}, {port, 8080}, {dest, "{{appid}}"}]}. Where dest may be overridden on the commandline but will default to being the appid. Mochiweb uses this so that we can create new projects from the template in a configurable directory. So $ rebar create template=mochiwebapp dest=foo appid=bar I thought about special casing dest but figured it might be generally useful to be able to nest template vars. However this patch only does one level of resolution. So if {variables, [{foo, "{{bar}}"}, {bar, "{{foo}}"}]}. then bar will end up being the literal string {{bar}} and foo the literal string {{foo}}.
Diffstat (limited to 'src')
-rw-r--r--src/rebar_templater.erl24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl
index 4aa5ed6..e251f40 100644
--- a/src/rebar_templater.erl
+++ b/src/rebar_templater.erl
@@ -104,8 +104,14 @@ create(_Config, _) ->
%% For each variable, see if it's defined in global vars -- if it is, prefer that
%% value over the defaults
- Context = update_vars(dict:fetch_keys(Context0), Context0),
- ?DEBUG("Template ~p context: ~p\n", [template_id(), dict:to_list(Context)]),
+ Context1 = update_vars(dict:fetch_keys(Context0), Context0),
+ ?DEBUG("Template ~p context: ~p\n", [template_id(), dict:to_list(Context1)]),
+
+ %% Handle variables that possibly include other variables in their
+ %% definition
+ Context = resolve_recursive_vars(dict:to_list(Context1), Context1),
+
+ ?DEBUG("Resolved Template ~p context: ~p\n", [template_id(), dict:to_list(Context1)]),
%% Now, use our context to process the template definition -- this permits us to
%% use variables within the definition for filenames.
@@ -206,6 +212,19 @@ update_vars([Key | Rest], Dict) ->
%%
+%% Given a list of key value pairs, for each string value attempt to
+%% render it using Dict as the context. Storing the result in Dict as Key.
+%%
+
+resolve_recursive_vars([], Dict) ->
+ Dict;
+resolve_recursive_vars([{Key, Value0} | Rest], Dict) when is_list(Value0) ->
+ Value = render(list_to_binary(Value0), Dict),
+ resolve_recursive_vars(Rest, dict:store(Key, Value, Dict));
+resolve_recursive_vars([_Pair | Rest], Dict) ->
+ resolve_recursive_vars(Rest, Dict).
+
+%%
%% Given a string or binary, parse it into a list of terms, ala file:consult/0
%%
consult(Str) when is_list(Str) ->
@@ -319,4 +338,3 @@ execute_template([{variables, _} | Rest], TemplateType, TemplateName, Context, F
execute_template([Other | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) ->
?WARN("Skipping unknown template instruction: ~p\n", [Other]),
execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles).
-