git: 6180c0b094c8 - main - Uses/cargo: Add a cargo-crates-merge maintainer target

From: Tobias Kortkamp <tobik_at_FreeBSD.org>
Date: Tue, 26 Oct 2021 14:47:13 UTC
The branch main has been updated by tobik:

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

commit 6180c0b094c839fb16fcd95e3c53f3ca79544f9c
Author:     Tobias Kortkamp <tobik@FreeBSD.org>
AuthorDate: 2021-10-26 11:01:27 +0000
Commit:     Tobias Kortkamp <tobik@FreeBSD.org>
CommitDate: 2021-10-26 14:46:00 +0000

    Uses/cargo: Add a cargo-crates-merge maintainer target
    
    It is the in-place version of cargo-crates and updates CARGO_CRATES
    in the port's Makefile for you via portedit from ports-mgmt/portfmt.
---
 Mk/Uses/cargo.mk | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/Mk/Uses/cargo.mk b/Mk/Uses/cargo.mk
index e1f5544b9c0b..76b44f3f2ba3 100644
--- a/Mk/Uses/cargo.mk
+++ b/Mk/Uses/cargo.mk
@@ -347,14 +347,18 @@ do-test:
 
 # cargo-crates will output the crates list from Cargo.lock.  If there
 # is no Cargo.lock for some reason, try and generate it first.
-cargo-crates: extract
+cargo-crates: cargo-crates-generate-lockfile
+	@${_CARGO_AWK} ${SCRIPTSDIR}/cargo-crates.awk ${CARGO_CARGOLOCK}
+
+# cargo-crates-generate-lockfile will try to generate a Cargo.lock file
+# if it does not exist.
+cargo-crates-generate-lockfile: extract
 	@if [ ! -r "${CARGO_CARGOLOCK}" ]; then \
 		${ECHO_MSG} "===> ${CARGO_CARGOLOCK} not found.  Trying to generate it..."; \
 		cd ${WRKSRC}; ${_CARGO_RUN} generate-lockfile \
 			--manifest-path ${CARGO_CARGOTOML} \
 			--verbose; \
 	fi
-	@${_CARGO_AWK} ${SCRIPTSDIR}/cargo-crates.awk ${CARGO_CARGOLOCK}
 
 # cargo-crates-licenses will try to grab license information from
 # all downloaded crates.
@@ -365,4 +369,17 @@ cargo-crates-licenses: configure
 		-e 's@^${CARGO_VENDOR_DIR}/@@' \
 		-e 's@/Cargo.toml:license.*= *"@|@' \
 		-e 's@"$$@@g' | sort | /usr/bin/column -t -s '|'
+
+# cargo-crates-merge will in-place update CARGO_CRATES in the port
+# based on the crates list from Cargo.lock.  If there is no Cargo.lock
+# for some reason, try and generate it first.
+cargo-crates-merge: cargo-crates-generate-lockfile
+	@if ! type portedit > /dev/null 2>&1; then \
+		${ECHO_MSG} "===> Please install \"ports-mgmt/portfmt\""; exit 1; \
+	fi
+	@f="${MASTERDIR}/Makefile"; [ -r "${MASTERDIR}/Makefile.crates" ] && f="${MASTERDIR}/Makefile.crates"; \
+		${_CARGO_AWK} ${SCRIPTSDIR}/cargo-crates.awk ${CARGO_CARGOLOCK} | \
+			portedit merge -i $$f; \
+		${ECHO_MSG} "CARGO_CRATES in $$f was updated"
+
 .endif