This commit is contained in:
Dave Smith 2010-02-14 09:24:47 -07:00
commit 7bfa2405db
3 changed files with 62 additions and 29 deletions

View file

@ -1,4 +1,4 @@
{variables, [{appid, "myapp"}]}. {variables, [{appid, "myapp"}]}.
{file, "simpleapp.app", "ebin/{{appid}}.app"}. {template, "simpleapp.app", "ebin/{{appid}}.app"}.
{file, "simpleapp_app.erl", "src/{{appid}}_app.erl"}. {template, "simpleapp_app.erl", "src/{{appid}}_app.erl"}.
{file, "simpleapp_sup.erl", "src/{{appid}}_sup.erl"}. {template, "simpleapp_sup.erl", "src/{{appid}}_sup.erl"}.

View file

@ -1,7 +1,7 @@
{variables, [{nodeid, "mynode"}]}. {variables, [{nodeid, "mynode"}]}.
{file, "simplenode.reltool.config", "reltool.config"}. {template, "simplenode.reltool.config", "reltool.config"}.
{file, "simplenode.erl.script", "overlay/erts-vsn/bin/erl"}. {file, "simplenode.erl.script", "overlay/erts-vsn/bin/erl"}.
{file, "simplenode.nodetool", "overlay/erts-vsn/bin/nodetool"}. {file, "simplenode.nodetool", "overlay/erts-vsn/bin/nodetool"}.
{file, "simplenode.runner", "overlay/bin/{{nodeid}}"}. {file, "simplenode.runner", "overlay/bin/{{nodeid}}"}.
{file, "simplenode.app.config", "overlay/etc/app.config"}. {file, "simplenode.app.config", "overlay/etc/app.config"}.
{file, "simplenode.vm.args", "overlay/etc/vm.args"}. {template, "simplenode.vm.args", "overlay/etc/vm.args"}.

View file

@ -127,8 +127,9 @@ find_escript_templates() ->
re:run(Name, ?TEMPLATE_RE, [{capture, none}]) == match]. re:run(Name, ?TEMPLATE_RE, [{capture, none}]) == match].
find_disk_templates() -> find_disk_templates() ->
Files = rebar_utils:find_files(filename:join(os:getenv("HOME"), ".rebar/templates"), ?TEMPLATE_RE), HomeFiles = rebar_utils:find_files(filename:join(os:getenv("HOME"), ".rebar/templates"), ?TEMPLATE_RE),
[{file, F} || F <- Files]. LocalFiles = rebar_utils:find_files(".", ?TEMPLATE_RE),
[{file, F} || F <- HomeFiles++LocalFiles].
select_template([], Template) -> select_template([], Template) ->
?ABORT("Template ~s not found.\n", [Template]); ?ABORT("Template ~s not found.\n", [Template]);
@ -206,6 +207,34 @@ render(Bin, Context) ->
Str = re:replace(Bin, "\"", "\\\\\"", [global, {return,list}]), Str = re:replace(Bin, "\"", "\\\\\"", [global, {return,list}]),
mustache:render(Str, Context). mustache:render(Str, Context).
write_file(Output, Data, Force) ->
%% determine if the target file already exists
FileExists = filelib:is_file(Output),
%% perform the function if we're allowed,
%% otherwise just process the next template
if
Force =:= "1"; FileExists =:= false ->
filelib:ensure_dir(Output),
if
{Force, FileExists} =:= {"1", true} ->
?CONSOLE("Writing ~s (forcibly overwriting)~n",
[Output]);
true ->
?CONSOLE("Writing ~s~n", [Output])
end,
case file:write_file(Output, Data) of
ok ->
ok;
{error, Reason} ->
?ABORT("Failed to write output file ~p: ~p\n",
[Output, Reason])
end;
true ->
{error, exists}
end.
%% %%
%% Execute each instruction in a template definition file. %% Execute each instruction in a template definition file.
%% %%
@ -218,29 +247,25 @@ execute_template([], _TemplateType, _TemplateName, _Context, _Force, ExistingFil
Help = "To force overwriting, specify force=1 on the command line.\n", Help = "To force overwriting, specify force=1 on the command line.\n",
?ERROR("One or more files already exist on disk and were not generated:~n~s~s", [Msg , Help]) ?ERROR("One or more files already exist on disk and were not generated:~n~s~s", [Msg , Help])
end; end;
execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> execute_template([{template, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) ->
% determine if the target file already exists
FileExists = filelib:is_file(Output),
% perform the function if we're allowed, otherwise just process the next template
if
Force =:= "1"; FileExists =:= false ->
InputName = filename:join(filename:dirname(TemplateName), Input), InputName = filename:join(filename:dirname(TemplateName), Input),
filelib:ensure_dir(Output), case write_file(Output, render(load_file(TemplateType, InputName), Context), Force) of
if
{Force, FileExists} =:= {"1", true} ->
?CONSOLE("Writing ~s (forcibly overwriting)~n", [Output]);
true ->
?CONSOLE("Writing ~s~n", [Output])
end,
case file:write_file(Output, render(load_file(TemplateType, InputName), Context)) of
ok -> ok ->
execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); execute_template(Rest, TemplateType, TemplateName, Context,
{error, Reason} -> Force, ExistingFiles);
?ABORT("Failed to write output file ~p: ~p\n", [Output, Reason]) {error, exists} ->
execute_template(Rest, TemplateType, TemplateName, Context,
Force, [Output|ExistingFiles])
end; end;
true -> execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) ->
execute_template(Rest, TemplateType, TemplateName, Context, Force, [Output|ExistingFiles]) InputName = filename:join(filename:dirname(TemplateName), Input),
case write_file(Output, load_file(TemplateType, InputName), Force) of
ok ->
execute_template(Rest, TemplateType, TemplateName, Context,
Force, ExistingFiles);
{error, exists} ->
execute_template(Rest, TemplateType, TemplateName, Context,
Force, [Output|ExistingFiles])
end; end;
execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) ->
case filelib:ensure_dir(filename:join(Name, "dummy")) of case filelib:ensure_dir(filename:join(Name, "dummy")) of
@ -250,6 +275,14 @@ execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context, Forc
?ABORT("Failed while processing template instruction {dir, ~s}: ~p\n", ?ABORT("Failed while processing template instruction {dir, ~s}: ~p\n",
[Name, Reason]) [Name, Reason])
end; end;
execute_template([{chmod, Mod, File} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) when is_integer(Mod) ->
case file:change_mode(File, Mod) of
ok ->
execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles);
{error, Reason} ->
?ABORT("Failed while processing template instruction {cmod, ~b, ~s}: ~p~n",
[Mod, File, Reason])
end;
execute_template([{variables, _} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> execute_template([{variables, _} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) ->
execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles);
execute_template([Other | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> execute_template([Other | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) ->