From 48769e75c224ccd8e04df250b86b50540270ce9a Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Wed, 17 Sep 2014 08:52:49 -0500 Subject: wip: move to erlydtl for rebar templates --- .gitignore | 2 + ebin/rebar.app | 98 +++++---------- priv/templates/LICENSE | 29 ----- priv/templates/LICENSE.dtl | 29 +++++ priv/templates/README.md | 9 -- priv/templates/README.md.dtl | 9 ++ priv/templates/app.erl | 27 ----- priv/templates/app.erl.dtl | 27 +++++ priv/templates/gitignore | 13 -- priv/templates/gitignore.dtl | 13 ++ priv/templates/mod.erl | 13 -- priv/templates/mod.erl.dtl | 13 ++ priv/templates/otp_app.app.src | 12 -- priv/templates/otp_app.app.src.dtl | 12 ++ priv/templates/otp_lib.app.src | 11 -- priv/templates/otp_lib.app.src.dtl | 11 ++ priv/templates/otp_lib.template | 7 -- priv/templates/otp_lib.template.dtl | 7 ++ priv/templates/rebar.config | 2 - priv/templates/rebar.config.dtl | 2 + priv/templates/relx.config | 12 -- priv/templates/relx.config.dtl | 12 ++ priv/templates/sup.erl | 35 ------ priv/templates/sup.erl.dtl | 35 ++++++ priv/templates/sys.config | 3 - priv/templates/sys.config.dtl | 3 + priv/templates/vm.args | 6 - priv/templates/vm.args.dtl | 6 + rebar.config | 3 + src/rebar.app.src | 38 ++++++ src/rebar_mustache.erl | 230 ------------------------------------ src/rebar_templater.erl | 46 +++++--- 32 files changed, 281 insertions(+), 494 deletions(-) delete mode 100644 priv/templates/LICENSE create mode 100644 priv/templates/LICENSE.dtl delete mode 100644 priv/templates/README.md create mode 100644 priv/templates/README.md.dtl delete mode 100644 priv/templates/app.erl create mode 100644 priv/templates/app.erl.dtl delete mode 100644 priv/templates/gitignore create mode 100644 priv/templates/gitignore.dtl delete mode 100644 priv/templates/mod.erl create mode 100644 priv/templates/mod.erl.dtl delete mode 100644 priv/templates/otp_app.app.src create mode 100644 priv/templates/otp_app.app.src.dtl delete mode 100644 priv/templates/otp_lib.app.src create mode 100644 priv/templates/otp_lib.app.src.dtl delete mode 100644 priv/templates/otp_lib.template create mode 100644 priv/templates/otp_lib.template.dtl delete mode 100644 priv/templates/rebar.config create mode 100644 priv/templates/rebar.config.dtl delete mode 100644 priv/templates/relx.config create mode 100644 priv/templates/relx.config.dtl delete mode 100644 priv/templates/sup.erl create mode 100644 priv/templates/sup.erl.dtl delete mode 100644 priv/templates/sys.config create mode 100644 priv/templates/sys.config.dtl delete mode 100644 priv/templates/vm.args create mode 100644 priv/templates/vm.args.dtl create mode 100644 src/rebar.app.src delete mode 100644 src/rebar_mustache.erl diff --git a/.gitignore b/.gitignore index f99ecb2..e9f2ee4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ rebar3 /deps /.rebar rebar.lock +priv/templates/*.dtl.erl +ebin/rebar.app \ No newline at end of file diff --git a/ebin/rebar.app b/ebin/rebar.app index 39354ea..2351695 100644 --- a/ebin/rebar.app +++ b/ebin/rebar.app @@ -1,69 +1,29 @@ -%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- -%% ex: ts=4 sw=4 ft=erlang et - -{application, rebar, - [{description, "Rebar: Erlang Build Tool"}, - {vsn, "3.0.0"}, - {modules, [ rebar3, - rebar_app_utils, - rebar_app_info, - rebar_app_discover, - rebar_base_compiler, - rebar_config, - rebar_core, - rebar_erlc_compiler, - rebar_erlydtl_compiler, - rebar_fetch, - rebar_file_utils, - rebar_log, - rebar_prv_lock, - rebar_otp_app, - rebar_provider, - rebar_packages, - rebar_prv_deps, - rebar_prv_install_deps, - rebar_prv_compile, - rebar_prv_app_discovery, - rebar_prv_escripter, - rebar_prv_release, - rebar_prv_packages, - rebar_prv_new, - rebar_prv_update, - rebar_prv_shell, - rebar_prv_tar, - rebar_mustache, - rebar_state, - rebar_templater, - rebar_topo, - rebar_utils]}, - {registered, []}, - {applications, [kernel, - stdlib, - sasl, - compiler, - crypto, - syntax_tools, - tools, - erlware_commons, - relx, - inets]}, - {env, [ - %% Default log level - {log_level, warn}, - - %% any_dir processing modules - {providers, [rebar_prv_escripter, - rebar_prv_deps, - rebar_prv_lock, - rebar_prv_install_deps, - rebar_prv_packages, - rebar_erlydtl_compiler, - rebar_prv_compile, - rebar_prv_app_discovery, - rebar_prv_shell, - rebar_prv_tar, - rebar_prv_new, - rebar_prv_update, - rebar_prv_release]} - ]} - ]}. +{application,rebar, + [{description,"Rebar: Erlang Build Tool"}, + {vsn,"3.0.0"}, + {modules,['LICENSE_dtl','README.md_dtl','app.erl_dtl', + gitignore_dtl,'mod.erl_dtl','otp_app.app.src_dtl', + 'otp_lib.app.src_dtl','otp_lib.template_dtl', + 'rebar.config_dtl',rebar3,rebar_app_discover, + rebar_app_info,rebar_app_utils,rebar_base_compiler, + rebar_config,rebar_core,rebar_erlc_compiler, + rebar_erlydtl_compiler,rebar_fetch,rebar_file_utils, + rebar_log,rebar_otp_app,rebar_packages,rebar_provider, + rebar_prv_app_discovery,rebar_prv_compile, + rebar_prv_deps,rebar_prv_escripter, + rebar_prv_install_deps,rebar_prv_lock,rebar_prv_new, + rebar_prv_packages,rebar_prv_release,rebar_prv_shell, + rebar_prv_tar,rebar_prv_update,rebar_state, + rebar_templater,rebar_topo,rebar_utils, + 'relx.config_dtl','sup.erl_dtl','sys.config_dtl', + 'vm.args_dtl']}, + {registered,[]}, + {applications,[kernel,stdlib,sasl,compiler,crypto,syntax_tools, + tools,erlware_commons,relx,inets]}, + {env,[{log_level,warn}, + {providers,[rebar_prv_escripter,rebar_prv_deps, + rebar_prv_lock,rebar_prv_install_deps, + rebar_prv_packages,rebar_erlydtl_compiler, + rebar_prv_compile,rebar_prv_app_discovery, + rebar_prv_shell,rebar_prv_tar,rebar_prv_new, + rebar_prv_update,rebar_prv_release]}]}]}. diff --git a/priv/templates/LICENSE b/priv/templates/LICENSE deleted file mode 100644 index 5ce77ce..0000000 --- a/priv/templates/LICENSE +++ /dev/null @@ -1,29 +0,0 @@ -Copyright (c) {{copyright_year}}, {{copyright_holder}} <{{author_email}}>. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - -* Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - -* The names of its contributors may not be used to endorse or promote - products derived from this software without specific prior written - permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/priv/templates/LICENSE.dtl b/priv/templates/LICENSE.dtl new file mode 100644 index 0000000..5ce77ce --- /dev/null +++ b/priv/templates/LICENSE.dtl @@ -0,0 +1,29 @@ +Copyright (c) {{copyright_year}}, {{copyright_holder}} <{{author_email}}>. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +* The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/priv/templates/README.md b/priv/templates/README.md deleted file mode 100644 index ce2898e..0000000 --- a/priv/templates/README.md +++ /dev/null @@ -1,9 +0,0 @@ -{{appid}} -===== - -An Erlang {{appid}} library. - -Build ------ - - $ rebar compile diff --git a/priv/templates/README.md.dtl b/priv/templates/README.md.dtl new file mode 100644 index 0000000..ce2898e --- /dev/null +++ b/priv/templates/README.md.dtl @@ -0,0 +1,9 @@ +{{appid}} +===== + +An Erlang {{appid}} library. + +Build +----- + + $ rebar compile diff --git a/priv/templates/app.erl b/priv/templates/app.erl deleted file mode 100644 index 1c3e8f2..0000000 --- a/priv/templates/app.erl +++ /dev/null @@ -1,27 +0,0 @@ -%%%------------------------------------------------------------------- -%% @doc {{appid}} public API -%% @end -%%%------------------------------------------------------------------- - --module({{appid}}_app). - --behaviour(application). - -%% Application callbacks --export([start/2 - ,stop/1]). - -%%==================================================================== -%% API -%%==================================================================== - -start(_StartType, _StartArgs) -> - {{appid}}_sup:start_link(). - -%%-------------------------------------------------------------------- -stop(_State) -> - ok. - -%%==================================================================== -%% Internal functions -%%==================================================================== diff --git a/priv/templates/app.erl.dtl b/priv/templates/app.erl.dtl new file mode 100644 index 0000000..1c3e8f2 --- /dev/null +++ b/priv/templates/app.erl.dtl @@ -0,0 +1,27 @@ +%%%------------------------------------------------------------------- +%% @doc {{appid}} public API +%% @end +%%%------------------------------------------------------------------- + +-module({{appid}}_app). + +-behaviour(application). + +%% Application callbacks +-export([start/2 + ,stop/1]). + +%%==================================================================== +%% API +%%==================================================================== + +start(_StartType, _StartArgs) -> + {{appid}}_sup:start_link(). + +%%-------------------------------------------------------------------- +stop(_State) -> + ok. + +%%==================================================================== +%% Internal functions +%%==================================================================== diff --git a/priv/templates/gitignore b/priv/templates/gitignore deleted file mode 100644 index 79ae0fb..0000000 --- a/priv/templates/gitignore +++ /dev/null @@ -1,13 +0,0 @@ -.eunit -deps -priv -*.o -*.beam -*.plt -.erlang.cookie -ebin -log -erl_crash.dump -.rebar -_rel -logs diff --git a/priv/templates/gitignore.dtl b/priv/templates/gitignore.dtl new file mode 100644 index 0000000..79ae0fb --- /dev/null +++ b/priv/templates/gitignore.dtl @@ -0,0 +1,13 @@ +.eunit +deps +priv +*.o +*.beam +*.plt +.erlang.cookie +ebin +log +erl_crash.dump +.rebar +_rel +logs diff --git a/priv/templates/mod.erl b/priv/templates/mod.erl deleted file mode 100644 index 1be8186..0000000 --- a/priv/templates/mod.erl +++ /dev/null @@ -1,13 +0,0 @@ --module({{appid}}). - -%% Application callbacks --export([]). - -%%==================================================================== -%% API -%%==================================================================== - - -%%==================================================================== -%% Internal functions -%%==================================================================== diff --git a/priv/templates/mod.erl.dtl b/priv/templates/mod.erl.dtl new file mode 100644 index 0000000..1be8186 --- /dev/null +++ b/priv/templates/mod.erl.dtl @@ -0,0 +1,13 @@ +-module({{appid}}). + +%% Application callbacks +-export([]). + +%%==================================================================== +%% API +%%==================================================================== + + +%%==================================================================== +%% Internal functions +%%==================================================================== diff --git a/priv/templates/otp_app.app.src b/priv/templates/otp_app.app.src deleted file mode 100644 index 1a4e4e8..0000000 --- a/priv/templates/otp_app.app.src +++ /dev/null @@ -1,12 +0,0 @@ -{application, {{appid}}, - [ - {description, "{{appid}}"} - ,{vsn, "0.1.0"} - ,{registered, []} - ,{mod, {'{{appid}}_app', []}} - ,{applications, - [kernel - ,stdlib - ]} - ,{env,[]} - ]}. diff --git a/priv/templates/otp_app.app.src.dtl b/priv/templates/otp_app.app.src.dtl new file mode 100644 index 0000000..1a4e4e8 --- /dev/null +++ b/priv/templates/otp_app.app.src.dtl @@ -0,0 +1,12 @@ +{application, {{appid}}, + [ + {description, "{{appid}}"} + ,{vsn, "0.1.0"} + ,{registered, []} + ,{mod, {'{{appid}}_app', []}} + ,{applications, + [kernel + ,stdlib + ]} + ,{env,[]} + ]}. diff --git a/priv/templates/otp_lib.app.src b/priv/templates/otp_lib.app.src deleted file mode 100644 index 5009c3d..0000000 --- a/priv/templates/otp_lib.app.src +++ /dev/null @@ -1,11 +0,0 @@ -{application, {{appid}}, - [ - {description, "{{appid}}"} - ,{vsn, "0.1.0"} - ,{registered, []} - ,{applications, - [kernel - ,stdlib - ]} - ,{env,[]} - ]}. diff --git a/priv/templates/otp_lib.app.src.dtl b/priv/templates/otp_lib.app.src.dtl new file mode 100644 index 0000000..5009c3d --- /dev/null +++ b/priv/templates/otp_lib.app.src.dtl @@ -0,0 +1,11 @@ +{application, {{appid}}, + [ + {description, "{{appid}}"} + ,{vsn, "0.1.0"} + ,{registered, []} + ,{applications, + [kernel + ,stdlib + ]} + ,{env,[]} + ]}. diff --git a/priv/templates/otp_lib.template b/priv/templates/otp_lib.template deleted file mode 100644 index 19d7593..0000000 --- a/priv/templates/otp_lib.template +++ /dev/null @@ -1,7 +0,0 @@ -{variables, []}. -{template, "mod.erl", "src/{{appid}}.erl"}. -{template, "otp_lib.app.src", "src/{{appid}}.app.src"}. -{template, "rebar.config", "rebar.config"}. -{template, "gitignore", ".gitignore"}. -{template, "LICENSE", "LICENSE"}. -{template, "README.md", "README.md"}. diff --git a/priv/templates/otp_lib.template.dtl b/priv/templates/otp_lib.template.dtl new file mode 100644 index 0000000..19d7593 --- /dev/null +++ b/priv/templates/otp_lib.template.dtl @@ -0,0 +1,7 @@ +{variables, []}. +{template, "mod.erl", "src/{{appid}}.erl"}. +{template, "otp_lib.app.src", "src/{{appid}}.app.src"}. +{template, "rebar.config", "rebar.config"}. +{template, "gitignore", ".gitignore"}. +{template, "LICENSE", "LICENSE"}. +{template, "README.md", "README.md"}. diff --git a/priv/templates/rebar.config b/priv/templates/rebar.config deleted file mode 100644 index f618f3e..0000000 --- a/priv/templates/rebar.config +++ /dev/null @@ -1,2 +0,0 @@ -{erl_opts, [debug_info]}. -{deps, []}. \ No newline at end of file diff --git a/priv/templates/rebar.config.dtl b/priv/templates/rebar.config.dtl new file mode 100644 index 0000000..f618f3e --- /dev/null +++ b/priv/templates/rebar.config.dtl @@ -0,0 +1,2 @@ +{erl_opts, [debug_info]}. +{deps, []}. \ No newline at end of file diff --git a/priv/templates/relx.config b/priv/templates/relx.config deleted file mode 100644 index 7cb3a8a..0000000 --- a/priv/templates/relx.config +++ /dev/null @@ -1,12 +0,0 @@ -%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*- -{release, {'{{appid}}', "0.1.0"}, - [{{appid}}, - sasl]}. - -{sys_config, "./config/sys.config"}. -{vm_args, "./config/vm.args"}. - -{dev_mode, true}. -{include_erts, false}. - -{extended_start_script, true}. diff --git a/priv/templates/relx.config.dtl b/priv/templates/relx.config.dtl new file mode 100644 index 0000000..7cb3a8a --- /dev/null +++ b/priv/templates/relx.config.dtl @@ -0,0 +1,12 @@ +%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*- +{release, {'{{appid}}', "0.1.0"}, + [{{appid}}, + sasl]}. + +{sys_config, "./config/sys.config"}. +{vm_args, "./config/vm.args"}. + +{dev_mode, true}. +{include_erts, false}. + +{extended_start_script, true}. diff --git a/priv/templates/sup.erl b/priv/templates/sup.erl deleted file mode 100644 index 3dd5b12..0000000 --- a/priv/templates/sup.erl +++ /dev/null @@ -1,35 +0,0 @@ -%%%------------------------------------------------------------------- -%% @doc {{appid}} top level supervisor. -%% @end -%%%------------------------------------------------------------------- - --module({{appid}}_sup). - --behaviour(supervisor). - -%% API --export([start_link/0]). - -%% Supervisor callbacks --export([init/1]). - --define(SERVER, ?MODULE). - -%%==================================================================== -%% API functions -%%==================================================================== - -start_link() -> - supervisor:start_link({local, ?SERVER}, ?MODULE, []). - -%%==================================================================== -%% Supervisor callbacks -%%==================================================================== - -%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} -init([]) -> - {ok, { {one_for_all, 0, 1}, []} }. - -%%==================================================================== -%% Internal functions -%%==================================================================== diff --git a/priv/templates/sup.erl.dtl b/priv/templates/sup.erl.dtl new file mode 100644 index 0000000..3dd5b12 --- /dev/null +++ b/priv/templates/sup.erl.dtl @@ -0,0 +1,35 @@ +%%%------------------------------------------------------------------- +%% @doc {{appid}} top level supervisor. +%% @end +%%%------------------------------------------------------------------- + +-module({{appid}}_sup). + +-behaviour(supervisor). + +%% API +-export([start_link/0]). + +%% Supervisor callbacks +-export([init/1]). + +-define(SERVER, ?MODULE). + +%%==================================================================== +%% API functions +%%==================================================================== + +start_link() -> + supervisor:start_link({local, ?SERVER}, ?MODULE, []). + +%%==================================================================== +%% Supervisor callbacks +%%==================================================================== + +%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} +init([]) -> + {ok, { {one_for_all, 0, 1}, []} }. + +%%==================================================================== +%% Internal functions +%%==================================================================== diff --git a/priv/templates/sys.config b/priv/templates/sys.config deleted file mode 100644 index e678cf7..0000000 --- a/priv/templates/sys.config +++ /dev/null @@ -1,3 +0,0 @@ -[ - {'{{appid}}', []} -]. diff --git a/priv/templates/sys.config.dtl b/priv/templates/sys.config.dtl new file mode 100644 index 0000000..e678cf7 --- /dev/null +++ b/priv/templates/sys.config.dtl @@ -0,0 +1,3 @@ +[ + {'{{appid}}', []} +]. diff --git a/priv/templates/vm.args b/priv/templates/vm.args deleted file mode 100644 index 27028ac..0000000 --- a/priv/templates/vm.args +++ /dev/null @@ -1,6 +0,0 @@ --name {{appid}} - --setcookie {{appid}}_cookie - -+K true -+A30 diff --git a/priv/templates/vm.args.dtl b/priv/templates/vm.args.dtl new file mode 100644 index 0000000..27028ac --- /dev/null +++ b/priv/templates/vm.args.dtl @@ -0,0 +1,6 @@ +-name {{appid}} + +-setcookie {{appid}}_cookie + ++K true ++A30 diff --git a/rebar.config b/rebar.config index ddf59fa..e465df1 100644 --- a/rebar.config +++ b/rebar.config @@ -41,3 +41,6 @@ {git, "https://github.com/erlware/relx.git", {branch, "master"}}}, {getopt, "", {git, "git@github.com:jcomellas/getopt.git", {branch, "master"}}}]}. + +{erlydtl_opts, [{doc_root, "priv/templates"}, + {compiler_options, [report, return, debug_info]}]}. diff --git a/src/rebar.app.src b/src/rebar.app.src new file mode 100644 index 0000000..818a0c7 --- /dev/null +++ b/src/rebar.app.src @@ -0,0 +1,38 @@ +%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 ft=erlang et + +{application, rebar, + [{description, "Rebar: Erlang Build Tool"}, + {vsn, "3.0.0"}, + {modules, []}, + {registered, []}, + {applications, [kernel, + stdlib, + sasl, + compiler, + crypto, + syntax_tools, + tools, + erlware_commons, + relx, + inets]}, + {env, [ + %% Default log level + {log_level, warn}, + + %% any_dir processing modules + {providers, [rebar_prv_escripter, + rebar_prv_deps, + rebar_prv_lock, + rebar_prv_install_deps, + rebar_prv_packages, + rebar_erlydtl_compiler, + rebar_prv_compile, + rebar_prv_app_discovery, + rebar_prv_shell, + rebar_prv_tar, + rebar_prv_new, + rebar_prv_update, + rebar_prv_release]} + ]} + ]}. diff --git a/src/rebar_mustache.erl b/src/rebar_mustache.erl deleted file mode 100644 index 9016c0f..0000000 --- a/src/rebar_mustache.erl +++ /dev/null @@ -1,230 +0,0 @@ -%% The MIT License -%% -%% Copyright (c) 2009 Tom Preston-Werner -%% -%% Permission is hereby granted, free of charge, to any person obtaining a copy -%% of this software and associated documentation files (the "Software"), to deal -%% in the Software without restriction, including without limitation the rights -%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -%% copies of the Software, and to permit persons to whom the Software is -%% furnished to do so, subject to the following conditions: -%% -%% The above copyright notice and this permission notice shall be included in -%% all copies or substantial portions of the Software. -%% -%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -%% THE SOFTWARE. - -%% See the README at http://github.com/mojombo/mustache.erl for additional -%% documentation and usage examples. - --module(rebar_mustache). %% v0.1.0 --author("Tom Preston-Werner"). --export([compile/1, compile/2, render/1, render/2, render/3, get/2, get/3, escape/1, start/1]). - --record(mstate, {mod = undefined, - section_re = undefined, - tag_re = undefined}). - --define(MUSTACHE_STR, "rebar_mustache"). - -compile(Body) when is_list(Body) -> - State = #mstate{}, - CompiledTemplate = pre_compile(Body, State), - % io:format("~p~n~n", [CompiledTemplate]), - % io:format(CompiledTemplate ++ "~n", []), - {ok, Tokens, _} = erl_scan:string(CompiledTemplate), - {ok, [Form]} = erl_parse:parse_exprs(Tokens), - Bindings = erl_eval:new_bindings(), - {value, Fun, _} = erl_eval:expr(Form, Bindings), - Fun; -compile(Mod) -> - TemplatePath = template_path(Mod), - compile(Mod, TemplatePath). - -compile(Mod, File) -> - code:purge(Mod), - {module, _} = code:load_file(Mod), - {ok, TemplateBin} = file:read_file(File), - Template = re:replace(TemplateBin, "\"", "\\\\\"", [global, {return,list}]), - State = #mstate{mod = Mod}, - CompiledTemplate = pre_compile(Template, State), - % io:format("~p~n~n", [CompiledTemplate]), - % io:format(CompiledTemplate ++ "~n", []), - {ok, Tokens, _} = erl_scan:string(CompiledTemplate), - {ok, [Form]} = erl_parse:parse_exprs(Tokens), - Bindings = erl_eval:new_bindings(), - {value, Fun, _} = erl_eval:expr(Form, Bindings), - Fun. - -render(Mod) -> - TemplatePath = template_path(Mod), - render(Mod, TemplatePath). - -render(Body, Ctx) when is_list(Body) -> - TFun = compile(Body), - render(undefined, TFun, Ctx); -render(Mod, File) when is_list(File) -> - render(Mod, File, dict:new()); -render(Mod, CompiledTemplate) -> - render(Mod, CompiledTemplate, dict:new()). - -render(Mod, File, Ctx) when is_list(File) -> - CompiledTemplate = compile(Mod, File), - render(Mod, CompiledTemplate, Ctx); -render(Mod, CompiledTemplate, Ctx) -> - Ctx2 = dict:store('__mod__', Mod, Ctx), - lists:flatten(CompiledTemplate(Ctx2)). - -pre_compile(T, State) -> - SectionRE = "\{\{\#([^\}]*)}}\s*(.+?){{\/\\1\}\}\s*", - {ok, CompiledSectionRE} = re:compile(SectionRE, [dotall]), - TagRE = "\{\{(#|=|!|<|>|\{)?(.+?)\\1?\}\}+", - {ok, CompiledTagRE} = re:compile(TagRE, [dotall]), - State2 = State#mstate{section_re = CompiledSectionRE, tag_re = CompiledTagRE}, - "fun(Ctx) -> " ++ - "CFun = fun(A, B) -> A end, " ++ - compiler(T, State2) ++ " end.". - -compiler(T, State) -> - Res = re:run(T, State#mstate.section_re), - case Res of - {match, [{M0, M1}, {N0, N1}, {C0, C1}]} -> - Front = string:substr(T, 1, M0), - Back = string:substr(T, M0 + M1 + 1), - Name = string:substr(T, N0 + 1, N1), - Content = string:substr(T, C0 + 1, C1), - "[" ++ compile_tags(Front, State) ++ - " | [" ++ compile_section(Name, Content, State) ++ - " | [" ++ compiler(Back, State) ++ "]]]"; - nomatch -> - compile_tags(T, State) - end. - -compile_section(Name, Content, State) -> - Mod = State#mstate.mod, - Result = compiler(Content, State), - "fun() -> " ++ - "case " ++ ?MUSTACHE_STR ++ ":get(" ++ Name ++ ", Ctx, " ++ atom_to_list(Mod) ++ ") of " ++ - "\"true\" -> " ++ - Result ++ "; " ++ - "\"false\" -> " ++ - "[]; " ++ - "List when is_list(List) -> " ++ - "[fun(Ctx) -> " ++ Result ++ " end(dict:merge(CFun, SubCtx, Ctx)) || SubCtx <- List]; " ++ - "Else -> " ++ - "throw({template, io_lib:format(\"Bad context for ~p: ~p\", [" ++ Name ++ ", Else])}) " ++ - "end " ++ - "end()". - -compile_tags(T, State) -> - Res = re:run(T, State#mstate.tag_re), - case Res of - {match, [{M0, M1}, K, {C0, C1}]} -> - Front = string:substr(T, 1, M0), - Back = string:substr(T, M0 + M1 + 1), - Content = string:substr(T, C0 + 1, C1), - Kind = tag_kind(T, K), - Result = compile_tag(Kind, Content, State), - "[\"" ++ Front ++ - "\" | [" ++ Result ++ - " | " ++ compile_tags(Back, State) ++ "]]"; - nomatch -> - "[\"" ++ T ++ "\"]" - end. - -tag_kind(_T, {-1, 0}) -> - none; -tag_kind(T, {K0, K1}) -> - string:substr(T, K0 + 1, K1). - -compile_tag(none, Content, State) -> - Mod = State#mstate.mod, - ?MUSTACHE_STR ++ ":escape(" ++ ?MUSTACHE_STR ++ ":get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ "))"; -compile_tag("{", Content, State) -> - Mod = State#mstate.mod, - ?MUSTACHE_STR ++ ":get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ ")"; -compile_tag("!", _Content, _State) -> - "[]". - -template_dir(Mod) -> - DefaultDirPath = filename:dirname(code:which(Mod)), - case application:get_env(mustache, templates_dir) of - {ok, DirPath} when is_list(DirPath) -> - case filelib:ensure_dir(DirPath) of - ok -> DirPath; - _ -> DefaultDirPath - end; - _ -> - DefaultDirPath - end. -template_path(Mod) -> - DirPath = template_dir(Mod), - Basename = atom_to_list(Mod), - filename:join(DirPath, Basename ++ ".mustache"). - -get(Key, Ctx) when is_list(Key) -> - {ok, Mod} = dict:find('__mod__', Ctx), - get(list_to_atom(Key), Ctx, Mod); -get(Key, Ctx) -> - {ok, Mod} = dict:find('__mod__', Ctx), - get(Key, Ctx, Mod). - -get(Key, Ctx, Mod) when is_list(Key) -> - get(list_to_atom(Key), Ctx, Mod); -get(Key, Ctx, Mod) -> - case dict:find(Key, Ctx) of - {ok, Val} -> - % io:format("From Ctx {~p, ~p}~n", [Key, Val]), - to_s(Val); - error -> - case erlang:function_exported(Mod, Key, 1) of - true -> - Val = to_s(Mod:Key(Ctx)), - % io:format("From Mod/1 {~p, ~p}~n", [Key, Val]), - Val; - false -> - case erlang:function_exported(Mod, Key, 0) of - true -> - Val = to_s(Mod:Key()), - % io:format("From Mod/0 {~p, ~p}~n", [Key, Val]), - Val; - false -> - [] - end - end - end. - -to_s(Val) when is_integer(Val) -> - integer_to_list(Val); -to_s(Val) when is_float(Val) -> - io_lib:format("~.2f", [Val]); -to_s(Val) when is_atom(Val) -> - atom_to_list(Val); -to_s(Val) -> - Val. - -escape(HTML) -> - escape(HTML, []). - -escape([], Acc) -> - lists:reverse(Acc); -escape(["<" | Rest], Acc) -> - escape(Rest, lists:reverse("<", Acc)); -escape([">" | Rest], Acc) -> - escape(Rest, lists:reverse(">", Acc)); -escape(["&" | Rest], Acc) -> - escape(Rest, lists:reverse("&", Acc)); -escape([X | Rest], Acc) -> - escape(Rest, [X | Acc]). - -%%--------------------------------------------------------------------------- - -start([T]) -> - Out = render(list_to_atom(T)), - io:format(Out ++ "~n", []). diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index c035ec3..a795b66 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -37,6 +37,7 @@ -include("rebar.hrl"). -define(TEMPLATE_RE, "^[^._].*\\.template\$"). +-define(ERLYDTL_COMPILE_OPTS, [report_warnings, return_errors, {auto_escape, false}, {out_dir, false}]). %% =================================================================== %% Public API @@ -80,20 +81,19 @@ 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 + %% just un-tag it so erlydtl can use it resolve_variables(Rest, dict:store(Key, Dicts, Dict)); resolve_variables([_Pair | Rest], Dict) -> resolve_variables(Rest, Dict). %% -%% Render a binary to a string, using mustache and the specified context +%% Render a binary to a string, using erlydtl 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), - rebar_mustache:render(Str1, Context). +render(Template, Context) when is_atom(Template) -> + Template:render(Context); +render(Template, Context) -> + Module = list_to_atom(Template++"_dtl"), + Module:render(Context). %% =================================================================== %% Internal functions @@ -156,19 +156,19 @@ create1(State, AppDir, TemplateId) -> %% Handle variables that possibly include other variables in their %% definition - Context = resolve_variables(dict:to_list(Context3), Context3), + %Context = resolve_variables(dict:to_list(Context3), Context3), - ?DEBUG("Resolved Template ~p context: ~p\n", - [TemplateId, dict:to_list(Context)]), + %?DEBUG("Resolved Template ~p context: ~p\n", + %[TemplateId, dict:to_list(Context)]), %% Now, use our context to process the template definition -- this %% permits us to use variables within the definition for filenames. - FinalTemplate = consult(render(load_file(Files, Type, Template), Context)), - ?DEBUG("Final template def ~p: ~p\n", [TemplateId, FinalTemplate]), + %FinalTemplate = consult(render(load_file(Files, Type, Template), Context)), + %?DEBUG("Final template def ~p: ~p\n", [TemplateId, FinalTemplate]), %% Execute the instructions in the finalized template Force = rebar_state:get(State, force, "0"), - execute_template(Files, FinalTemplate, Type, Template, Context, Force, []). + execute_template([], TemplateTerms, Type, TemplateId, Context3, Force, []). find_templates(State) -> %% Load a list of all the files in the escript -- cache them since @@ -377,8 +377,12 @@ execute_template(Files, [{'case', Variable, Values, Instructions} | Rest], Templ execute_template(Files, [{template, Input, Output} | Rest], TemplateType, TemplateName, Context, Force, ExistingFiles) -> InputName = filename:join(filename:dirname(TemplateName), Input), - File = load_file(Files, TemplateType, InputName), - case write_file(Output, render(File, Context), Force) of + %File = load_file(Files, TemplateType, InputName), + OutputTemplateName = make_template_name("rebar_output_template", Output), + {ok, OutputTemplateName1} = erlydtl:compile_template(Output, OutputTemplateName, ?ERLYDTL_COMPILE_OPTS), + {ok, OutputRendered} = OutputTemplateName1:render(dict:to_list(Context)), + {ok, Rendered} = render(Input, dict:to_list(Context)), + case write_file(lists:flatten(io_lib:format("~s", [OutputRendered])), Rendered, Force) of ok -> execute_template(Files, Rest, TemplateType, TemplateName, Context, Force, ExistingFiles); @@ -449,3 +453,13 @@ execute_template(Files, [Other | Rest], TemplateType, TemplateName, ?WARN("Skipping unknown template instruction: ~p\n", [Other]), execute_template(Files, Rest, TemplateType, TemplateName, Context, Force, ExistingFiles). + +-spec make_template_name(string(), term()) -> module(). +make_template_name(Base, Value) -> + %% Seed so we get different values each time + random:seed(erlang:now()), + Hash = erlang:phash2(Value), + Ran = random:uniform(10000000), + erlang:list_to_atom(Base ++ "_" ++ + erlang:integer_to_list(Hash) ++ + "_" ++ erlang:integer_to_list(Ran)). -- cgit v1.1