erlang.mk icon indicating copy to clipboard operation
erlang.mk copied to clipboard

relx.config.script isn't evaluated since relx v4

Open artman41 opened this issue 2 years ago • 2 comments

Our projects depend on the relx.config.script being evaluated alongside the relx.config

Looks like the new relx plugin only checks for & evaluates the relx.config.

using the following changes, I was able to replicate the old functionality that relx provided

diff --git a/erlang.mk b/erlang.mk
index acbd80d..ab32ec9 100644
--- a/erlang.mk
+++ b/erlang.mk
@@ -7473,6 +7473,7 @@ ifeq ($(filter relx,$(BUILD_DEPS) $(DEPS) $(REL_DEPS)),relx)
 # Configuration.
 
 RELX_CONFIG ?= $(CURDIR)/relx.config
+RELX_CONFIG_SCRIPT ?= $(CURDIR)/relx.config.script
 
 RELX_OUTPUT_DIR ?= _rel
 RELX_REL_EXT ?=
@@ -7485,7 +7486,7 @@ endif
 # Core targets.
 
 ifeq ($(IS_DEP),)
-ifneq ($(wildcard $(RELX_CONFIG)),)
+ifneq ($(wildcard $(RELX_CONFIG))$(wildcard $(RELX_CONFIG_SCRIPT)),)
 rel:: relx-rel
 
 relup:: relx-relup
@@ -7497,41 +7498,86 @@ distclean:: distclean-relx-rel
 # Plugin-specific targets.
 
 define relx_release.erl
-	{ok, Config} = file:consult("$(call core_native_path,$(RELX_CONFIG))"),
-	{release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config),
+	Config0 = 
+		case file:consult("$(call core_native_path,$(RELX_CONFIG))") of
+			{ok, Terms} ->
+				Terms;
+			{error, _} ->
+				[]
+		end,
+	Config1 =
+		case filelib:is_file("$(call core_native_path,$(RELX_CONFIG_SCRIPT))") of
+			true ->
+				Bindings = erl_eval:add_binding('CONFIG', Config0, erl_eval:new_bindings()),
+				{ok, Config_} = file:script("$(call core_native_path,$(RELX_CONFIG_SCRIPT))", Bindings),
+				Config_;
+			false ->
+				Config0
+		end,
+	{release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config1),
 	Vsn = case Vsn0 of
 		{cmd, Cmd} -> os:cmd(Cmd);
 		semver -> "";
 		{semver, _} -> "";
 		VsnStr -> Vsn0
 	end,
-	{ok, _} = relx:build_release(#{name => Name, vsn => Vsn}, Config),
+	{ok, _} = relx:build_release(#{name => Name, vsn => Vsn}, Config1),
 	halt(0).
 endef
 
 define relx_tar.erl
-	{ok, Config} = file:consult("$(call core_native_path,$(RELX_CONFIG))"),
-	{release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config),
+	Config0 = 
+		case file:consult("$(call core_native_path,$(RELX_CONFIG))") of
+			{ok, Terms} ->
+				Terms;
+			{error, _} ->
+				[]
+		end,
+	Config1 =
+		case filelib:is_file("$(call core_native_path,$(RELX_CONFIG_SCRIPT))") of
+			true ->
+				Bindings = erl_eval:add_binding('CONFIG', Config0, erl_eval:new_bindings()),
+				{ok, Config_} = file:script("$(call core_native_path,$(RELX_CONFIG_SCRIPT))", Bindings),
+				Config_;
+			false ->
+				Config0
+		end,
+	{release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config1),
 	Vsn = case Vsn0 of
 		{cmd, Cmd} -> os:cmd(Cmd);
 		semver -> "";
 		{semver, _} -> "";
 		VsnStr -> Vsn0
 	end,
-	{ok, _} = relx:build_tar(#{name => Name, vsn => Vsn}, Config),
+	{ok, _} = relx:build_tar(#{name => Name, vsn => Vsn}, Config1),
 	halt(0).
 endef
 
 define relx_relup.erl
-	{ok, Config} = file:consult("$(call core_native_path,$(RELX_CONFIG))"),
-	{release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config),
+	Config0 = 
+		case file:consult("$(call core_native_path,$(RELX_CONFIG))") of
+			{ok, Terms} ->
+				Terms;
+			{error, _} ->
+				[]
+		end,
+	Config1 =
+		case filelib:is_file("$(call core_native_path,$(RELX_CONFIG_SCRIPT))") of
+			true ->
+				Bindings = erl_eval:add_binding('CONFIG', Config0, erl_eval:new_bindings()),
+				{ok, Config_} = file:script("$(call core_native_path,$(RELX_CONFIG_SCRIPT))", Bindings),
+				Config_;
+			false ->
+				Config0
+		end,
+	{release, {Name, Vsn0}, _} = lists:keyfind(release, 1, Config1),
 	Vsn = case Vsn0 of
 		{cmd, Cmd} -> os:cmd(Cmd);
 		semver -> "";
 		{semver, _} -> "";
 		VsnStr -> Vsn0
 	end,
-	{ok, _} = relx:build_relup(Name, Vsn, undefined, Config ++ [{output_dir, "$(RELX_OUTPUT_DIR)"}]),
+	{ok, _} = relx:build_relup(Name, Vsn, undefined, Config1 ++ [{output_dir, "$(RELX_OUTPUT_DIR)"}]),
 	halt(0).
 endef

artman41 avatar Aug 05 '22 16:08 artman41

Oh I didn't realize this was dropped from upstream relx, sorry. Thanks for the patch I will try to cook something permanent when I can (possibly middle of August we'll see).

essen avatar Aug 05 '22 21:08 essen

@essen I've put together an MR for you - if I've done any of it wrong please say 😃

artman41 avatar Aug 20 '22 16:08 artman41

I've run into this as well, can #960 be merged?

philipcristiano avatar Dec 20 '22 11:12 philipcristiano

Done. Thanks!

essen avatar May 12 '23 13:05 essen