PureYAML/scripts/check-namespacing.sh

41 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Namespacing gate (docs/rules/code-style.md): one non-private top-level type per
# file. A file with more than one file-scope type declaration is a violation:
# split it, or mark helper types private/fileprivate.
#
# Inert until Sources exists. Portable: bash 3.2 and 4+.
set -u
SRC="Sources"
if [ ! -d "$SRC" ]; then
echo "namespacing: no $SRC yet, skipping."
exit 0
fi
FAIL=0
while IFS= read -r f; do
count=$(grep -cE "^(public |package |internal )?(actor|struct|enum|protocol|class|final class) [A-Z]" "$f")
if [ "$count" -gt 1 ]; then
echo "namespacing: $count file-scope types in $f (one per file)" >&2
FAIL=1
fi
public_types=$(grep -nE "^public (actor|struct|enum|protocol|class|final class) [A-Z]" "$f" || true)
if [ -n "$public_types" ]; then
if [ "$f" = "Sources/PureYAML.swift" ] &&
[ "$public_types" = "2:public enum PureYAML {" ]; then
:
else
echo "namespacing: public file-scope type in $f (wrap it under PureYAML)" >&2
printf '%s\n' "$public_types" | sed 's/^/ /' >&2
FAIL=1
fi
fi
done < <(find "$SRC" -name "*.swift")
if [ "$FAIL" -ne 0 ]; then
echo "namespacing: gate failed. Rule: docs/rules/code-style.md" >&2
fi
exit "$FAIL"