tools

various tools I have been using throughout the years
Log | Files | Refs | README | LICENSE

module-dep-check.sh (1319B)


      1 #!/bin/sh
      2 set -eu
      3 
      4 BCR_BASE_URL="https://bcr.bazel.build/modules"
      5 MODULE_FILE="MODULE.bazel"
      6 
      7 usage() {
      8 	cat <<EOF
      9 Usage: $(basename "$0") [OPTIONS]
     10 
     11 Check for outdated bazel_dep entries in MODULE.bazel.
     12 
     13 Options:
     14   -f FILE   Path to MODULE.bazel (default: MODULE.bazel)
     15   -h        Show this help message
     16 
     17 Exit codes:
     18   0   All dependencies are up to date
     19   1   One or more dependencies are outdated
     20   2   Usage error or missing file
     21 EOF
     22 }
     23 
     24 while getopts f:h opt; do
     25 	case "$opt" in
     26 	f) MODULE_FILE="$OPTARG" ;;
     27 	h)
     28 		usage
     29 		exit 0
     30 		;;
     31 	*)
     32 		usage
     33 		exit 2
     34 		;;
     35 	esac
     36 done
     37 
     38 if [ ! -f "$MODULE_FILE" ]; then
     39 	printf 'error: file not found: %s\n' "$MODULE_FILE" >&2
     40 	exit 2
     41 fi
     42 
     43 tmpdir=$(mktemp -d)
     44 trap 'rm -rf "$tmpdir"' EXIT
     45 
     46 deps=$(grep 'bazel_dep(' "$MODULE_FILE") || true
     47 [ -z "$deps" ] && exit 0
     48 
     49 printf '%s\n' "$deps" | sed -n 's/.*name *= *"\([^"]*\)".*version *= *"\([^"]*\)".*/\1 \2/p' | while read -r name cur; do
     50 	if [ -z "$name" ] || [ -z "$cur" ]; then
     51 		continue
     52 	fi
     53 	latest=$(curl -sf "${BCR_BASE_URL}/${name}/metadata.json" | jaq -r '.versions[-1]') || {
     54 		printf 'warn: failed to parse metadata for %s\n' "$name" >&2
     55 		continue
     56 	}
     57 	if [ "$latest" != "$cur" ]; then
     58 		printf '%s: %s -> %s\n' "$name" "$cur" "$latest"
     59 		touch "${tmpdir}/outdated"
     60 	fi
     61 done
     62 
     63 [ -f "${tmpdir}/outdated" ] && exit 1
     64 exit 0