Преглед изворни кода

build: enforce quality gates via scripts/check.sh

The original Makefile only had `make check` that ran cargo fmt/test/
clippy/lines inline.  Move the actual implementation into
scripts/check.sh so CI can run the same script without parsing
Makefile output, and add auto-fix targets (make fmt, make clippy-fix)
for dev ergonomics.

The script:
- Runs fmt, clippy, tests, then file-size check — fail-fast on the
  first failure so devs see one problem at a time.
- Color output when stderr is a tty, plain when piped (CI logs).
- Same exit codes as the underlying cargo invocations.

The Makefile:
- `make check` delegates to the script.
- Existing `make check-fmt/test/clippy/lines` targets kept for iterating
  on a single failure.
- New `make fmt` (auto-fix formatting) and `make clippy-fix` (apply
  clippy suggestions) for the rare cases where a developer wants to
  pre-fix before reviewing the diff.
- `make help` lists targets.
Alexander Korolev пре 4 месеци
родитељ
комит
65cbf60fce
2 измењених фајлова са 93 додато и 4 уклоњено
  1. 23 4
      Makefile
  2. 70 0
      scripts/check.sh

+ 23 - 4
Makefile

@@ -1,8 +1,10 @@
-.PHONY: check check-fmt check-test check-clippy check-lines
+.PHONY: check check-fmt check-test check-clippy check-lines fmt clippy test help
 
-# Run all quality gates
-check: check-fmt check-test check-clippy check-lines
+# Run all quality gates (delegates to the shared script for use in CI).
+check:
+	@./scripts/check.sh
 
+# Individual gates (for iterating on a single failure).
 check-fmt:
 	cargo fmt --check
 
@@ -12,7 +14,7 @@ check-test:
 check-clippy:
 	cargo clippy --all-targets -- -D warnings
 
-# Ensure no Rust source file exceeds 500 lines
+# Ensure no Rust source file exceeds 500 lines (see AGENTS.md).
 check-lines:
 	@exit_code=0; \
 	max=500; \
@@ -29,3 +31,20 @@ check-lines:
 	else \
 		exit "$$exit_code"; \
 	fi
+
+# Auto-fix formatting (not a gate; use sparingly before committing).
+fmt:
+	cargo fmt --all
+
+# Auto-fix lints (clippy --fix); review the diff before committing.
+clippy-fix:
+	cargo clippy --all-targets --fix --allow-dirty --allow-staged
+
+help:
+	@echo "make check         Run all quality gates (fmt, test, clippy, lines)"
+	@echo "make check-fmt     Run cargo fmt --check"
+	@echo "make check-test    Run cargo test"
+	@echo "make check-clippy  Run cargo clippy --all-targets -- -D warnings"
+	@echo "make check-lines   Verify all src/**/*.rs are ≤500 lines"
+	@echo "make fmt           Auto-fix formatting"
+	@echo "make clippy-fix    Apply clippy suggestions (review diff)"

+ 70 - 0
scripts/check.sh

@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+# Run all quality gates for sm121-launcher (see AGENTS.md and Makefile):
+#
+#   1. cargo fmt --check          (formatting)
+#   2. cargo clippy --all-targets -- -D warnings   (lints, warnings-as-errors)
+#   3. cargo test                 (unit tests)
+#   4. file-size check            (no src/**/*.rs > 500 lines)
+#
+# Fails fast on the first failed gate so devs see one problem at a time.
+# Wire into CI by running `./scripts/check.sh`; locally use `make check`.
+
+set -euo pipefail
+
+cd "$(dirname "$0")/.."
+
+# Color output if stderr is a tty.
+if [ -t 2 ]; then
+    RED=$'\033[31m'; GREEN=$'\033[32m'; YELLOW=$'\033[33m'; RESET=$'\033[0m'
+else
+    RED=""; GREEN=""; YELLOW=""; RESET=""
+fi
+
+step() { printf '%s==> %s%s\n' "$YELLOW" "$*" "$RESET" >&2; }
+ok()   { printf '%s  ✓ %s%s\n' "$GREEN"  "$*" "$RESET" >&2; }
+die()  { printf '%s  ✗ %s%s\n' "$RED"    "$*" "$RESET" >&2; exit 1; }
+
+# 1. Formatting
+step "cargo fmt --check"
+if cargo fmt --check; then
+    ok "fmt"
+else
+    die "cargo fmt --check failed — run 'cargo fmt' or 'make fmt' to fix"
+fi
+
+# 2. Lints (treat warnings as errors)
+step "cargo clippy --all-targets -- -D warnings"
+if cargo clippy --all-targets -- -D warnings; then
+    ok "clippy"
+else
+    die "cargo clippy failed"
+fi
+
+# 3. Tests
+step "cargo test"
+if cargo test; then
+    ok "test"
+else
+    die "cargo test failed"
+fi
+
+# 4. File size (AGENTS.md: no src file >500 lines)
+step "src/**/*.rs ≤ 500 lines"
+max=500
+exit_code=0
+while IFS= read -r f; do
+    [ -f "$f" ] || continue
+    lines=$(wc -l < "$f")
+    if [ "$lines" -gt "$max" ]; then
+        printf '%s  ✗ %s has %s lines (max %s)%s\n' "$RED" "$f" "$lines" "$max" "$RESET" >&2
+        exit_code=1
+    fi
+done < <(find src -type f -name '*.rs')
+if [ "$exit_code" -eq 0 ]; then
+    ok "lines"
+else
+    exit 1
+fi
+
+echo
+printf '%sAll gates passed.%s\n' "$GREEN" "$RESET"