git: b242400ea886 - main - Tools/scripts/update-rust-port.sh: update, make more robust

From: Yuri Victorovich <yuri_at_FreeBSD.org>
Date: Sat, 10 Oct 2026 05:29:19 UTC
The branch main has been updated by yuri:

URL: https://cgit.FreeBSD.org/ports/commit/?id=b242400ea8861214a9130b523e725ac029947558

commit b242400ea8861214a9130b523e725ac029947558
Author:     Yuri Victorovich <yuri@FreeBSD.org>
AuthorDate: 2026-10-10 04:37:03 +0000
Commit:     Yuri Victorovich <yuri@FreeBSD.org>
CommitDate: 2026-10-10 05:29:16 +0000

    Tools/scripts/update-rust-port.sh: update, make more robust
---
 Tools/scripts/update-rust-port.sh | 106 +++++++++++++++++++++++++++-----------
 1 file changed, 77 insertions(+), 29 deletions(-)

diff --git a/Tools/scripts/update-rust-port.sh b/Tools/scripts/update-rust-port.sh
index 57d8f0641f9c..feb32352c064 100755
--- a/Tools/scripts/update-rust-port.sh
+++ b/Tools/scripts/update-rust-port.sh
@@ -2,8 +2,6 @@
 #
 # MAINTAINER: yuri@FreeBSD.org
 
-# CAVEAT: ports with Makefile.crates are not yet supported
-
 ## args
 
 VERSION="$1"
@@ -25,26 +23,46 @@ if [ -z "$VERSION" ]; then
 	echo "Usage: $0 <new-version>"
 	exit 1
 fi
-if ! [ -f Makefile ] || ! [ -f pkg-descr ] || ! grep -q "CARGO_CRATES=" Makefile; then
-	echo "$0 should be run in a Rust-based port directory"
+if ! [ -f Makefile ] || ! [ -f pkg-descr ]; then
+	echo "$0 should be run in a port directory" >&2
 	exit 1
 fi
 
-## MAIN
+USE_CRATES_FILE=false
+if [ -f Makefile.crates ]; then
+	USE_CRATES_FILE=true
+elif ! grep -q "CARGO_CRATES=" Makefile; then
+	echo "$0 should be run in a Rust-based port directory (no CARGO_CRATES found)" >&2
+	exit 1
+fi
 
-# copy Makefile
-cp Makefile Makefile.new
+## helpers
+
+substitute_version() {
+	# Use awk so version strings containing '/' (e.g. GitHub tag prefixes)
+	# do not break the substitution.
+	awk -v ver="$VERSION" '
+		/^((PORT|DIST)VERSION=[\t ]*)/ {
+			prefix = $0
+			sub(/[\t ]*[^\t ]*$/, "", prefix)
+			print prefix "\t" ver
+			next
+		}
+		{ print }
+	' "$1" > "$1.tmp" && mv "$1.tmp" "$1"
+}
 
-# substitute version tag PORTVERSION or DISTVERSION
-sed -i '' -E "s/^(PORT|DIST)(VERSION=[\t ]*)[0-9.-]+/\1\2${VERSION}/" Makefile.new
+reset_portrevision() {
+	if grep -q "^PORTREVISION=" Makefile; then
+		echo PORTREVISION=0 | portedit merge -i Makefile
+	fi
+}
 
-# reset PORTREVISION if present
-if grep -q "PORTREVISION=" Makefile; then
-	echo PORTREVISION=0 | portedit merge -i Makefile.new
-fi
+update_inline_crates() {
+	cp Makefile Makefile.new
+	substitute_version Makefile.new
 
-# replace CARGO_CRATES with a placeholder
-/usr/bin/awk '
+	/usr/bin/awk '
 BEGIN {
 	in_cargo_crates = 0
 }
@@ -66,21 +84,51 @@ BEGIN {
 	print $0
 }' < Makefile.new > Makefile
 
-# update distinfo
-make makesum
+	BATCH=yes make makesum
 
-# replace the placeholder
-while IFS= read -r line; do
-	if [ "$line" = "#@@@PLACEHOLDER@@@" ]; then
-		make cargo-crates | grep -v '^='
-	else
-		echo "$line"
-	fi
-done < Makefile > Makefile.new &&
-mv Makefile.new Makefile
+	while IFS= read -r line; do
+		if [ "$line" = "#@@@PLACEHOLDER@@@" ]; then
+			BATCH=yes make cargo-crates | grep -v '^='
+		else
+			echo "$line"
+		fi
+	done < Makefile > Makefile.new &&
+	mv Makefile.new Makefile
+}
+
+update_crates_file() {
+	# Keep the include target valid while we fetch the source distfile.
+	: > Makefile.crates
+
+	substitute_version Makefile
+
+	BATCH=yes make makesum
+
+	BATCH=yes make cargo-crates | grep -v '^=' > Makefile.crates
+}
+
+## MAIN
+
+reset_portrevision
+
+# Make sure a crates-file include target is parseable before we ask make to
+# clean up stale work from a previous version.
+if $USE_CRATES_FILE; then
+	: > Makefile.crates
+fi
+
+# Start from a clean work directory so stale extractions do not confuse
+# cargo-crates after the version bump.
+BATCH=yes make clean || true
+
+if $USE_CRATES_FILE; then
+	update_crates_file
+else
+	update_inline_crates
+fi
 
 # clean
-make clean
+BATCH=yes make clean
 
-# update distinfo
-make makesum
+# update distinfo with the real crate list
+BATCH=yes make makesum