public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-dev] cargo.eclass: Add support for GIT_CRATES
@ 2023-02-07 23:25 Patrick McLean
  2023-02-07 23:25 ` [gentoo-dev] [PATCH] " Patrick McLean
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick McLean @ 2023-02-07 23:25 UTC (permalink / raw
  To: gentoo-dev; +Cc: rust, gyakovlev

This adds support for an associative array called GIT_CRATES containing any
crates that must be fetched from git rather than just cargo crates. This will
add the code to the cargo config to make sure that the locations are overridden
so cargo won't try to fetch the crates from git.

Currently it automatically handle GitHub and GitLab URIs, and will accept
arbitrary URIs.





^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gentoo-dev] [PATCH] cargo.eclass: Add support for GIT_CRATES
  2023-02-07 23:25 [gentoo-dev] cargo.eclass: Add support for GIT_CRATES Patrick McLean
@ 2023-02-07 23:25 ` Patrick McLean
  0 siblings, 0 replies; 2+ messages in thread
From: Patrick McLean @ 2023-02-07 23:25 UTC (permalink / raw
  To: gentoo-dev; +Cc: rust, gyakovlev, Patrick McLean

This adds support for an associative array called GIT_CRATES containing any
crates that must be fetched from git rather than just cargo crates. This will
add the code to the cargo config to make sure that the locations are overridden
so cargo won't try to fetch the crates from git.

Currently it automatically handle GitHub and GitLab URIs, and will accept
arbitrary URIs.

Tested-By: Georgy Yakovlev <gyakovlev@gentoo.org>
Signed-off-by: Patrick McLean <chutzpah@gentoo.org>
---
 eclass/cargo.eclass | 103 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 103 insertions(+)

diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index eb9d2e8c359..788663a070c 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -75,6 +75,43 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
 # SRC_URI="$(cargo_crate_uris)"
 # @CODE
 
+# @ECLASS_VARIABLE: GIT_CRATES
+# @DEFAULT_UNSET
+# @PRE_INHERIT
+# @DESCRIPTION:
+# bash associative array containing all crates that a package wants
+# to be fetch by git.
+# The key is the crate name, the value is a semicolon separated list of
+# the following fields:
+#
+# - the URI to to fetch the crate from
+#     - this intelligentally handles GitHub URIs and GitLab URIs so
+#       just the path is needed.
+#     - the string "%commit%" gets replaced with the commit
+# - the hash of the commit to use
+# - (optional) the path to look for Cargo.toml in
+#   - this will also replace  the string "%commit%" with the commit
+#   - if this not provided, it will be generated using the crate name and
+#     the commit
+# Used by cargo_crate_uris
+#
+# If this is defined, then cargo_src_install will add --frozen to "cargo install"
+#
+# Example of simple definition of GIT_CRATES without any paths defined
+# @CODE
+# declare -A GIT_CRATES=(
+# 	[home]="https://github.com/rbtcollins/home;a243ee2fbee6022c57d56f5aa79aefe194eabe53"
+# )
+# @CODE
+#
+# Example code of how to define GIT_CRATES with paths defined.
+# @CODE
+# declare -A GIT_CRATES=(
+# 	[rustpython-common]="https://github.com/RustPython/RustPython;4f38cb68e4a97aeea9eb19673803a0bd5f655383;RustPython-%commit%/common"
+# 	[rustpython-parser]="https://github.com/RustPython/RustPython;4f38cb68e4a97aeea9eb19673803a0bd5f655383;RustPython-%commit%/compiler/parser"
+# )
+# @CODE
+
 # @ECLASS_VARIABLE: CARGO_OPTIONAL
 # @DEFAULT_UNSET
 # @PRE_INHERIT
@@ -160,6 +197,37 @@ cargo_crate_uris() {
 		url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
 		echo "${url}"
 	done
+
+	local git_crates_type
+	git_crates_type="$(declare -p GIT_CRATES)"
+	if [[ ${git_crates_type} == "declare -A "* ]]; then
+		local crate commit crate_uri crate_dir repo_ext feat_expr
+
+		for crate in "${!GIT_CRATES[@]}"; do
+			IFS=';' read -r crate_uri commit crate_dir <<< "${GIT_CRATES[${crate}]}"
+
+			case "${crate_uri}" in
+				https://github.com/*)
+					repo_ext=".gh"
+					repo_name="${crate_uri##*/}"
+					crate_uri="${crate_uri%/}/archive/%commit%.tar.gz"
+				;;
+				https://gitlab.com/*)
+					repo_ext=".gl"
+					repo_name="${crate_uri##*/}"
+					crate_uri="${crate_uri%/}/archive/-/%commit%/${repo_name}/%commit%.tar.gz"
+				;;
+				*)
+					repo_ext=
+					repo_name="${crate}"
+				;;
+			esac
+
+			printf -- '%s -> %s\n' "${crate_uri//%commit%/${commit}}" "${repo_name}-${commit}${repo_ext}.tar.gz"
+		done
+	elif [[ -n ${git_crates_type} ]]; then
+		die "GIT_CRATE must be declared as an associative array"
+	fi
 }
 
 # @FUNCTION: cargo_gen_config
@@ -195,12 +263,46 @@ cargo_gen_config() {
 	[term]
 	verbose = true
 	$([[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'")
+	$(_cargo_gen_git_config)
 	_EOF_
 
 	export CARGO_HOME="${ECARGO_HOME}"
 	_CARGO_GEN_CONFIG_HAS_RUN=1
 }
 
+# @FUNCTION: _cargo_gen_git_config
+# @USAGE:
+# @INTERNAL
+# @DESCRIPTION:
+# Generate the cargo config for git crates, this will output the
+# configuration for cargo to override the cargo config so the local git crates
+# specified in GIT_CRATES will be used rather than attempting to fetch
+# from git.
+#
+# Called by cargo_gen_config when generating the config.
+_cargo_gen_git_config() {
+	local git_crates_type
+	git_crates_type="$(declare -p GIT_CRATES)"
+
+	if [[ ${git_crates_type} == "declare -A "* ]]; then
+		local crate commit crate_uri crate_dir
+		local -A crate_patches
+
+		for crate in "${!GIT_CRATES[@]}"; do
+			IFS=';' read -r crate_uri commit crate_dir <<< "${GIT_CRATES[${crate}]}"
+			: "${crate_dir:=${crate}-%commit%}"
+			crate_patches["${crate_uri}"]+="${crate} = { path = \"${WORKDIR}/${crate_dir//%commit%/${commit}}\" };;"
+		done
+
+		for crate_uri in "${!crate_patches[@]}"; do
+			printf -- "[patch.'%s']\\n%s\n" "${crate_uri}" "${crate_patches["${crate_uri}"]//;;/$'\n'}"
+		done
+
+	elif [[ -n ${git_crates_type} ]]; then
+		die "GIT_CRATE must be declared as an associative array"
+	fi
+}
+
 # @FUNCTION: cargo_src_unpack
 # @DESCRIPTION:
 # Unpacks the package and the cargo registry
@@ -412,6 +514,7 @@ cargo_src_install() {
 
 	set -- cargo install $(has --path ${@} || echo --path ./) \
 		--root "${ED}/usr" \
+		 ${GIT_CRATES:---frozen} \
 		$(usex debug --debug "") \
 		${ECARGO_ARGS[@]} "$@"
 	einfo "${@}"
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-07 23:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-07 23:25 [gentoo-dev] cargo.eclass: Add support for GIT_CRATES Patrick McLean
2023-02-07 23:25 ` [gentoo-dev] [PATCH] " Patrick McLean

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox