Bläddra i källkod

Do not use rebar_config:set_global in rebar_templater

pull/3/head
Tuncer Ayaz 13 år sedan
förälder
incheckning
e1c9d69d1d
1 ändrade filer med 34 tillägg och 38 borttagningar
  1. +34
    -38
      src/rebar_templater.erl

+ 34
- 38
src/rebar_templater.erl Visa fil

@ -43,15 +43,13 @@
%% Public API %% Public API
%% =================================================================== %% ===================================================================
'create-app'(Config, File) ->
'create-app'(_Config, _File) ->
%% Alias for create w/ template=simpleapp %% Alias for create w/ template=simpleapp
rebar_config:set_global(template, "simpleapp"),
create(Config, File).
create("simpleapp").
'create-node'(Config, File) ->
'create-node'(_Config, _File) ->
%% Alias for create w/ template=simplenode %% Alias for create w/ template=simplenode
rebar_config:set_global(template, "simplenode"),
create(Config, File).
create("simplenode").
'list-templates'(_Config, _File) -> 'list-templates'(_Config, _File) ->
{AvailTemplates, Files} = find_templates(), {AvailTemplates, Files} = find_templates(),
@ -70,13 +68,40 @@
end, AvailTemplates), end, AvailTemplates),
ok. ok.
create(_Config, _) -> create(_Config, _) ->
TemplateId = template_id(),
create(TemplateId).
%%
%% 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_variables([], Dict) ->
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([_Pair | Rest], Dict) ->
resolve_variables(Rest, Dict).
%%
%% Render a binary to a string, using mustache and the specified context
%%
render(Bin, Context) ->
%% Be sure to escape any double-quotes before rendering...
ReOpts = [global, {return, list}],
Str0 = re:replace(Bin, "\\\\", "\\\\\\", ReOpts),
Str1 = re:replace(Str0, "\"", "\\\\\"", ReOpts),
mustache:render(Str1, Context).
%% ===================================================================
%% Internal functions
%% ===================================================================
create(TemplateId) ->
{AvailTemplates, Files} = find_templates(), {AvailTemplates, Files} = find_templates(),
?DEBUG("Available templates: ~p\n", [AvailTemplates]), ?DEBUG("Available templates: ~p\n", [AvailTemplates]),
TemplateId = template_id(),
%% Using the specified template id, find the matching template file/type. %% Using the specified template id, find the matching template file/type.
%% Note that if you define the same template in both ~/.rebar/templates %% Note that if you define the same template in both ~/.rebar/templates
%% that is also present in the escript, the one on the file system will %% that is also present in the escript, the one on the file system will
@ -141,35 +166,6 @@ create(_Config, _) ->
Force = rebar_config:get_global(force, "0"), Force = rebar_config:get_global(force, "0"),
execute_template(Files, FinalTemplate, Type, Template, Context, Force, []). execute_template(Files, FinalTemplate, Type, Template, Context, Force, []).
%%
%% 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_variables([], Dict) ->
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([_Pair | Rest], Dict) ->
resolve_variables(Rest, Dict).
%%
%% Render a binary to a string, using mustache and the specified context
%%
render(Bin, Context) ->
%% Be sure to escape any double-quotes before rendering...
ReOpts = [global, {return, list}],
Str0 = re:replace(Bin, "\\\\", "\\\\\\", ReOpts),
Str1 = re:replace(Str0, "\"", "\\\\\"", ReOpts),
mustache:render(Str1, Context).
%% ===================================================================
%% Internal functions
%% ===================================================================
find_templates() -> find_templates() ->
%% Load a list of all the files in the escript -- cache them since %% Load a list of all the files in the escript -- cache them since
%% we'll potentially need to walk it several times over the course of %% we'll potentially need to walk it several times over the course of

Laddar…
Avbryt
Spara