diff options
author | Dave Smith <dizzyd@dizzyd.com> | 2010-02-14 09:24:47 -0700 |
---|---|---|
committer | Dave Smith <dizzyd@dizzyd.com> | 2010-02-14 09:24:47 -0700 |
commit | 7bfa2405db9b5659eb52f667961bdaae26e78b7d (patch) | |
tree | 56d2359874f034e9661ee1c521e5f6349ee883af | |
parent | 0f7b47ce7b2e946a0ff98fb96e0b82cfc55fd7bb (diff) | |
parent | f6feac493d17f3fb0b8fa79884e38ce68e9644a8 (diff) |
Merging w/ http://bitbucket.org/bryan/rebar
-rw-r--r-- | priv/templates/simpleapp.template | 6 | ||||
-rw-r--r-- | priv/templates/simplenode.template | 4 | ||||
-rw-r--r-- | src/rebar_templater.erl | 79 |
3 files changed, 61 insertions, 28 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 4e2e678..ebb3085 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]); @@ -206,41 +207,65 @@ render(Bin, Context) -> Str = re:replace(Bin, "\"", "\\\\\"", [global, {return,list}]), mustache:render(Str, Context). -%% -%% Execute each instruction in a template definition file. -%% -execute_template([], _TemplateType, _TemplateName, _Context, _Force, ExistingFiles) -> - case length(ExistingFiles) of - 0 -> - ok; - _ -> - Msg = lists:flatten([io_lib:format("\t* ~p~n", [F]) || F <- lists:reverse(ExistingFiles)]), - 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) -> - % determine if the target file already exists +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 + %% 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]); + ?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 + case file:write_file(Output, Data) of ok -> - execute_template(Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); + ok; {error, Reason} -> - ?ABORT("Failed to write output file ~p: ~p\n", [Output, Reason]) + ?ABORT("Failed to write output file ~p: ~p\n", + [Output, Reason]) end; true -> - execute_template(Rest, TemplateType, TemplateName, Context, Force, [Output|ExistingFiles]) + {error, exists} + end. + + +%% +%% Execute each instruction in a template definition file. +%% +execute_template([], _TemplateType, _TemplateName, _Context, _Force, ExistingFiles) -> + case length(ExistingFiles) of + 0 -> + ok; + _ -> + Msg = lists:flatten([io_lib:format("\t* ~p~n", [F]) || F <- lists:reverse(ExistingFiles)]), + 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([{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 @@ -250,6 +275,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([Other | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> |