From 4e01b32d5db959009dbd57d6928725e33b8aa7be Mon Sep 17 00:00:00 2001 From: Bryan Fink Date: Thu, 4 Feb 2010 14:30:10 -0500 Subject: [PATCH 1/4] look for templates in local directory as well --- src/rebar_templater.erl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 4e2e678..8e07694 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -127,8 +127,9 @@ find_escript_templates() -> re:run(Name, ?TEMPLATE_RE, [{capture, none}]) == match]. find_disk_templates() -> - Files = rebar_utils:find_files(filename:join(os:getenv("HOME"), ".rebar/templates"), ?TEMPLATE_RE), - [{file, F} || F <- Files]. + HomeFiles = rebar_utils:find_files(filename:join(os:getenv("HOME"), ".rebar/templates"), ?TEMPLATE_RE), + LocalFiles = rebar_utils:find_files(".", ?TEMPLATE_RE), + [{file, F} || F <- HomeFiles++LocalFiles]. select_template([], Template) -> ?ABORT("Template ~s not found.\n", [Template]); From 4c2dade0f42169309b742fa13715ef92f2aa70fb Mon Sep 17 00:00:00 2001 From: Bryan Fink Date: Thu, 4 Feb 2010 15:52:44 -0500 Subject: [PATCH 2/4] add Render flag to 'file' template directive {file,In,Out,true} = render with mustache {file,In,Out,false} = do not render with mustache (leave as-is) old-form {file,In,Out} is equivalent to {file,In,Out,true} --- src/rebar_templater.erl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 8e07694..7a21e7d 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -219,7 +219,7 @@ execute_template([], _TemplateType, _TemplateName, _Context, _Force, ExistingFil 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]) end; -execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> +execute_template([{file, Input, Output, Render} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> % determine if the target file already exists FileExists = filelib:is_file(Output), @@ -234,7 +234,10 @@ execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Con true -> ?CONSOLE("Writing ~s~n", [Output]) end, - case file:write_file(Output, render(load_file(TemplateType, InputName), Context)) of + Rendered = if Render -> render(load_file(TemplateType, InputName), Context); + true -> load_file(TemplateType, InputName) + end, + case file:write_file(Output, Rendered) of ok -> execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); {error, Reason} -> @@ -253,6 +256,10 @@ execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context, Forc end; execute_template([{variables, _} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); +execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> + %% old short-form of {file,_,_} is same as + %% new long-form of {file,_,_,true} + execute_template([{file, Input, Output, true}|Rest], TemplateType, TemplateName, Context, Force, ExistingFiles); 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). From 60629e85162110e750b986a1531ce0594e75bd3f Mon Sep 17 00:00:00 2001 From: Bryan Fink Date: Thu, 4 Feb 2010 17:45:05 -0500 Subject: [PATCH 3/4] add chmod template directive for changing file file permission usage: {chmod, Mode, File} where: Mode is an integer, as specified by the 'mode' field of the file_info record (see docs for file:write_file_info/2) --- src/rebar_templater.erl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 7a21e7d..9e2971c 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -254,6 +254,14 @@ execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context, Forc ?ABORT("Failed while processing template instruction {dir, ~s}: ~p\n", [Name, Reason]) 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(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> From f6feac493d17f3fb0b8fa79884e38ce68e9644a8 Mon Sep 17 00:00:00 2001 From: Bryan Fink Date: Sat, 13 Feb 2010 14:14:29 -0500 Subject: [PATCH 4/4] provide separate 'file' and 'template' directives, instead of one 'file' directive with a boolean Render flag --- priv/templates/simpleapp.template | 6 +-- priv/templates/simplenode.template | 4 +- src/rebar_templater.erl | 77 ++++++++++++++++++------------ 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/priv/templates/simpleapp.template b/priv/templates/simpleapp.template index f879dea..a95ce45 100644 --- a/priv/templates/simpleapp.template +++ b/priv/templates/simpleapp.template @@ -1,4 +1,4 @@ {variables, [{appid, "myapp"}]}. -{file, "simpleapp.app", "ebin/{{appid}}.app"}. -{file, "simpleapp_app.erl", "src/{{appid}}_app.erl"}. -{file, "simpleapp_sup.erl", "src/{{appid}}_sup.erl"}. \ No newline at end of file +{template, "simpleapp.app", "ebin/{{appid}}.app"}. +{template, "simpleapp_app.erl", "src/{{appid}}_app.erl"}. +{template, "simpleapp_sup.erl", "src/{{appid}}_sup.erl"}. \ No newline at end of file diff --git a/priv/templates/simplenode.template b/priv/templates/simplenode.template index 2615e35..0f69a45 100644 --- a/priv/templates/simplenode.template +++ b/priv/templates/simplenode.template @@ -1,7 +1,7 @@ {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.nodetool", "overlay/erts-vsn/bin/nodetool"}. {file, "simplenode.runner", "overlay/bin/{{nodeid}}"}. {file, "simplenode.app.config", "overlay/etc/app.config"}. -{file, "simplenode.vm.args", "overlay/etc/vm.args"}. +{template, "simplenode.vm.args", "overlay/etc/vm.args"}. diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 9e2971c..ebb3085 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -207,6 +207,34 @@ render(Bin, Context) -> Str = re:replace(Bin, "\"", "\\\\\"", [global, {return,list}]), 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. %% @@ -219,32 +247,25 @@ execute_template([], _TemplateType, _TemplateName, _Context, _Force, ExistingFil 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]) end; -execute_template([{file, Input, Output, Render} | 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), - filelib:ensure_dir(Output), - if - {Force, FileExists} =:= {"1", true} -> - ?CONSOLE("Writing ~s (forcibly overwriting)~n", [Output]); - true -> - ?CONSOLE("Writing ~s~n", [Output]) - end, - Rendered = if Render -> render(load_file(TemplateType, InputName), Context); - true -> load_file(TemplateType, InputName) - end, - case file:write_file(Output, Rendered) of - ok -> - execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); - {error, Reason} -> - ?ABORT("Failed to write output file ~p: ~p\n", [Output, Reason]) - end; - true -> - execute_template(Rest, TemplateType, TemplateName, Context, Force, [Output|ExistingFiles]) +execute_template([{template, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> + InputName = filename:join(filename:dirname(TemplateName), Input), + case write_file(Output, render(load_file(TemplateType, InputName), Context), Force) of + ok -> + execute_template(Rest, TemplateType, TemplateName, Context, + Force, ExistingFiles); + {error, exists} -> + execute_template(Rest, TemplateType, TemplateName, Context, + Force, [Output|ExistingFiles]) + end; +execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, 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; execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> case filelib:ensure_dir(filename:join(Name, "dummy")) of @@ -264,10 +285,6 @@ execute_template([{chmod, Mod, File} | Rest], TemplateType, TemplateName, Contex end; execute_template([{variables, _} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); -execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> - %% old short-form of {file,_,_} is same as - %% new long-form of {file,_,_,true} - execute_template([{file, Input, Output, true}|Rest], TemplateType, TemplateName, Context, Force, ExistingFiles); 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).