mirror of
https://github.com/correl/rebar.git
synced 2024-12-18 03:00:17 +00:00
Support reading mustache 'lists' from files
This commit add support for reading mustache 'lists' from files, so you can use the list section functionality when templating things. An example of the list syntax is as follows: {package_commands, {list, [[{name, "riak"}], [{name, "riak-admin"}], [{name, "search-cmd"}]]}}. Then you can, for each of the list elements, render some text: {{#package_commands}} chmod +x bin/{{name}} {{/package_commands}}
This commit is contained in:
parent
78fa8fc3d5
commit
ee8919420d
1 changed files with 17 additions and 7 deletions
|
@ -81,6 +81,9 @@ resolve_variables([], Dict) ->
|
||||||
resolve_variables([{Key, Value0} | Rest], Dict) when is_list(Value0) ->
|
resolve_variables([{Key, Value0} | Rest], Dict) when is_list(Value0) ->
|
||||||
Value = render(list_to_binary(Value0), Dict),
|
Value = render(list_to_binary(Value0), Dict),
|
||||||
resolve_variables(Rest, dict:store(Key, Value, Dict));
|
resolve_variables(Rest, dict:store(Key, Value, Dict));
|
||||||
|
resolve_variables([{Key, {list, Dicts}} | Rest], Dict) when is_list(Dicts) ->
|
||||||
|
%% just un-tag it so mustache can use it
|
||||||
|
resolve_variables(Rest, dict:store(Key, Dicts, Dict));
|
||||||
resolve_variables([_Pair | Rest], Dict) ->
|
resolve_variables([_Pair | Rest], Dict) ->
|
||||||
resolve_variables(Rest, Dict).
|
resolve_variables(Rest, Dict).
|
||||||
|
|
||||||
|
@ -134,14 +137,14 @@ create1(Config, TemplateId) ->
|
||||||
undefined ->
|
undefined ->
|
||||||
Context0;
|
Context0;
|
||||||
File ->
|
File ->
|
||||||
case file:consult(File) of
|
case consult(load_file([], file, 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} ->
|
{error, Reason} ->
|
||||||
?ABORT("Unable to load template_vars from ~s: ~p\n",
|
?ABORT("Unable to load template_vars from ~s: ~p\n",
|
||||||
[File, Reason])
|
[File, Reason]);
|
||||||
|
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))
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
@ -275,7 +278,7 @@ consult(Cont, Str, Acc) ->
|
||||||
case Result of
|
case Result of
|
||||||
{ok, Tokens, _} ->
|
{ok, Tokens, _} ->
|
||||||
{ok, Term} = erl_parse:parse_term(Tokens),
|
{ok, Term} = erl_parse:parse_term(Tokens),
|
||||||
consult([], Remaining, [Term | Acc]);
|
consult([], Remaining, [maybe_dict(Term) | Acc]);
|
||||||
{eof, _Other} ->
|
{eof, _Other} ->
|
||||||
lists:reverse(Acc);
|
lists:reverse(Acc);
|
||||||
{error, Info, _} ->
|
{error, Info, _} ->
|
||||||
|
@ -286,6 +289,13 @@ consult(Cont, Str, Acc) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
maybe_dict({Key, {list, Dicts}}) ->
|
||||||
|
%% this is a 'list' element; a list of lists representing dicts
|
||||||
|
{Key, {list, [dict:from_list(D) || D <- Dicts]}};
|
||||||
|
maybe_dict(Term) ->
|
||||||
|
Term.
|
||||||
|
|
||||||
|
|
||||||
write_file(Output, Data, Force) ->
|
write_file(Output, Data, Force) ->
|
||||||
%% determine if the target file already exists
|
%% determine if the target file already exists
|
||||||
FileExists = filelib:is_regular(Output),
|
FileExists = filelib:is_regular(Output),
|
||||||
|
|
Loading…
Reference in a new issue