Browse Source

Add option DTL_PREFIX for erlydtl-produced module names

Loïc Hoguin 6 years ago
parent
commit
3803446bcd
2 changed files with 28 additions and 2 deletions
  1. 3 2
      plugins/erlydtl.mk
  2. 25 0
      test/plugin_erlydtl.mk

+ 3 - 2
plugins/erlydtl.mk

@@ -5,6 +5,7 @@
 
 DTL_FULL_PATH ?=
 DTL_PATH ?= templates/
+DTL_PREFIX ?=
 DTL_SUFFIX ?= _dtl
 DTL_OPTS ?=
 
@@ -20,7 +21,7 @@ DTL_FILES := $(sort $(call core_find,$(DTL_PATH),*.dtl))
 
 ifneq ($(DTL_FILES),)
 
-DTL_NAMES   = $(addsuffix $(DTL_SUFFIX),$(DTL_FILES:$(DTL_PATH)/%.dtl=%))
+DTL_NAMES   = $(addprefix $(DTL_PREFIX),$(addsuffix $(DTL_SUFFIX),$(DTL_FILES:$(DTL_PATH)/%.dtl=%)))
 DTL_MODULES = $(if $(DTL_FULL_PATH),$(subst /,_,$(DTL_NAMES)),$(notdir $(DTL_NAMES)))
 BEAM_FILES += $(addsuffix .beam,$(addprefix ebin/,$(DTL_MODULES)))
 
@@ -45,7 +46,7 @@ define erlydtl_compile.erl
 				"$(DTL_PATH)/" ++ F2 = filename:rootname(F, ".dtl"),
 				re:replace(F2, "/",  "_",  [{return, list}, global])
 		end,
-		Module = list_to_atom(string:to_lower(Module0) ++ "$(DTL_SUFFIX)"),
+		Module = list_to_atom("$(DTL_PREFIX)" ++ string:to_lower(Module0) ++ "$(DTL_SUFFIX)"),
 		case erlydtl:compile(F, Module, [$(DTL_OPTS)] ++ [{out_dir, "ebin/"}, return_errors]) of
 			ok -> ok;
 			{ok, _} -> ok

+ 25 - 0
test/plugin_erlydtl.mk

@@ -198,6 +198,31 @@ erlydtl-path-full-path-suffix: build clean
 		{ok, [one_suffix, two_three_suffix]} = application:get_key($(APP), modules), \
 		halt()"
 
+erlydtl-prefix: build clean
+
+	$i "Bootstrap a new OTP library named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$i "Generate ErlyDTL templates"
+	$t mkdir $(APP)/templates/
+	$t touch $(APP)/templates/one.dtl
+	$t touch $(APP)/templates/two.dtl
+
+	$i "Build the application"
+	$t $(MAKE) -C $(APP) DEPS=erlydtl DTL_PREFIX=number_ $v
+
+	$i "Check that ErlyDTL templates are compiled"
+	$t test -f $(APP)/ebin/number_one_dtl.beam
+	$t test -f $(APP)/ebin/number_two_dtl.beam
+
+	$i "Check that ErlyDTL generated modules are included in .app file"
+	$t $(ERL) -pa $(APP)/ebin/ -eval " \
+		ok = application:load($(APP)), \
+		{ok, [number_one_dtl, number_two_dtl]} = application:get_key($(APP), modules), \
+		halt()"
+
 erlydtl-suffix: build clean
 
 	$i "Bootstrap a new OTP library named $(APP)"