summaryrefslogtreecommitdiff
path: root/src/rebar_templater.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rebar_templater.erl')
-rw-r--r--src/rebar_templater.erl48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl
index 0e1eef1..e997975 100644
--- a/src/rebar_templater.erl
+++ b/src/rebar_templater.erl
@@ -35,6 +35,9 @@
-export([resolve_variables/2,
render/2]).
+%% for internal use only
+-export([info/2]).
+
-include("rebar.hrl").
-define(TEMPLATE_RE, ".*\\.template\$").
@@ -81,6 +84,9 @@ resolve_variables([], Dict) ->
resolve_variables([{Key, Value0} | Rest], Dict) when is_list(Value0) ->
Value = render(list_to_binary(Value0), 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(Rest, Dict).
@@ -98,6 +104,27 @@ render(Bin, Context) ->
%% Internal functions
%% ===================================================================
+info(help, create) ->
+ ?CONSOLE(
+ "Create skel based on template and vars.~n"
+ "~n"
+ "Valid command line options:~n"
+ " template= [var=foo,...]~n", []);
+info(help, 'create-app') ->
+ ?CONSOLE(
+ "Create simple app skel.~n"
+ "~n"
+ "Valid command line options:~n"
+ " [appid=myapp]~n", []);
+info(help, 'create-node') ->
+ ?CONSOLE(
+ "Create simple node skel.~n"
+ "~n"
+ "Valid command line options:~n"
+ " [nodeid=mynode]~n", []);
+info(help, 'list-templates') ->
+ ?CONSOLE("List available templates.~n", []).
+
create1(Config, TemplateId) ->
{AvailTemplates, Files} = find_templates(Config),
?DEBUG("Available templates: ~p\n", [AvailTemplates]),
@@ -134,14 +161,14 @@ create1(Config, TemplateId) ->
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));
+ case consult(load_file([], file, File)) of
{error, Reason} ->
?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,
@@ -275,7 +302,7 @@ consult(Cont, Str, Acc) ->
case Result of
{ok, Tokens, _} ->
{ok, Term} = erl_parse:parse_term(Tokens),
- consult([], Remaining, [Term | Acc]);
+ consult([], Remaining, [maybe_dict(Term) | Acc]);
{eof, _Other} ->
lists:reverse(Acc);
{error, Info, _} ->
@@ -286,6 +313,13 @@ consult(Cont, Str, Acc) ->
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) ->
%% determine if the target file already exists
FileExists = filelib:is_regular(Output),