From e9331856f9b0fab47ad10104721472262a501af9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= <essen@ninenines.eu>
Date: Tue, 11 Feb 2025 15:34:58 +0100
Subject: Add support for specifying Hex requirements for dependencies

This allows overriding the default which is to use the same
explicit version as the dep_* line, and instead specify
ranges of versions.
---
 plugins/hex.mk     |  2 +-
 test/plugin_hex.mk | 30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/plugins/hex.mk b/plugins/hex.mk
index 44beb65..482a56a 100644
--- a/plugins/hex.mk
+++ b/plugins/hex.mk
@@ -91,7 +91,7 @@ define hex_tarball_create.erl
 			<<"$(if $(subst hex,,$(call query_fetch_method,$d)),$d,$(if $(word 3,$(dep_$d)),$(word 3,$(dep_$d)),$d))">> => #{
 				<<"app">> => <<"$d">>,
 				<<"optional">> => false,
-				<<"requirement">> => <<"$(call query_version,$d)">>
+				<<"requirement">> => <<"$(if $(hex_req_$d),$(strip $(hex_req_$d)),$(call query_version,$d))">>
 			},)
 		$(if $(DEPS),dummy => dummy)
 	},
diff --git a/test/plugin_hex.mk b/test/plugin_hex.mk
index 99e6356..27060b6 100644
--- a/test/plugin_hex.mk
+++ b/test/plugin_hex.mk
@@ -162,6 +162,36 @@ endif
 		{_, <<\"2.13.0\">>} = lists:keyfind(<<\"requirement\">>, 1, Cowlib), \
 		halt(0)"
 
+hex-tarball-create-with-req: init
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
+
+	$i "Add Cowlib to the list of dependencies"
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = cowlib\ndep_cowlib_commit = 2.13.0\nhex_req_cowlib = ~> 2.13\n"}' $(APP)/Makefile
+
+ifdef LEGACY
+	$i "Add Cowlib to the applications key in the .app.src file"
+	$t perl -ni.bak -e 'print;if ($$.==7) {print "\t\tcowlib,\n"}' $(APP)/src/$(APP).app.src
+endif
+
+	$i "Create a release tarball"
+	$t $(MAKE) -C $(APP) hex-tarball-create $v
+
+	$i "Confirm the tarball contents can be extracted"
+	$t cd $(APP)/.erlang.mk/ && tar xf $(APP).tar
+
+	$i "Confirm the tarball contains a metadata.config file that lists Cowlib as requirement"
+	$t $(ERL) -eval " \
+		{ok, Metadata} = file:consult(\"$(APP)/.erlang.mk/metadata.config\"), \
+		{_, [{<<\"cowlib\">>, Cowlib}]} = lists:keyfind(<<\"requirements\">>, 1, Metadata), \
+		{_, <<\"cowlib\">>} = lists:keyfind(<<\"app\">>, 1, Cowlib), \
+		{_, false} = lists:keyfind(<<\"optional\">>, 1, Cowlib), \
+		{_, <<\"~> 2.13\">>} = lists:keyfind(<<\"requirement\">>, 1, Cowlib), \
+		halt(0)"
+
 hex-release-publish: init
 
 	$i "Bootstrap a new OTP application named $(APP)"
-- 
cgit v1.2.3