Add Cargo workspace, crate skeletons and the gate

Implemented-By: Laguna S 2.1 (OpenCode)
This commit is contained in:
2026-09-17 06:45:02 -07:00
parent 3e26c2e3c0
commit 8dc04b1d48
31 changed files with 516 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
#!/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
+105
View File
@@ -0,0 +1,105 @@
#!/bin/sh
# Fails if a key in [workspace.dependencies] of ROOT/Cargo.toml that is not a
# workspace crate has no table row starting `| `name` |` in ROOT/docs/dependencies.md.
# Also fails if any dependency line in any ROOT/crates/*/Cargo.toml lacks
# `workspace = true`.
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
root_toml="$ROOT/Cargo.toml"
docs="$ROOT/docs/dependencies.md"
# Extract dependency keys from every [dependencies*]-ish section.
dep_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)
}
}
' "$root_toml" 2>/dev/null)
# Part 1: non-workspace-crate workspace dependencies must have a docs table row.
for key in $dep_keys; do
hit=0
for w in $workspace; do
if [ "$key" = "$w" ]; then hit=1; break; fi
done
if [ "$hit" -eq 0 ]; then
if ! awk -v k="$key" 'index($0, "| `" k "` |") == 1 { found = 1; exit } END { exit !found }' "$docs" 2>/dev/null; then
echo "check-dep-docs: $key has no row in $docs" >&2
status=1
fi
fi
done
# Part 2: every dependency line in a crate manifest must contain `workspace = true`.
for crate in "$ROOT"/crates/*/; do
[ -d "$crate" ] || continue
crate_name=$(basename "$crate")
[ -f "$crate/Cargo.toml" ] || continue
bad=$(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]*=/) {
print $0
}
}
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 | grep -v 'workspace = true')
if [ -n "$bad" ]; then
echo "$bad" | while read -r line; do
[ -n "$line" ] && echo "check-dep-docs: $crate_name declares a dependency without workspace = true: $line" >&2
done
status=1
fi
done
exit $status
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
# Fails if any *.rs file under ROOT/crates has more than 500 lines.
# Files under any target/ directory are ignored. Test files count.
ROOT="${1:-.}"
bad=$(find "$ROOT/crates" -type f -name '*.rs' ! -path '*/target/*' \
-exec awk 'END { exit !(NR > 500) }' {} \; -print 2>/dev/null)
if [ -n "$bad" ]; then
echo "$bad" | while read -r f; do
[ -n "$f" ] && echo "check-lines: $f" >&2
done
exit 1
fi
exit 0
+81
View File
@@ -0,0 +1,81 @@
#!/bin/sh
# Self-test for the three gate scripts. It builds small fake trees in a temporary
# directory and checks that each script passes the good tree and fails the bad ones.
# Do not edit: this file defines the required behaviour of the scripts.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
fails=0
expect() { # expect pass|fail NAME SCRIPT ROOT
want="$1"; name="$2"; script="$3"; root="$4"
if sh "$here/$script" "$root" >/dev/null 2>&1; then got=pass; else got=fail; fi
if [ "$got" != "$want" ]; then
echo "test-gate-scripts: $name: expected $want, got $got" >&2
fails=$((fails + 1))
fi
}
manifest() { # manifest DIR NAME [DEPENDENCY-LINES...]
dir="$1"; name="$2"; shift 2
mkdir -p "$dir/src"
{
printf '[package]\nname = "%s"\nversion = "0.1.0"\n\n[dependencies]\n' "$name"
for line in "$@"; do printf '%s\n' "$line"; done
} > "$dir/Cargo.toml"
}
tree() { # tree ROOT: a good workspace with proto, loopd and brokerd
root="$1"
mkdir -p "$root/docs"
printf '[workspace]\nmembers = ["crates/*"]\n\n[workspace.dependencies]\nproto = { path = "crates/proto" }\nserde = { version = "1", features = ["derive"] }\n' > "$root/Cargo.toml"
printf '# Dependencies\n\n| Crate | Why |\n|---|---|\n| `serde` | types |\n' > "$root/docs/dependencies.md"
manifest "$root/crates/proto" proto 'serde.workspace = true'
manifest "$root/crates/loopd" loopd 'proto.workspace = true'
manifest "$root/crates/brokerd" brokerd 'proto = { workspace = true }' 'serde.workspace = true'
}
lines() { # lines N FILE
mkdir -p "$(dirname "$2")"
i=0; : > "$2"
while [ "$i" -lt "$1" ]; do echo "// line" >> "$2"; i=$((i + 1)); done
}
# check-lines.sh
tree "$tmp/l-ok"; lines 500 "$tmp/l-ok/crates/loopd/src/lib.rs"
expect pass "500 lines is allowed" check-lines.sh "$tmp/l-ok"
tree "$tmp/l-bad"; lines 501 "$tmp/l-bad/crates/loopd/src/deep/mod.rs"
expect fail "501 lines in a nested file" check-lines.sh "$tmp/l-bad"
tree "$tmp/l-test"; lines 501 "$tmp/l-test/crates/proto/tests/big.rs"
expect fail "501 lines in a test file" check-lines.sh "$tmp/l-test"
tree "$tmp/l-tgt"; lines 501 "$tmp/l-tgt/crates/proto/target/debug/gen.rs"
expect pass "files under target/ are ignored" check-lines.sh "$tmp/l-tgt"
# check-crate-deps.sh
tree "$tmp/c-ok"
expect pass "roles depend only on proto" check-crate-deps.sh "$tmp/c-ok"
tree "$tmp/c-role"; manifest "$tmp/c-role/crates/loopd" loopd 'proto.workspace = true' 'brokerd.workspace = true'
expect fail "role depends on another role" check-crate-deps.sh "$tmp/c-role"
tree "$tmp/c-tbl"; manifest "$tmp/c-tbl/crates/loopd" loopd 'brokerd = { path = "../brokerd" }'
expect fail "role depends on another role by path" check-crate-deps.sh "$tmp/c-tbl"
tree "$tmp/c-dev"; printf '\n[dev-dependencies]\nbrokerd.workspace = true\n' >> "$tmp/c-dev/crates/loopd/Cargo.toml"
expect fail "role dev-depends on another role" check-crate-deps.sh "$tmp/c-dev"
tree "$tmp/c-proto"; manifest "$tmp/c-proto/crates/proto" proto 'loopd.workspace = true'
expect fail "proto depends on a role" check-crate-deps.sh "$tmp/c-proto"
# check-dep-docs.sh
tree "$tmp/d-ok"
expect pass "every dependency is documented" check-dep-docs.sh "$tmp/d-ok"
tree "$tmp/d-miss"; printf 'rand = "0.9"\n' >> "$tmp/d-miss/Cargo.toml"
expect fail "workspace dependency without a docs row" check-dep-docs.sh "$tmp/d-miss"
tree "$tmp/d-prose"; printf 'rand = "0.9"\n' >> "$tmp/d-prose/Cargo.toml"; printf '\nWe do not use rand.\n' >> "$tmp/d-prose/docs/dependencies.md"
expect fail "a mention in prose is not a table row" check-dep-docs.sh "$tmp/d-prose"
tree "$tmp/d-loose"; manifest "$tmp/d-loose/crates/loopd" loopd 'proto.workspace = true' 'rand = "0.9"'
expect fail "crate declares a dependency outside the workspace table" check-dep-docs.sh "$tmp/d-loose"
if [ "$fails" -ne 0 ]; then
echo "test-gate-scripts: $fails failure(s)" >&2
exit 1
fi
echo "test-gate-scripts: ok"