|
| 1 | +#!/bin/bash |
| 2 | +[ -n "$BASH_VERSION" ] || { echo "ERROR: This script requires bash. Run with: bash $0" >&2; exit 1; } |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +RUNTIME_DIR="Packages/com.unity.inputsystem/InputSystem/Runtime" |
| 6 | + |
| 7 | +# POSIX ERE patterns for grep -E, compatible with macOS (BSD grep) and Ubuntu (GNU grep). |
| 8 | +# No \b word boundaries — not portable across both. False positive risk is negligible |
| 9 | +# since no real identifiers contain these namespace roots as substrings. |
| 10 | +# (\.[A-Za-z0-9_]+)* covers sub-namespaces (UnityEditor.UI, …Editor.Tools, …). |
| 11 | +# Add more lines as needed, e.g. 'UnityEditorInternal(\.[A-Za-z0-9_]+)*' |
| 12 | +FORBIDDEN_REGEX=( |
| 13 | + 'UnityEditor(\.[A-Za-z0-9_]+)*' |
| 14 | + 'UnityEngine\.InputSystem\.Editor(\.[A-Za-z0-9_]+)*' |
| 15 | +) |
| 16 | + |
| 17 | +RED=$'\033[0;31m' |
| 18 | +GREEN=$'\033[0;32m' |
| 19 | +NC=$'\033[0m' |
| 20 | + |
| 21 | +INCLUDE_COMMENTS=false |
| 22 | +for arg in "$@"; do |
| 23 | + case "$arg" in |
| 24 | + --include-comments) INCLUDE_COMMENTS=true ;; |
| 25 | + --help) |
| 26 | + cat <<EOF |
| 27 | +Usage: $(basename "$0") [OPTIONS] |
| 28 | +
|
| 29 | +Check that Runtime code has no Editor namespace dependencies. |
| 30 | +Edit FORBIDDEN_REGEX in this script to add patterns. |
| 31 | +
|
| 32 | +Options: |
| 33 | + --include-comments Also flag references inside XML doc and // comments |
| 34 | + --help Show this help |
| 35 | +EOF |
| 36 | + exit 0 ;; |
| 37 | + *) echo "Unknown option: $arg"; exit 1 ;; |
| 38 | + esac |
| 39 | +done |
| 40 | + |
| 41 | +[ -d "$RUNTIME_DIR" ] || { echo "${RED}ERROR: Runtime directory not found: $RUNTIME_DIR${NC}" >&2; exit 1; } |
| 42 | + |
| 43 | +COMBINED=$(IFS='|'; echo "${FORBIDDEN_REGEX[*]}") |
| 44 | + |
| 45 | +GREP_OUTPUT=$(grep -rnw --include='*.cs' --color=never -E "$COMBINED" "$RUNTIME_DIR" || true) |
| 46 | + |
| 47 | +if [ "$INCLUDE_COMMENTS" = false ]; then |
| 48 | + VIOLATIONS=$(echo "$GREP_OUTPUT" | grep -Ev ':[0-9]+:[[:space:]]*//' || true) |
| 49 | +else |
| 50 | + VIOLATIONS="$GREP_OUTPUT" |
| 51 | +fi |
| 52 | + |
| 53 | +if [ -z "$VIOLATIONS" ]; then |
| 54 | + echo "${GREEN}PASS: No Editor namespace references found in Runtime code.${NC}" |
| 55 | + exit 0 |
| 56 | +fi |
| 57 | + |
| 58 | +VIOLATION_COUNT=$(echo "$VIOLATIONS" | wc -l | tr -d ' ') |
| 59 | +echo "${RED}FAIL: Found $VIOLATION_COUNT Editor namespace reference(s) in Runtime code:${NC}" |
| 60 | +echo "" |
| 61 | +echo "$VIOLATIONS" |
| 62 | +echo "" |
| 63 | +echo "${RED}Active patterns:${NC}" |
| 64 | +for re in "${FORBIDDEN_REGEX[@]}"; do |
| 65 | + printf ' \033[0;31m%s\033[0m\n' "$re" |
| 66 | +done |
| 67 | +exit 1 |
0 commit comments