Files
boxmaker/scripts/check-crate-deps.sh
T
kyle 8dc04b1d48 Add Cargo workspace, crate skeletons and the gate
Implemented-By: Laguna S 2.1 (OpenCode)
2026-09-17 06:45:02 -07:00

69 lines
2.2 KiB
Bash

#!/bin/sh
# Fails if `proto` depends on any workspace crate, or if any other crate depends on
# a workspace crate other than `proto`. Every manifest section whose name contains
# "dependencies" (including [dev-dependencies]) is inspected. A dependency is the
# key in such a section, however it is written: `x.workspace = true`,
# `x = { path = ".." }` or `x = "1"`.
ROOT="${1:-.}"
status=0
# Workspace crate names: directories under ROOT/crates that contain a Cargo.toml.
workspace=""
for d in "$ROOT"/crates/*/; do
[ -d "$d" ] || continue
[ -f "$d/Cargo.toml" ] || continue
workspace="$workspace $(basename "$d")"
done
for crate in "$ROOT"/crates/*/; do
[ -d "$crate" ] || continue
crate_name=$(basename "$crate")
[ -f "$crate/Cargo.toml" ] || continue
keys=$(awk '
BEGIN { in_deps = 0; depth = 0 }
/^\[/ {
sec = $0
sub(/^\[[ \t]*/, "", sec)
sub(/[ \t]*\][ \t]*.*$/, "", sec)
in_deps = (sec ~ /dependencies/)
depth = 0
next
}
in_deps {
if (depth == 0) {
t = $0
sub(/^[ \t]+/, "", t)
if (t ~ /^[A-Za-z0-9][A-Za-z0-9_-]*(\.workspace)?[ \t]*=/) {
if (match(t, /[A-Za-z0-9][A-Za-z0-9_-]*/)) {
k = substr(t, RSTART, RLENGTH)
if (RSTART == 1) print k
}
}
}
s = $0
while (match(s, /[{}]/)) {
c = substr(s, RSTART, 1)
if (c == "{") depth++
else if (depth > 0) depth--
s = substr(s, RSTART + 1)
}
}
' "$crate/Cargo.toml" 2>/dev/null)
for key in $keys; do
hit=0
for w in $workspace; do
if [ "$key" = "$w" ]; then hit=1; break; fi
done
if [ "$hit" -eq 1 ]; then
if [ "$crate_name" = "proto" ]; then
echo "check-crate-deps: $crate_name depends on workspace crate $key" >&2
status=1
elif [ "$key" != "proto" ]; then
echo "check-crate-deps: $crate_name depends on workspace crate $key" >&2
status=1
fi
fi
done
done
exit $status