From 2e5496f5f5f3c39bd1e23b4601d1e89d4d2c9767 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Tue, 28 Apr 2015 19:50:55 -0500 Subject: remove use of erlydtl in rebar --- bootstrap | 9 +- priv/templates/LICENSE | 29 ++++ priv/templates/LICENSE.dtl | 29 ---- priv/templates/Makefile | 74 ++++++++++ priv/templates/Makefile.dtl | 74 ---------- priv/templates/README.md | 9 ++ priv/templates/README.md.dtl | 9 -- priv/templates/app.erl | 27 ++++ priv/templates/app.erl.dtl | 27 ---- priv/templates/app.template | 14 +- priv/templates/cmake.template | 2 +- priv/templates/gitignore | 18 +++ priv/templates/gitignore.dtl | 18 --- priv/templates/lib.template | 12 +- 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 | 9 ++ priv/templates/otp_lib.app.src.dtl | 9 -- priv/templates/plugin.erl | 33 +++++ priv/templates/plugin.erl.dtl | 33 ----- priv/templates/plugin.template | 12 +- priv/templates/plugin_README.md | 26 ++++ priv/templates/plugin_README.md.dtl | 26 ---- priv/templates/rebar.config | 2 + priv/templates/rebar.config.dtl | 2 - priv/templates/release.template | 18 +-- priv/templates/relx_rebar.config | 20 +++ priv/templates/relx_rebar.config.dtl | 20 --- 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 | 33 +---- rebar.lock | 16 +-- src/rebar.app.src | 1 - src/rebar_mustache.erl | 230 +++++++++++++++++++++++++++++ src/rebar_prv_erlydtl_compiler.erl | 270 ----------------------------------- src/rebar_templater.erl | 81 ++++------- test/rebar_erlydtl_SUITE.erl | 72 ---------- 43 files changed, 611 insertions(+), 791 deletions(-) create mode 100644 priv/templates/LICENSE delete mode 100644 priv/templates/LICENSE.dtl create mode 100644 priv/templates/Makefile delete mode 100644 priv/templates/Makefile.dtl create mode 100644 priv/templates/README.md delete mode 100644 priv/templates/README.md.dtl create mode 100644 priv/templates/app.erl delete mode 100644 priv/templates/app.erl.dtl create mode 100644 priv/templates/gitignore delete mode 100644 priv/templates/gitignore.dtl create mode 100644 priv/templates/mod.erl delete mode 100644 priv/templates/mod.erl.dtl create mode 100644 priv/templates/otp_app.app.src delete mode 100644 priv/templates/otp_app.app.src.dtl create mode 100644 priv/templates/otp_lib.app.src delete mode 100644 priv/templates/otp_lib.app.src.dtl create mode 100644 priv/templates/plugin.erl delete mode 100644 priv/templates/plugin.erl.dtl create mode 100644 priv/templates/plugin_README.md delete mode 100644 priv/templates/plugin_README.md.dtl create mode 100644 priv/templates/rebar.config delete mode 100644 priv/templates/rebar.config.dtl create mode 100644 priv/templates/relx_rebar.config delete mode 100644 priv/templates/relx_rebar.config.dtl create mode 100644 priv/templates/sup.erl delete mode 100644 priv/templates/sup.erl.dtl create mode 100644 priv/templates/sys.config delete mode 100644 priv/templates/sys.config.dtl create mode 100644 priv/templates/vm.args delete mode 100644 priv/templates/vm.args.dtl create mode 100644 src/rebar_mustache.erl delete mode 100644 src/rebar_prv_erlydtl_compiler.erl delete mode 100644 test/rebar_erlydtl_SUITE.erl diff --git a/bootstrap b/bootstrap index 9799647..a96483b 100755 --- a/bootstrap +++ b/bootstrap @@ -26,7 +26,7 @@ main(_Args) -> {ok, State} = rebar3:run(["compile"]), reset_env(), os:putenv("REBAR_PROFILE", ""), - %% Build erlydtl files (a hook on compile in the default profile) and escript file + DepsPaths = rebar_state:code_paths(State, all_deps), code:add_pathsa(DepsPaths), @@ -112,16 +112,15 @@ bootstrap_rebar3() -> code:add_patha(filename:absname("_build/default/lib/rebar/ebin")). setup_env() -> - %% We don't need or want erlydtl or relx providers loaded yet + %% We don't need or want relx providers loaded yet application:load(rebar), {ok, Providers} = application:get_env(rebar, providers), - Providers1 = Providers -- [rebar_prv_erlydtl_compiler, - rebar_prv_release, + Providers1 = Providers -- [rebar_prv_release, rebar_prv_tar], application:set_env(rebar, providers, Providers1). reset_env() -> - %% Reset the env so we get all providers and can build erlydtl files + %% Reset the env so we get all providers application:unset_env(rebar, providers), application:unload(rebar), application:load(rebar). diff --git a/priv/templates/LICENSE b/priv/templates/LICENSE new file mode 100644 index 0000000..41588ab --- /dev/null +++ b/priv/templates/LICENSE @@ -0,0 +1,29 @@ +Copyright (c) {{copyright_year}}, {{author_name}} <{{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 deleted file mode 100644 index 41588ab..0000000 --- a/priv/templates/LICENSE.dtl +++ /dev/null @@ -1,29 +0,0 @@ -Copyright (c) {{copyright_year}}, {{author_name}} <{{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/Makefile b/priv/templates/Makefile new file mode 100644 index 0000000..d3c3767 --- /dev/null +++ b/priv/templates/Makefile @@ -0,0 +1,74 @@ +# Based on c_src.mk from erlang.mk by Loic Hoguin + +CURDIR := $(shell pwd) +BASEDIR := $(abspath $(CURDIR)/..) + +PROJECT ?= $(notdir $(BASEDIR)) +PROJECT := $(strip $(PROJECT)) + +ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).") +ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).") +ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).") + +C_SRC_DIR = $(CURDIR) +C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so + +# System type and C compiler/flags. + +UNAME_SYS := $(shell uname -s) +ifeq ($(UNAME_SYS), Darwin) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall + LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress +else ifeq ($(UNAME_SYS), FreeBSD) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +else ifeq ($(UNAME_SYS), Linux) + CC ?= gcc + CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -Wall +endif + +CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) +CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) + +LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei +LDFLAGS += -shared + +# Verbosity. + +c_verbose_0 = @echo " C " $(?F); +c_verbose = $(c_verbose_$(V)) + +cpp_verbose_0 = @echo " CPP " $(?F); +cpp_verbose = $(cpp_verbose_$(V)) + +link_verbose_0 = @echo " LD " $(@F); +link_verbose = $(link_verbose_$(V)) + +SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \)) +OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) + +COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c +COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c + +$(C_SRC_OUTPUT): $(OBJECTS) + @mkdir -p $(BASEDIR)/priv/ + $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) + +%.o: %.c + $(COMPILE_C) $(OUTPUT_OPTION) $< + +%.o: %.cc + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +%.o: %.C + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +%.o: %.cpp + $(COMPILE_CPP) $(OUTPUT_OPTION) $< + +clean: + @rm -f $(C_SRC_OUTPUT) $(OBJECTS) diff --git a/priv/templates/Makefile.dtl b/priv/templates/Makefile.dtl deleted file mode 100644 index d3c3767..0000000 --- a/priv/templates/Makefile.dtl +++ /dev/null @@ -1,74 +0,0 @@ -# Based on c_src.mk from erlang.mk by Loic Hoguin - -CURDIR := $(shell pwd) -BASEDIR := $(abspath $(CURDIR)/..) - -PROJECT ?= $(notdir $(BASEDIR)) -PROJECT := $(strip $(PROJECT)) - -ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).") -ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).") -ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).") - -C_SRC_DIR = $(CURDIR) -C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so - -# System type and C compiler/flags. - -UNAME_SYS := $(shell uname -s) -ifeq ($(UNAME_SYS), Darwin) - CC ?= cc - CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes - CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall - LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress -else ifeq ($(UNAME_SYS), FreeBSD) - CC ?= cc - CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes - CXXFLAGS ?= -O3 -finline-functions -Wall -else ifeq ($(UNAME_SYS), Linux) - CC ?= gcc - CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes - CXXFLAGS ?= -O3 -finline-functions -Wall -endif - -CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) - -LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei -LDFLAGS += -shared - -# Verbosity. - -c_verbose_0 = @echo " C " $(?F); -c_verbose = $(c_verbose_$(V)) - -cpp_verbose_0 = @echo " CPP " $(?F); -cpp_verbose = $(cpp_verbose_$(V)) - -link_verbose_0 = @echo " LD " $(@F); -link_verbose = $(link_verbose_$(V)) - -SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \)) -OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) - -COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c -COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c - -$(C_SRC_OUTPUT): $(OBJECTS) - @mkdir -p $(BASEDIR)/priv/ - $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT) - -%.o: %.c - $(COMPILE_C) $(OUTPUT_OPTION) $< - -%.o: %.cc - $(COMPILE_CPP) $(OUTPUT_OPTION) $< - -%.o: %.C - $(COMPILE_CPP) $(OUTPUT_OPTION) $< - -%.o: %.cpp - $(COMPILE_CPP) $(OUTPUT_OPTION) $< - -clean: - @rm -f $(C_SRC_OUTPUT) $(OBJECTS) diff --git a/priv/templates/README.md b/priv/templates/README.md new file mode 100644 index 0000000..5507536 --- /dev/null +++ b/priv/templates/README.md @@ -0,0 +1,9 @@ +{{name}} +===== + +{{desc}} + +Build +----- + + $ rebar3 compile diff --git a/priv/templates/README.md.dtl b/priv/templates/README.md.dtl deleted file mode 100644 index 5507536..0000000 --- a/priv/templates/README.md.dtl +++ /dev/null @@ -1,9 +0,0 @@ -{{name}} -===== - -{{desc}} - -Build ------ - - $ rebar3 compile diff --git a/priv/templates/app.erl b/priv/templates/app.erl new file mode 100644 index 0000000..83eb9a3 --- /dev/null +++ b/priv/templates/app.erl @@ -0,0 +1,27 @@ +%%%------------------------------------------------------------------- +%% @doc {{name}} public API +%% @end +%%%------------------------------------------------------------------- + +-module({{name}}_app). + +-behaviour(application). + +%% Application callbacks +-export([start/2 + ,stop/1]). + +%%==================================================================== +%% API +%%==================================================================== + +start(_StartType, _StartArgs) -> + {{name}}_sup:start_link(). + +%%-------------------------------------------------------------------- +stop(_State) -> + ok. + +%%==================================================================== +%% Internal functions +%%==================================================================== diff --git a/priv/templates/app.erl.dtl b/priv/templates/app.erl.dtl deleted file mode 100644 index 83eb9a3..0000000 --- a/priv/templates/app.erl.dtl +++ /dev/null @@ -1,27 +0,0 @@ -%%%------------------------------------------------------------------- -%% @doc {{name}} public API -%% @end -%%%------------------------------------------------------------------- - --module({{name}}_app). - --behaviour(application). - -%% Application callbacks --export([start/2 - ,stop/1]). - -%%==================================================================== -%% API -%%==================================================================== - -start(_StartType, _StartArgs) -> - {{name}}_sup:start_link(). - -%%-------------------------------------------------------------------- -stop(_State) -> - ok. - -%%==================================================================== -%% Internal functions -%%==================================================================== diff --git a/priv/templates/app.template b/priv/templates/app.template index b17e30c..4087e5e 100644 --- a/priv/templates/app.template +++ b/priv/templates/app.template @@ -3,10 +3,10 @@ {name, "mylib", "Name of the OTP application"}, {desc, "An OTP application", "Short description of the app"} ]}. -{template, "app.erl.dtl", "{{name}}/src/{{name}}_app.erl"}. -{template, "sup.erl.dtl", "{{name}}/src/{{name}}_sup.erl"}. -{template, "otp_app.app.src.dtl", "{{name}}/src/{{name}}.app.src"}. -{template, "rebar.config.dtl", "{{name}}/rebar.config"}. -{template, "gitignore.dtl", "{{name}}/.gitignore"}. -{template, "LICENSE.dtl", "{{name}}/LICENSE"}. -{template, "README.md.dtl", "{{name}}/README.md"}. +{template, "app.erl", "{{name}}/src/{{name}}_app.erl"}. +{template, "sup.erl", "{{name}}/src/{{name}}_sup.erl"}. +{template, "otp_app.app.src", "{{name}}/src/{{name}}.app.src"}. +{template, "rebar.config", "{{name}}/rebar.config"}. +{template, "gitignore", "{{name}}/.gitignore"}. +{template, "LICENSE", "{{name}}/LICENSE"}. +{template, "README.md", "{{name}}/README.md"}. diff --git a/priv/templates/cmake.template b/priv/templates/cmake.template index b3f23e8..2874c8a 100644 --- a/priv/templates/cmake.template +++ b/priv/templates/cmake.template @@ -1,2 +1,2 @@ {description, "Makefile for building C/C++ in c_src"}. -{template, "Makefile.dtl", "c_src/Makefile"}. +{template, "Makefile", "c_src/Makefile"}. diff --git a/priv/templates/gitignore b/priv/templates/gitignore new file mode 100644 index 0000000..40a1d4f --- /dev/null +++ b/priv/templates/gitignore @@ -0,0 +1,18 @@ +.rebar3 +_* +.eunit +*.o +*.beam +*.plt +*.swp +*.swo +.erlang.cookie +ebin +log +erl_crash.dump +.rebar +_rel +_deps +_plugins +_tdeps +logs diff --git a/priv/templates/gitignore.dtl b/priv/templates/gitignore.dtl deleted file mode 100644 index 40a1d4f..0000000 --- a/priv/templates/gitignore.dtl +++ /dev/null @@ -1,18 +0,0 @@ -.rebar3 -_* -.eunit -*.o -*.beam -*.plt -*.swp -*.swo -.erlang.cookie -ebin -log -erl_crash.dump -.rebar -_rel -_deps -_plugins -_tdeps -logs diff --git a/priv/templates/lib.template b/priv/templates/lib.template index 4b9a75d..cbb3672 100644 --- a/priv/templates/lib.template +++ b/priv/templates/lib.template @@ -3,9 +3,9 @@ {name, "mylib", "Name of the OTP library application"}, {desc, "An OTP library", "Short description of the app"} ]}. -{template, "mod.erl.dtl", "{{name}}/src/{{name}}.erl"}. -{template, "otp_lib.app.src.dtl", "{{name}}/src/{{name}}.app.src"}. -{template, "rebar.config.dtl", "{{name}}/rebar.config"}. -{template, "gitignore.dtl", "{{name}}/.gitignore"}. -{template, "LICENSE.dtl", "{{name}}/LICENSE"}. -{template, "README.md.dtl", "{{name}}/README.md"}. +{template, "mod.erl", "{{name}}/src/{{name}}.erl"}. +{template, "otp_lib.app.src", "{{name}}/src/{{name}}.app.src"}. +{template, "rebar.config", "{{name}}/rebar.config"}. +{template, "gitignore", "{{name}}/.gitignore"}. +{template, "LICENSE", "{{name}}/LICENSE"}. +{template, "README.md", "{{name}}/README.md"}. diff --git a/priv/templates/mod.erl b/priv/templates/mod.erl new file mode 100644 index 0000000..2f5e09e --- /dev/null +++ b/priv/templates/mod.erl @@ -0,0 +1,13 @@ +-module({{name}}). + +%% API exports +-export([]). + +%%==================================================================== +%% API functions +%%==================================================================== + + +%%==================================================================== +%% Internal functions +%%==================================================================== diff --git a/priv/templates/mod.erl.dtl b/priv/templates/mod.erl.dtl deleted file mode 100644 index 2f5e09e..0000000 --- a/priv/templates/mod.erl.dtl +++ /dev/null @@ -1,13 +0,0 @@ --module({{name}}). - -%% API exports --export([]). - -%%==================================================================== -%% API functions -%%==================================================================== - - -%%==================================================================== -%% Internal functions -%%==================================================================== diff --git a/priv/templates/otp_app.app.src b/priv/templates/otp_app.app.src new file mode 100644 index 0000000..5188f56 --- /dev/null +++ b/priv/templates/otp_app.app.src @@ -0,0 +1,12 @@ +{application, {{name}}, + [{description, "{{desc}}"} + ,{vsn, "0.1.0"} + ,{registered, []} + ,{mod, {'{{name}}_app', []}} + ,{applications, + [kernel + ,stdlib + ]} + ,{env,[]} + ,{modules, []} + ]}. diff --git a/priv/templates/otp_app.app.src.dtl b/priv/templates/otp_app.app.src.dtl deleted file mode 100644 index 5188f56..0000000 --- a/priv/templates/otp_app.app.src.dtl +++ /dev/null @@ -1,12 +0,0 @@ -{application, {{name}}, - [{description, "{{desc}}"} - ,{vsn, "0.1.0"} - ,{registered, []} - ,{mod, {'{{name}}_app', []}} - ,{applications, - [kernel - ,stdlib - ]} - ,{env,[]} - ,{modules, []} - ]}. diff --git a/priv/templates/otp_lib.app.src b/priv/templates/otp_lib.app.src new file mode 100644 index 0000000..3adefeb --- /dev/null +++ b/priv/templates/otp_lib.app.src @@ -0,0 +1,9 @@ +{application, {{name}}, + [{description, "{{desc}}"} + ,{vsn, "0.1.0"} + ,{registered, []} + ,{applications, + [kernel,stdlib]} + ,{env,[]} + ,{modules, []} + ]}. diff --git a/priv/templates/otp_lib.app.src.dtl b/priv/templates/otp_lib.app.src.dtl deleted file mode 100644 index 3adefeb..0000000 --- a/priv/templates/otp_lib.app.src.dtl +++ /dev/null @@ -1,9 +0,0 @@ -{application, {{name}}, - [{description, "{{desc}}"} - ,{vsn, "0.1.0"} - ,{registered, []} - ,{applications, - [kernel,stdlib]} - ,{env,[]} - ,{modules, []} - ]}. diff --git a/priv/templates/plugin.erl b/priv/templates/plugin.erl new file mode 100644 index 0000000..abf4648 --- /dev/null +++ b/priv/templates/plugin.erl @@ -0,0 +1,33 @@ +-module({{name}}). +-behaviour(provider). + +-export([init/1, do/1, format_error/1]). + +-define(PROVIDER, {{name}}). +-define(DEPS, [app_discovery]). + +%% =================================================================== +%% Public API +%% =================================================================== +-spec init(rebar_state:t()) -> {ok, rebar_state:t()}. +init(State) -> + Provider = providers:create([ + {name, ?PROVIDER}, % The 'user friendly' name of the task + {module, ?MODULE}, % The module implementation of the task + {bare, true}, % The task can be run by the user, always true + {deps, ?DEPS}, % The list of dependencies + {example, "rebar3 {{name}}"}, % How to use the plugin + {opts, []}, % list of options understood by the plugin + {short_desc, "{{desc}}"}, + {desc, "{{desc}}"} + ]), + {ok, rebar_state:add_provider(State, Provider)}. + + +-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. +do(State) -> + {ok, State}. + +-spec format_error(any()) -> iolist(). +format_error(Reason) -> + io_lib:format("~p", [Reason]). diff --git a/priv/templates/plugin.erl.dtl b/priv/templates/plugin.erl.dtl deleted file mode 100644 index abf4648..0000000 --- a/priv/templates/plugin.erl.dtl +++ /dev/null @@ -1,33 +0,0 @@ --module({{name}}). --behaviour(provider). - --export([init/1, do/1, format_error/1]). - --define(PROVIDER, {{name}}). --define(DEPS, [app_discovery]). - -%% =================================================================== -%% Public API -%% =================================================================== --spec init(rebar_state:t()) -> {ok, rebar_state:t()}. -init(State) -> - Provider = providers:create([ - {name, ?PROVIDER}, % The 'user friendly' name of the task - {module, ?MODULE}, % The module implementation of the task - {bare, true}, % The task can be run by the user, always true - {deps, ?DEPS}, % The list of dependencies - {example, "rebar3 {{name}}"}, % How to use the plugin - {opts, []}, % list of options understood by the plugin - {short_desc, "{{desc}}"}, - {desc, "{{desc}}"} - ]), - {ok, rebar_state:add_provider(State, Provider)}. - - --spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. -do(State) -> - {ok, State}. - --spec format_error(any()) -> iolist(). -format_error(Reason) -> - io_lib:format("~p", [Reason]). diff --git a/priv/templates/plugin.template b/priv/templates/plugin.template index 3419191..0181bd2 100644 --- a/priv/templates/plugin.template +++ b/priv/templates/plugin.template @@ -3,9 +3,9 @@ {name, "myplugin", "Name of the plugin"}, {desc, "A rebar plugin", "Short description of the plugin's purpose"} ]}. -{template, "plugin.erl.dtl", "{{name}}/src/{{name}}.erl"}. -{template, "otp_lib.app.src.dtl", "{{name}}/src/{{name}}.app.src"}. -{template, "rebar.config.dtl", "{{name}}/rebar.config"}. -{template, "gitignore.dtl", "{{name}}/.gitignore"}. -{template, "LICENSE.dtl", "{{name}}/LICENSE"}. -{template, "plugin_README.md.dtl", "{{name}}/README.md"}. +{template, "plugin.erl", "{{name}}/src/{{name}}.erl"}. +{template, "otp_lib.app.src", "{{name}}/src/{{name}}.app.src"}. +{template, "rebar.config", "{{name}}/rebar.config"}. +{template, "gitignore", "{{name}}/.gitignore"}. +{template, "LICENSE", "{{name}}/LICENSE"}. +{template, "plugin_README.md", "{{name}}/README.md"}. diff --git a/priv/templates/plugin_README.md b/priv/templates/plugin_README.md new file mode 100644 index 0000000..7f9ba84 --- /dev/null +++ b/priv/templates/plugin_README.md @@ -0,0 +1,26 @@ +{{name}} +===== + +{{desc}} + +Build +----- + + $ rebar3 compile + +Use +--- + +Add the plugin to your rebar config: + + {plugins, [ + { {{name}}, ".*", {git, "git@host:user/{{name}}.git", {tag, "0.1.0"}}} + ]}. + +Then just call your plugin directly in an existing application: + + + $ rebar3 {{name}} + ===> Fetching {{name}} + ===> Compiling {{name}} + diff --git a/priv/templates/plugin_README.md.dtl b/priv/templates/plugin_README.md.dtl deleted file mode 100644 index 7f9ba84..0000000 --- a/priv/templates/plugin_README.md.dtl +++ /dev/null @@ -1,26 +0,0 @@ -{{name}} -===== - -{{desc}} - -Build ------ - - $ rebar3 compile - -Use ---- - -Add the plugin to your rebar config: - - {plugins, [ - { {{name}}, ".*", {git, "git@host:user/{{name}}.git", {tag, "0.1.0"}}} - ]}. - -Then just call your plugin directly in an existing application: - - - $ rebar3 {{name}} - ===> Fetching {{name}} - ===> Compiling {{name}} - diff --git a/priv/templates/rebar.config b/priv/templates/rebar.config new file mode 100644 index 0000000..f618f3e --- /dev/null +++ b/priv/templates/rebar.config @@ -0,0 +1,2 @@ +{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 deleted file mode 100644 index f618f3e..0000000 --- a/priv/templates/rebar.config.dtl +++ /dev/null @@ -1,2 +0,0 @@ -{erl_opts, [debug_info]}. -{deps, []}. \ No newline at end of file diff --git a/priv/templates/release.template b/priv/templates/release.template index e350130..a15d163 100644 --- a/priv/templates/release.template +++ b/priv/templates/release.template @@ -3,12 +3,12 @@ {name, "myapp", "Name of the OTP release. An app with this name will also be created."}, {desc, "An OTP application", "Short description of the release's main app's purpose"} ]}. -{template, "app.erl.dtl", "{{name}}/{{apps_dir}}/{{name}}/src/{{name}}_app.erl"}. -{template, "sup.erl.dtl", "{{name}}/{{apps_dir}}/{{name}}/src/{{name}}_sup.erl"}. -{template, "otp_app.app.src.dtl", "{{name}}/{{apps_dir}}/{{name}}/src/{{name}}.app.src"}. -{template, "relx_rebar.config.dtl", "{{name}}/rebar.config"}. -{template, "sys.config.dtl", "{{name}}/config/sys.config"}. -{template, "vm.args.dtl", "{{name}}/config/vm.args"}. -{template, "gitignore.dtl", "{{name}}/.gitignore"}. -{template, "LICENSE.dtl", "{{name}}/LICENSE"}. -{template, "README.md.dtl", "{{name}}/README.md"}. +{template, "app.erl", "{{name}}/{{apps_dir}}/{{name}}/src/{{name}}_app.erl"}. +{template, "sup.erl", "{{name}}/{{apps_dir}}/{{name}}/src/{{name}}_sup.erl"}. +{template, "otp_app.app.src", "{{name}}/{{apps_dir}}/{{name}}/src/{{name}}.app.src"}. +{template, "relx_rebar.config", "{{name}}/rebar.config"}. +{template, "sys.config", "{{name}}/config/sys.config"}. +{template, "vm.args", "{{name}}/config/vm.args"}. +{template, "gitignore", "{{name}}/.gitignore"}. +{template, "LICENSE", "{{name}}/LICENSE"}. +{template, "README.md", "{{name}}/README.md"}. diff --git a/priv/templates/relx_rebar.config b/priv/templates/relx_rebar.config new file mode 100644 index 0000000..e7505eb --- /dev/null +++ b/priv/templates/relx_rebar.config @@ -0,0 +1,20 @@ +{erl_opts, [debug_info]}. +{deps, []}. + +{relx, [{release, {'{{name}}', "0.1.0"}, + [{{name}}, + sasl]}, + + {sys_config, "./config/sys.config"}, + {vm_args, "./config/vm.args"}, + + {dev_mode, true}, + {include_erts, false}, + + {extended_start_script, true}] +}. + +{profiles, [{prod, [{relx, [{dev_mode, false}, + {include_erts, true}]}] + }] +}. diff --git a/priv/templates/relx_rebar.config.dtl b/priv/templates/relx_rebar.config.dtl deleted file mode 100644 index e7505eb..0000000 --- a/priv/templates/relx_rebar.config.dtl +++ /dev/null @@ -1,20 +0,0 @@ -{erl_opts, [debug_info]}. -{deps, []}. - -{relx, [{release, {'{{name}}', "0.1.0"}, - [{{name}}, - sasl]}, - - {sys_config, "./config/sys.config"}, - {vm_args, "./config/vm.args"}, - - {dev_mode, true}, - {include_erts, false}, - - {extended_start_script, true}] -}. - -{profiles, [{prod, [{relx, [{dev_mode, false}, - {include_erts, true}]}] - }] -}. diff --git a/priv/templates/sup.erl b/priv/templates/sup.erl new file mode 100644 index 0000000..a2e7209 --- /dev/null +++ b/priv/templates/sup.erl @@ -0,0 +1,35 @@ +%%%------------------------------------------------------------------- +%% @doc {{name}} top level supervisor. +%% @end +%%%------------------------------------------------------------------- + +-module({{name}}_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 deleted file mode 100644 index a2e7209..0000000 --- a/priv/templates/sup.erl.dtl +++ /dev/null @@ -1,35 +0,0 @@ -%%%------------------------------------------------------------------- -%% @doc {{name}} top level supervisor. -%% @end -%%%------------------------------------------------------------------- - --module({{name}}_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 new file mode 100644 index 0000000..7fd6bcb --- /dev/null +++ b/priv/templates/sys.config @@ -0,0 +1,3 @@ +[ + {'{{name}}', []} +]. diff --git a/priv/templates/sys.config.dtl b/priv/templates/sys.config.dtl deleted file mode 100644 index 7fd6bcb..0000000 --- a/priv/templates/sys.config.dtl +++ /dev/null @@ -1,3 +0,0 @@ -[ - {'{{name}}', []} -]. diff --git a/priv/templates/vm.args b/priv/templates/vm.args new file mode 100644 index 0000000..a8a43f0 --- /dev/null +++ b/priv/templates/vm.args @@ -0,0 +1,6 @@ +-name {{name}} + +-setcookie {{name}}_cookie + ++K true ++A30 diff --git a/priv/templates/vm.args.dtl b/priv/templates/vm.args.dtl deleted file mode 100644 index a8a43f0..0000000 --- a/priv/templates/vm.args.dtl +++ /dev/null @@ -1,6 +0,0 @@ --name {{name}} - --setcookie {{name}}_cookie - -+K true -+A30 diff --git a/rebar.config b/rebar.config index 83da0e5..89acac7 100644 --- a/rebar.config +++ b/rebar.config @@ -8,18 +8,15 @@ {providers, "", {git, "https://github.com/tsloughter/providers.git", {tag, "v1.3.1"}}}, - {erlydtl, "", - {git, "https://github.com/erlydtl/erlydtl.git", - {branch, "master"}}}, {relx, "", - {git, "https://github.com/erlware/relx.git", - {branch, "master"}}}, + {git, "https://github.com/tsloughter/relx.git", + {branch, "mustache"}}}, {getopt, "", {git, "https://github.com/jcomellas/getopt.git", {branch, "master"}}}]}. {escript_incl_apps, - [getopt, merl, erlydtl, erlware_commons, relx, providers, rebar]}. + [getopt, erlware_commons, relx, providers, rebar]}. {escript_top_level_app, rebar}. {escript_name, rebar3}. {escript_emu_args, "%%! +sbtu +A0\n"}. @@ -34,12 +31,7 @@ debug_info, warnings_as_errors]}. -{erlydtl_opts, [{doc_root, "priv/templates"}, - {compiler_options, [report, return, debug_info]}]}. - -{dialyzer_plt_apps, [common_test, dialyzer, erlydtl, eunit, snmp]}. - -{provider_hooks, [{post, [{compile, {erlydtl, compile}}]}]}. +{dialyzer_plt_apps, [common_test, dialyzer, eunit, snmp]}. %% Profiles {profiles, [{test, @@ -50,21 +42,8 @@ }, %% We don't want erlydtl to attempt to run on the first compile pass to bootstrap - {bootstrap, [{overrides, [{override, relx, [{provider_hooks, [{post, []}]}]}]}, - {provider_hooks, [{post, []}]}]} + {bootstrap, []} ]}. %% Overrides -{overrides, [{override, erlware_commons, [{plugins, []}]}, - {override, merl, [{pre_hooks, [{"(linux|darwin|solaris)", compile, "make -C \"$REBAR_DEPS_DIR/merl\" all -W test"}, - {"(freebsd|netbsd|openbsd)", compile, "gmake -C \"$REBAR_DEPS_DIR/merl\" all"}, - {"win32", compile, "make -C \"%REBAR_DEPS_DIR%/merl\" all -W test"}, - {eunit, - "erlc -I include/erlydtl_preparser.hrl -o test" - " test/erlydtl_extension_testparser.yrl"}, - {"(linux|darwin|solaris)", eunit, "make -C \"$REBAR_DEPS_DIR/merl\" test"}, - {"(freebsd|netbsd|openbsd)", eunit, "gmake -C \"$REBAR_DEPS_DIR/merl\" test"}, - {"win32", eunit, "make -C \"%REBAR_DEPS_DIR%/merl\" test"} - ]}]}, - {override, erlydtl, [{pre_hooks, []}]} - ]}. +{overrides, [{override, erlware_commons, [{plugins, []}]}]}. diff --git a/rebar.lock b/rebar.lock index 89dca39..2734711 100644 --- a/rebar.lock +++ b/rebar.lock @@ -2,17 +2,9 @@ {git,"https://github.com/erlware/rebar_vsn_plugin.git", {ref,"fd40c960c7912193631d948fe962e1162a8d1334"}}, 1}, - {<<"merl">>, - {git,"git://github.com/erlydtl/merl.git", - {ref,"750b09d44425f435ff579a4d28bf5844bb5b4ef1"}}, - 1}, - {<<"eunit_formatters">>, - {git,"git://github.com/seancribbs/eunit_formatters", - {ref,"2c73eb6e46b0863f19507857b386a48a53aaf141"}}, - 1}, {<<"relx">>, - {git,"https://github.com/erlware/relx.git", - {ref,"f8af9f5cd9f70ff43a043fe274dda4e56be82a2d"}}, + {git,"https://github.com/tsloughter/relx.git", + {ref,"732ad5a3f902a07972460731504ca7e085b80ae5"}}, 0}, {<<"providers">>, {git,"https://github.com/tsloughter/providers.git", @@ -22,10 +14,6 @@ {git,"https://github.com/jcomellas/getopt.git", {ref,"626698975e63866156159661d100785d65eab6f9"}}, 0}, - {<<"erlydtl">>, - {git,"https://github.com/erlydtl/erlydtl.git", - {ref,"a4ac28680d6e066aabf86b3be9f073352a1a4d40"}}, - 0}, {<<"erlware_commons">>, {git,"https://github.com/erlware/erlware_commons.git", {ref,"ef0d252b11c863f9c228af2fe93a4e42fba2f7f3"}}, diff --git a/src/rebar.app.src b/src/rebar.app.src index b404c42..5862ebd 100644 --- a/src/rebar.app.src +++ b/src/rebar.app.src @@ -37,7 +37,6 @@ rebar_prv_dialyzer, rebar_prv_do, rebar_prv_edoc, - rebar_prv_erlydtl_compiler, rebar_prv_escriptize, rebar_prv_eunit, rebar_prv_help, diff --git a/src/rebar_mustache.erl b/src/rebar_mustache.erl new file mode 100644 index 0000000..9016c0f --- /dev/null +++ b/src/rebar_mustache.erl @@ -0,0 +1,230 @@ +%% 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_prv_erlydtl_compiler.erl b/src/rebar_prv_erlydtl_compiler.erl deleted file mode 100644 index a7442af..0000000 --- a/src/rebar_prv_erlydtl_compiler.erl +++ /dev/null @@ -1,270 +0,0 @@ -%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- -%% ex: ts=4 sw=4 et -%% ------------------------------------------------------------------- -%% -%% rebar: Erlang Build Tools -%% -%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com), -%% Bryan Fink (bryan@basho.com) -%% -%% 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. -%% ------------------------------------------------------------------- - -%% The rebar_erlydtl_compiler module is a plugin for rebar that compiles -%% ErlyDTL templates. By default, it compiles all templates/*.dtl -%% to ebin/*_dtl.beam. -%% -%% Configuration options should be placed in rebar.config under -%% 'erlydtl_opts'. It can be a list of name-value tuples or a list of -%% lists of name-value tuples if you have multiple template directories -%% that need to have different settings (see example below). -%% -%% Available options include: -%% -%% doc_root: where to find templates to compile -%% "templates" by default -%% -%% out_dir: where to put compiled template beam files -%% "ebin" by default -%% -%% source_ext: the file extension the template sources have -%% ".dtl" by default -%% -%% module_ext: characters to append to the template's module name -%% "_dtl" by default -%% -%% recursive: boolean that determines if doc_root(s) need to be -%% scanned recursively for matching template file names -%% (default: true). -%% For example, if you had: -%% /t_src/ -%% base.html -%% foo.html -%% -%% And you wanted them compiled to: -%% /priv/ -%% base.beam -%% foo.beam -%% -%% You would add to your rebar.config: -%% {erlydtl_opts, [ -%% {doc_root, "t_src"}, -%% {out_dir, "priv"}, -%% {source_ext, ".html"}, -%% {module_ext, ""} -%% ]}. -%% -%% The default settings are the equivalent of: -%% {erlydtl_opts, [ -%% {doc_root, "templates"}, -%% {out_dir, "ebin"}, -%% {source_ext, ".dtl"}, -%% {module_ext, "_dtl"} -%% ]}. -%% -%% The following example will compile the following templates: -%% "src/*.dtl" files into "ebin/*_dtl.beam" and -%% "templates/*.html" into "ebin/*.beam". Note that any tuple option -%% (such as 'out_dir') in the outer list is added to each inner list: -%% {erlydtl_opts, [ -%% {out_dir, "ebin"}, -%% {recursive, false}, -%% [ -%% {doc_root, "src"}, {module_ext, "_dtl"} -%% ], -%% [ -%% {doc_root, "templates"}, {module_ext, ""}, {source_ext, ".html"} -%% ] -%% ]}. --module(rebar_prv_erlydtl_compiler). - --behaviour(provider). - --export([init/1, - do/1, - format_error/1]). - --include("rebar.hrl"). --include_lib("providers/include/providers.hrl"). - --define(PROVIDER, compile). --define(DEPS, [{default, compile}]). - -%% =================================================================== -%% Public API -%% =================================================================== - --spec init(rebar_state:t()) -> {ok, rebar_state:t()}. -init(State) -> - State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER}, - {module, ?MODULE}, - {namespace, erlydtl}, - {bare, false}, - {deps, ?DEPS}, - {example, "rebar3 erlydtl compile"}, - {short_desc, "Compile erlydtl templates."}, - {desc, "Compile erlydtl templates."}, - {opts, []}])), - {ok, State1}. - -do(State) -> - ?INFO("Running erlydtl...", []), - case rebar_state:get(State, escript_main_app, undefined) of - undefined -> - Dir = rebar_state:dir(State), - case rebar_app_discover:find_app(Dir, all) of - {true, AppInfo} -> - AllApps = rebar_state:project_apps(State) ++ rebar_state:all_deps(State), - case rebar_app_utils:find(rebar_app_info:name(AppInfo), AllApps) of - {ok, AppInfo1} -> - %% Use the existing app info instead of newly created one - run_erlydtl(AppInfo1, State); - _ -> - run_erlydtl(AppInfo, State) - end, - {ok, State}; - _ -> - ?PRV_ERROR(no_main_app) - end; - Name -> - AllApps = rebar_state:project_apps(State) ++ rebar_state:all_deps(State), - {ok, App} = rebar_app_utils:find(Name, AllApps), - run_erlydtl(App, State), - {ok, State} - end. - -run_erlydtl(App, State) -> - Dir = rebar_state:dir(State), - DtlOpts = proplists:unfold(rebar_state:get(State, erlydtl_opts, [])), - TemplateDir = filename:join(Dir, option(doc_root, DtlOpts)), - DtlOpts2 = [{doc_root, TemplateDir} | proplists:delete(doc_root, DtlOpts)], - OutDir = rebar_app_info:ebin_dir(App), - filelib:ensure_dir(filename:join(OutDir, "dummy.beam")), - rebar_base_compiler:run(State, - [], - TemplateDir, - option(source_ext, DtlOpts2), - OutDir, - option(module_ext, DtlOpts2) ++ ".beam", - fun(S, T, C) -> - compile_dtl(C, S, T, DtlOpts2, Dir, OutDir) - end, - [{check_last_mod, false}, - {recursive, option(recursive, DtlOpts2)}]). - --spec format_error(any()) -> iolist(). -format_error(no_main_app) -> - "Erlydtl Error: Multiple project apps found and no {app, atom()} option found in erlydtl_opts."; -format_error(Reason) -> - io_lib:format("~p", [Reason]). - -%% =================================================================== -%% Internal functions -%% =================================================================== - -option(Opt, DtlOpts) -> - proplists:get_value(Opt, DtlOpts, default(Opt)). - -default(app) -> undefined; -default(doc_root) -> "priv/templates"; -default(source_ext) -> ".dtl"; -default(module_ext) -> "_dtl"; -default(custom_tags_dir) -> ""; -default(compiler_options) -> [return]; -default(recursive) -> true. - -compile_dtl(State, Source, Target, DtlOpts, Dir, OutDir) -> - case needs_compile(Source, Target, DtlOpts) of - true -> - do_compile(State, Source, Target, DtlOpts, Dir, OutDir); - false -> - skipped - end. - -do_compile(State, Source, Target, DtlOpts, Dir, OutDir) -> - CompilerOptions = option(compiler_options, DtlOpts), - - Sorted = proplists:unfold( - lists:sort( - [{out_dir, OutDir}, - {doc_root, filename:join(Dir, option(doc_root, DtlOpts))}, - {custom_tags_dir, option(custom_tags_dir, DtlOpts)}, - {compiler_options, CompilerOptions}])), - - %% ensure that doc_root and out_dir are defined, - %% using defaults if necessary - Opts = lists:ukeymerge(1, DtlOpts, Sorted), - ?DEBUG("Compiling \"~s\" -> \"~s\" with options:~n ~s", - [Source, Target, io_lib:format("~p", [Opts])]), - case erlydtl:compile_file(ec_cnv:to_list(Source), - list_to_atom(module_name(Target)), - Opts) of - {ok, _Mod} -> - ok; - {ok, _Mod, Ws} -> - rebar_base_compiler:ok_tuple(State, Source, Ws); - error -> - rebar_base_compiler:error_tuple(State, Source, [], [], Opts); - {error, Es, Ws} -> - rebar_base_compiler:error_tuple(State, Source, Es, Ws, Opts) - end. - -module_name(Target) -> - filename:rootname(filename:basename(Target), ".beam"). - -needs_compile(Source, Target, DtlOpts) -> - LM = filelib:last_modified(Target), - LM < filelib:last_modified(Source) orelse - lists:any(fun(D) -> LM < filelib:last_modified(D) end, - referenced_dtls(Source, DtlOpts)). - -referenced_dtls(Source, DtlOpts) -> - DtlOpts1 = lists:keyreplace(doc_root, 1, DtlOpts, - {doc_root, filename:dirname(Source)}), - Set = referenced_dtls1([Source], DtlOpts1, - sets:add_element(Source, sets:new())), - sets:to_list(sets:del_element(Source, Set)). - -referenced_dtls1(Step, DtlOpts, Seen) -> - ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.", - [{return, list}]), - - ShOpts = [{use_stdout, false}, return_on_error], - AllRefs = - lists:append( - [begin - Cmd = lists:flatten(["grep -o [^\\\"]*\\", - ExtMatch, "[^\\\"]* ", F]), - case rebar_utils:sh(Cmd, ShOpts) of - {ok, Res} -> - string:tokens(Res, "\n"); - {error, _} -> - "" - end - end || F <- Step]), - DocRoot = option(doc_root, DtlOpts), - WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ], - ?DEBUG("All deps: ~p\n", [WithPaths]), - Existing = [F || F <- WithPaths, filelib:is_regular(F)], - New = sets:subtract(sets:from_list(Existing), Seen), - case sets:size(New) of - 0 -> Seen; - _ -> referenced_dtls1(sets:to_list(New), DtlOpts, - sets:union(New, Seen)) - end. diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 746e440..0a9a07c 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -29,14 +29,10 @@ -export([new/4, list_templates/1]). -%% API for other utilities that need templating functionality --export([resolve_variables/2, - render/2]). -include("rebar.hrl"). -define(TEMPLATE_RE, "^[^._].*\\.template\$"). --define(ERLYDTL_COMPILE_OPTS, [report_warnings, return_errors, {auto_escape, false}, {out_dir, false}]). %% =================================================================== %% Public API @@ -45,7 +41,7 @@ %% Apply a template new(Template, Vars, Force, State) -> {AvailTemplates, Files} = find_templates(State), - ?DEBUG("Looking for ~p~n", [Template]), + ?DEBUG("Looking for ~p", [Template]), case lists:keyfind(Template, 1, AvailTemplates) of false -> {not_found, Template}; TemplateTup -> create(TemplateTup, Files, Vars, Force, State) @@ -57,34 +53,6 @@ list_templates(State) -> [list_template(Files, Template, State) || Template <- AvailTemplates]. %% =================================================================== -%% Rendering API / legacy? -%% =================================================================== - -%% 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(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 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 erlydtl and the specified 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 %% =================================================================== @@ -191,7 +159,7 @@ create({Template, Type, File}, Files, UserVars, Force, State) -> %% Run template instructions one at a time. execute_template([], _, {Template,_,_}, _, _) -> - ?DEBUG("Template ~s applied~n", [Template]), + ?DEBUG("Template ~s applied", [Template]), ok; %% We can't execute the description execute_template([{description, _} | Terms], Files, Template, Vars, Force) -> @@ -201,13 +169,13 @@ execute_template([{variables, _} | Terms], Files, Template, Vars, Force) -> execute_template(Terms, Files, Template, Vars, Force); %% Create a directory execute_template([{dir, Path} | Terms], Files, Template, Vars, Force) -> - ?DEBUG("Creating directory ~p~n", [Path]), + ?DEBUG("Creating directory ~p", [Path]), case ec_file:mkdir_p(expand_path(Path, Vars)) of ok -> ok; {error, Reason} -> ?ABORT("Failed while processing template instruction " - "{dir, ~p}: ~p~n", [Path, Reason]) + "{dir, ~p}: ~p", [Path, Reason]) end, execute_template(Terms, Files, Template, Vars, Force); %% Change permissions on a file @@ -218,29 +186,28 @@ execute_template([{chmod, File, Perm} | Terms], Files, Template, Vars, Force) -> execute_template(Terms, Files, Template, Vars, Force); {error, Reason} -> ?ABORT("Failed while processing template instruction " - "{chmod, ~.8#, ~p}: ~p~n", [Perm, File, Reason]) + "{chmod, ~.8#, ~p}: ~p", [Perm, File, Reason]) end; %% Create a raw untemplated file execute_template([{file, From, To} | Terms], Files, {Template, Type, Cwd}, Vars, Force) -> - ?DEBUG("Creating file ~p~n", [To]), + ?DEBUG("Creating file ~p", [To]), Data = load_file(Files, Type, filename:join(Cwd, From)), Out = expand_path(To,Vars), case write_file(Out, Data, Force) of ok -> ok; - {error, exists} -> ?INFO("File ~p already exists.~n", [Out]) + {error, exists} -> ?INFO("File ~p already exists.", [Out]) end, execute_template(Terms, Files, {Template, Type, Cwd}, Vars, Force); %% Operate on a django template execute_template([{template, From, To} | Terms], Files, {Template, Type, Cwd}, Vars, Force) -> - ?DEBUG("Executing template file ~p~n", [From]), + ?DEBUG("Executing template file ~p", [From]), Out = expand_path(To, Vars), Tpl = load_file(Files, Type, filename:join(Cwd, From)), - TplName = make_template_name("rebar_template", Out), - {ok, Mod} = erlydtl:compile_template(Tpl, TplName, ?ERLYDTL_COMPILE_OPTS), - {ok, Output} = Mod:render(Vars), - case write_file(Out, Output, Force) of - ok -> ok; - {error, exists} -> ?INFO("File ~p already exists~n", [Out]) + case write_file(Out, render(Tpl, Vars), Force) of + ok -> + ok; + {error, exists} -> + ?INFO("File ~p already exists", [Out]) end, execute_template(Terms, Files, {Template, Type, Cwd}, Vars, Force); %% Unknown @@ -342,11 +309,11 @@ prioritize_templates([{Name, Type, File} | Rest], Valid) -> prioritize_templates(Rest, [{Name, Type, File} | Valid]); {_, escript, _} -> ?DEBUG("Skipping template ~p, due to presence of a built-in " - "template with the same name~n", [Name]), + "template with the same name", [Name]), prioritize_templates(Rest, Valid); {_, file, _} -> ?DEBUG("Skipping template ~p, due to presence of a custom " - "template at ~s~n", [Name, File]), + "template at ~s", [Name, File]), prioritize_templates(Rest, Valid) end. @@ -409,12 +376,12 @@ write_file(Output, Data, Force) -> {error, exists} end. --spec make_template_name(string(), term()) -> module(). -make_template_name(Base, Value) -> - %% Seed so we get different values each time - random:seed(os:timestamp()), - Hash = erlang:phash2(Value), - Ran = random:uniform(10000000), - erlang:list_to_atom(Base ++ "_" ++ - erlang:integer_to_list(Hash) ++ - "_" ++ erlang:integer_to_list(Ran)). +%% +%% 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), + rebar_mustache:render(Str1, dict:from_list(Context)). diff --git a/test/rebar_erlydtl_SUITE.erl b/test/rebar_erlydtl_SUITE.erl deleted file mode 100644 index c9054fd..0000000 --- a/test/rebar_erlydtl_SUITE.erl +++ /dev/null @@ -1,72 +0,0 @@ -%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- -%% ex: ts=4 sw=4 et --module(rebar_erlydtl_SUITE). - --export([suite/0, - init_per_suite/1, - end_per_suite/1, - init_per_testcase/2, - end_per_testcase/2, - all/0, - compile/1]). - --include_lib("common_test/include/ct.hrl"). --include_lib("eunit/include/eunit.hrl"). --include_lib("kernel/include/file.hrl"). - -%% =================================================================== -%% common_test callbacks -%% =================================================================== - -suite() -> - []. - -init_per_suite(Config) -> - Config. - -end_per_suite(_Config) -> - ok. - - -init_per_testcase(_, Config) -> - UpdConfig = rebar_test_utils:init_rebar_state(Config), - AppDir = ?config(apps, UpdConfig), - - Name = rebar_test_utils:create_random_name("erlydtlapp_"), - Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), - - write_dtl_file(AppDir, Name), - - RebarConfig = [{erl_opts, [debug_info]}, - {erlydtl_opts, []}], - [{app_name, Name}, - {rebar_config, RebarConfig} | UpdConfig]. - -end_per_testcase(_, _Config) -> - ok. - -all() -> - [compile]. - -compile(Config) -> - AppDir = ?config(apps, Config), - AppName = ?config(app_name, Config), - RebarConfig = ?config(rebar_config, Config), - Beam = beam_file(AppDir, AppName), - rebar_test_utils:run_and_check( - Config, RebarConfig, ["erlydtl", "compile"], - {ok, [{file, Beam}]} - ). - -beam_file(AppDir, AppName) -> - filename:join([AppDir, "_build", "default", "lib", - AppName, "ebin", AppName++"_template_dtl.beam"]). - -write_dtl_file(Dir, AppName) -> - Erl = filename:join([Dir, "priv", "templates", AppName++"_template.dtl"]), - ok = filelib:ensure_dir(Erl), - ok = ec_file:write(Erl, get_body()). - -get_body() -> - ["[]"]. -- cgit v1.1