From a6f9a450af99a7a95848dde671de658bb53a43dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 21 Nov 2024 15:55:37 +0100 Subject: Move templates outside the source .mk files Templates now no longer use Make variables for substitution but instead replace strings with their equivalent: template_name: Corresponds to n=template_name project_name: Corresponds to $(PROJECT) or in=project_name This allows defining templates outside of Makefiles. For example an external plugin could define their templates in templates/my_template.erl and then have the following in the included Makefile: tpl_my_template = $(file < $(THIS)/templates/my_template.erl) By default the created file will be in src/template_name.erl. This can be overriden with the tplp_* variable: tplp_my_template = src/model/my_template.erl Substitution is applied both to the template contents and to its path. In addition, attempting to overwrite an existing file when creating a template will result in failure. --- test/core_plugins.mk | 39 ++++++++++++++++++++++++++++++++++++++- test/core_upgrade.mk | 2 -- test/plugin_bootstrap.mk | 18 +++++++++++++----- 3 files changed, 51 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/core_plugins.mk b/test/core_plugins.mk index dcffb9a..8544945 100644 --- a/test/core_plugins.mk +++ b/test/core_plugins.mk @@ -194,7 +194,7 @@ core-plugins-templates: init $t mkdir -p $(APP)/plugin_dep $t printf "%s\n" \ "define tpl_test_mk" \ - "-module(test_mk)." \ + "-module(template_name)." \ "endef" > $(APP)/plugin_dep/plugins.mk $t cd $(APP)/plugin_dep && \ git init -q && \ @@ -263,6 +263,43 @@ core-plugins-templates-apps-only: init $i "Check that the file was compiled correctly" $t test -f $(APP)/apps/my_app/ebin/test_mk.beam +core-plugins-templates-file: init + + $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 "Create a local git repository with a plugin containing a template file" + $t mkdir -p $(APP)/plugin_dep/templates + $t printf "%s\n" "-module(template_name)." > $(APP)/plugin_dep/templates/test_mk.erl + $t echo "THIS := \$$(dir \$$(realpath \$$(lastword \$$(MAKEFILE_LIST))))" > $(APP)/plugin_dep/plugins.mk + $t printf "%s\n" "tpl_test_mk = \$$(file < \$$(THIS)/templates/test_mk.erl)" >> $(APP)/plugin_dep/plugins.mk + $t cd $(APP)/plugin_dep && \ + git init -q && \ + git config user.email "testsuite@erlang.mk" && \ + git config user.name "test suite" && \ + git add . && \ + git commit -q --no-gpg-sign -m "Tests" + + $i "Add dependency and plugins to the Makefile" + $t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = plugin_dep\ndep_plugin_dep = git file://$(abspath $(APP)/plugin_dep) master\nDEP_PLUGINS = plugin_dep\n"}' $(APP)/Makefile + + $i "Run 'make list-templates' and check that it prints test_mk" + $t $(MAKE) --no-print-directory -C $(APP) list-templates | grep -qw test_mk + + $i "Create a new file using the template" + $t $(MAKE) --no-print-directory -C $(APP) new t=test_mk n=test_mk $v + + $i "Confirm the file exists" + $t test -f $(APP)/src/test_mk.erl + + $i "Build the application" + $t $(MAKE) -C $(APP) $v + + $i "Check that the file was compiled correctly" + $t test -f $(APP)/ebin/test_mk.beam + core-plugins-test: init $i "Bootstrap a new OTP library named $(APP)" diff --git a/test/core_upgrade.mk b/test/core_upgrade.mk index f165098..c9fea3d 100644 --- a/test/core_upgrade.mk +++ b/test/core_upgrade.mk @@ -16,8 +16,6 @@ core-upgrade-changelog: init $i "Fork erlang.mk locally and set a test CHANGELOG.asciidoc" $t git clone -q https://github.com/ninenines/erlang.mk $(APP)/alt-erlangmk-repo $t echo "$(APP)$(APP)" > $(APP)/alt-erlangmk-repo/CHANGELOG.asciidoc -# Since part of this functionality needs the main Makefile, copy it. - $t cp ../Makefile $(APP)/alt-erlangmk-repo/ $t (cd $(APP)/alt-erlangmk-repo && \ git config user.email "testsuite@erlang.mk" && \ git config user.name "test suite" && \ diff --git a/test/plugin_bootstrap.mk b/test/plugin_bootstrap.mk index ac10ca0..aa873a8 100644 --- a/test/plugin_bootstrap.mk +++ b/test/plugin_bootstrap.mk @@ -303,13 +303,16 @@ bootstrap-templates: init $t $(MAKE) -C $(APP) --no-print-directory new t=gen_statem n=my_statem $t $(MAKE) -C $(APP) --no-print-directory new t=gen_server n=my_server $t $(MAKE) -C $(APP) --no-print-directory new t=supervisor n=my_sup - $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_http n=my_http - $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_loop n=my_loop - $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_rest n=my_rest - $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_ws n=my_ws + $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_http_h n=my_http_h + $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_loop_h n=my_loop_h + $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_rest_h n=my_rest_h + $t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_websocket_h n=my_ws_h $t $(MAKE) -C $(APP) --no-print-directory new t=ranch_protocol n=my_protocol $t $(MAKE) -C $(APP) --no-print-directory new t=module n=my_module + $i "Confirm we can't overwrite existing files" + $t ! $(MAKE) -C $(APP) --no-print-directory new t=gen_server n=my_server $v + # Here we disable warnings because templates contain missing behaviors. $i "Build the application" $t $(MAKE) -C $(APP) ERLC_OPTS=+debug_info $v @@ -320,12 +323,17 @@ bootstrap-templates: init $t test -f $(APP)/ebin/my_statem.beam $t test -f $(APP)/ebin/my_server.beam $t test -f $(APP)/ebin/my_sup.beam + $t test -f $(APP)/ebin/my_http_h.beam + $t test -f $(APP)/ebin/my_loop_h.beam + $t test -f $(APP)/ebin/my_rest_h.beam + $t test -f $(APP)/ebin/my_ws_h.beam + $t test -f $(APP)/ebin/my_protocol.beam $t test -f $(APP)/ebin/my_module.beam $i "Check that all the modules can be loaded" $t $(ERL) -pa $(APP)/ebin/ -eval " \ ok = application:start($(APP)), \ - {ok, Mods = [my_fsm, my_http, my_loop, my_module, my_protocol, my_rest, my_server, my_statem, my_sup, my_ws]} \ + {ok, Mods = [my_fsm, my_http_h, my_loop_h, my_module, my_protocol, my_rest_h, my_server, my_statem, my_sup, my_ws_h]} \ = application:get_key($(APP), modules), \ [{module, M} = code:load_file(M) || M <- Mods], \ halt()" -- cgit v1.2.3