-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-check-strict.sh
More file actions
executable file
·436 lines (359 loc) · 15.1 KB
/
dev-check-strict.sh
File metadata and controls
executable file
·436 lines (359 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env bash
# dev-check.sh — full Rust quality gate (macOS)
# Runs: fmt · fix · clippy (pedantic+nursery) · tests · audit · deny · dupes
# Produces per-file clustered clippy reports in clippy_reports/
set -Eeuo pipefail
# ─── Colours ──────────────────────────────────────────────────────────────────
if [[ -t 1 ]]; then
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
RESET='\033[0m'
else
RED=''
YELLOW=''
GREEN=''
CYAN=''
BOLD=''
DIM=''
RESET=''
fi
# ─── Globals ──────────────────────────────────────────────────────────────────
SCRIPT_START=$SECONDS
PASS_COUNT=0
FAIL_COUNT=0
SKIP_COUNT=0
FAILED_STEPS=()
REPORT_DIR="clippy_reports"
RAW_FILE="$REPORT_DIR/clippy_raw.txt"
CLUSTER_DIR="$REPORT_DIR/clusters"
SUMMARY_FILE="$REPORT_DIR/summary.txt"
# ─── Helpers ──────────────────────────────────────────────────────────────────
has_cmd() {
type -P "$1" >/dev/null 2>&1
}
step() {
echo ""
echo -e "${BOLD}${CYAN}════ $1 ════${RESET}"
}
pass() {
echo -e " ${GREEN}✓${RESET} $1"
PASS_COUNT=$(( PASS_COUNT + 1 ))
}
fail() {
echo -e " ${RED}✗${RESET} $1"
FAIL_COUNT=$(( FAIL_COUNT + 1 ))
FAILED_STEPS+=("$1")
}
skip() {
echo -e " ${DIM}–${RESET} $1 ${DIM}(skipped — tool not installed)${RESET}"
SKIP_COUNT=$(( SKIP_COUNT + 1 ))
}
warn() {
echo -e " ${YELLOW}⚠${RESET} $1"
}
die() {
echo -e "${RED}Error:${RESET} $1"
echo -e " Install with: ${DIM}$2${RESET}"
exit 1
}
elapsed() {
local secs=$(( SECONDS - SCRIPT_START ))
printf '%dm%02ds' $(( secs / 60 )) $(( secs % 60 ))
}
# ─── Strip cargo noise from clippy output ─────────────────────────────────────
filter_clippy() {
grep -Ev \
'^[[:space:]]*(Compiling|Checking|Downloading|Updating|Fresh|Finished|Blocking|Locking|Dirty|Scraping|Running|Doctest)[[:space:]]' \
| grep -Ev \
'^[[:space:]]*= note: `#\[' \
| grep -Ev \
'^warning: [0-9]+ warning(s)? emitted' \
| grep -Ev \
'^error: aborting due to' \
| grep -Ev \
'^[[:space:]]*= note: for more information' \
| sed '/^[[:space:]]*$/d' \
|| true
}
# ─── Header ───────────────────────────────────────────────────────────────────
echo -e "${BOLD}"
echo "╔══════════════════════════════════════════════════╗"
echo "║ Rust Full Quality Gate Check ║"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${RESET}"
# ─── CPU cores (macOS-aware) ──────────────────────────────────────────────────
export CARGO_BUILD_JOBS
CARGO_BUILD_JOBS=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 4)
echo -e " ${DIM}Using ${BOLD}${CARGO_BUILD_JOBS}${RESET}${DIM} CPU cores${RESET}"
# ─── Required tools ───────────────────────────────────────────────────────────
step "Verifying required tools"
if ! has_cmd cargo; then
die "required tool 'cargo' is not installed." "https://rustup.rs"
fi
if ! has_cmd rustfmt; then
die "required tool 'rustfmt' is not installed." "rustup component add rustfmt"
fi
# clippy-driver is not always on PATH on macOS — check via cargo subcommand instead
if ! cargo clippy --version >/dev/null 2>&1; then
die "required tool 'clippy' is not installed." "rustup component add clippy"
fi
pass "cargo · rustfmt · clippy all present"
# Optional tools — warn but do not exit
if ! has_cmd cargo-audit; then
warn "'cargo-audit' not installed — step will be skipped. cargo install cargo-audit"
fi
if ! has_cmd cargo-deny; then
warn "'cargo-deny' not installed — step will be skipped. cargo install cargo-deny"
fi
if ! has_cmd cargo-udeps; then
warn "'cargo-udeps' not installed — step will be skipped. cargo install cargo-udeps"
fi
if ! has_cmd cargo-msrv; then
warn "'cargo-msrv' not installed — step will be skipped. cargo install cargo-msrv"
fi
# ─── Prepare report directory ─────────────────────────────────────────────────
rm -rf "$REPORT_DIR"
mkdir -p "$CLUSTER_DIR"
# ─── Optional: update deps ────────────────────────────────────────────────────
if [[ "${1:-}" == "--update" ]]; then
step "Updating dependency index"
if cargo update 2>&1; then
pass "cargo update"
else
fail "cargo update"
fi
fi
# ─── 1. Format ────────────────────────────────────────────────────────────────
step "1 · Formatting (cargo fmt)"
if cargo fmt --all 2>&1; then
pass "cargo fmt --all"
else
fail "cargo fmt --all"
fi
if git diff --quiet 2>/dev/null; then
pass "No unstaged format changes"
else
warn "cargo fmt changed files — commit the formatted code"
fi
# ─── 2. Auto-fix ──────────────────────────────────────────────────────────────
step "2 · Automatic fixes (cargo fix)"
if cargo fix --allow-dirty --allow-staged --allow-no-vcs --all-features 2>&1; then
pass "cargo fix"
else
warn "cargo fix had warnings (non-fatal)"
fi
# ─── 3. Clippy — strict pedantic+nursery ──────────────────────────────────────
step "3 · Lint (cargo clippy — pedantic + nursery)"
CLIPPY_FLAGS=(
"-D" "warnings"
"-W" "clippy::pedantic"
"-W" "clippy::nursery"
"-W" "clippy::correctness"
"-W" "clippy::suspicious"
"-W" "clippy::complexity"
"-W" "clippy::perf"
"-W" "clippy::unwrap_used"
"-W" "clippy::expect_used"
"-W" "clippy::panic"
"-W" "clippy::todo"
"-W" "clippy::unimplemented"
"-W" "clippy::unreachable"
"-W" "clippy::indexing_slicing"
"-W" "clippy::cast_possible_truncation"
"-W" "clippy::cast_possible_wrap"
"-W" "clippy::cast_sign_loss"
"-W" "clippy::cast_precision_loss"
"-W" "clippy::arithmetic_side_effects"
"-W" "clippy::format_collect"
"-W" "clippy::uninlined_format_args"
"-W" "clippy::redundant_closure_for_method_calls"
"-W" "clippy::map_unwrap_or"
"-W" "clippy::manual_let_else"
"-W" "clippy::single_match_else"
"-W" "clippy::if_not_else"
"-W" "clippy::option_if_let_else"
"-W" "clippy::cloned_instead_of_copied"
"-W" "clippy::doc_markdown"
"-W" "clippy::redundant_else"
"-W" "clippy::too_many_lines"
"-W" "clippy::missing_errors_doc"
"-W" "clippy::missing_panics_doc"
)
CLIPPY_CMD=(
cargo clippy
--all-targets
--all-features
--message-format=short
--
"${CLIPPY_FLAGS[@]}"
)
echo -e " ${DIM}Running: ${CLIPPY_CMD[*]}${RESET}"
echo ""
CLIPPY_EXIT=0
"${CLIPPY_CMD[@]}" 2>&1 \
| filter_clippy \
| tee "$RAW_FILE" \
|| CLIPPY_EXIT=${PIPESTATUS[0]}
# ── Cluster clippy output by source file ──────────────────────────────────────
echo ""
echo -e " ${DIM}Clustering clippy output by file...${RESET}"
CURRENT_OUTFILE=""
while IFS= read -r line; do
if [[ $line =~ ([a-zA-Z0-9_/.-]+\.rs):[0-9]+:[0-9]+ ]]; then
FILE="${BASH_REMATCH[1]}"
DIR=$(dirname "$FILE")
if [[ "$DIR" == "." ]]; then
CLUSTER="root"
else
CLUSTER=$(echo "$DIR" | tr '/' '_')
fi
CURRENT_OUTFILE="$CLUSTER_DIR/${CLUSTER}.txt"
{
echo ""
echo "----------------------------------------"
echo "FILE: $FILE"
echo "----------------------------------------"
} >> "$CURRENT_OUTFILE"
fi
if [[ -n "$CURRENT_OUTFILE" ]]; then
echo "$line" >> "$CURRENT_OUTFILE"
fi
done < "$RAW_FILE"
# ── Count errors and warnings ─────────────────────────────────────────────────
CLIPPY_ERRORS=$(grep -c '^error' "$RAW_FILE" 2>/dev/null || echo 0)
CLIPPY_WARNS=$(grep -c '^warning' "$RAW_FILE" 2>/dev/null || echo 0)
CLUSTER_COUNT=$(find "$CLUSTER_DIR" -name '*.txt' | wc -l | tr -d ' ')
if [[ $CLIPPY_EXIT -eq 0 ]]; then
pass "clippy clean (${CLIPPY_WARNS} warnings, 0 errors)"
else
fail "clippy reported ${CLIPPY_ERRORS} error(s) across ${CLUSTER_COUNT} file cluster(s)"
echo ""
echo -e " ${BOLD}Cluster reports:${RESET}"
for f in "$CLUSTER_DIR"/*.txt; do
[[ -f "$f" ]] && echo -e " ${DIM}$(basename "$f")${RESET}"
done
echo -e " ${DIM}Full output: $RAW_FILE${RESET}"
fi
# ─── 4. Tests ─────────────────────────────────────────────────────────────────
step "4 · Tests (cargo test)"
TEST_EXIT=0
cargo test --all --all-features 2>&1 || TEST_EXIT=$?
if [[ $TEST_EXIT -eq 0 ]]; then
pass "All tests passed"
else
fail "Test suite failed (exit $TEST_EXIT)"
fi
# ─── 5. Security audit ────────────────────────────────────────────────────────
step "5 · Security audit (cargo audit)"
if has_cmd cargo-audit; then
AUDIT_EXIT=0
cargo audit 2>&1 || AUDIT_EXIT=$?
if [[ $AUDIT_EXIT -eq 0 ]]; then
pass "No known vulnerabilities"
else
fail "cargo-audit found vulnerability/advisory — review output above"
fi
else
skip "cargo-audit → cargo install cargo-audit"
fi
# ─── 6. Dependency policy ─────────────────────────────────────────────────────
step "6 · Dependency policy (cargo deny)"
if has_cmd cargo-deny; then
DENY_EXIT=0
cargo deny check 2>&1 || DENY_EXIT=$?
if [[ $DENY_EXIT -eq 0 ]]; then
pass "cargo deny — all policies satisfied"
else
fail "cargo deny — policy violation(s) found"
fi
else
skip "cargo-deny → cargo install cargo-deny"
fi
# ─── 7. Unused dependencies ───────────────────────────────────────────────────
step "7 · Unused dependencies (cargo udeps)"
if has_cmd cargo-udeps; then
UDEPS_EXIT=0
cargo +nightly udeps --all-targets 2>&1 || UDEPS_EXIT=$?
if [[ $UDEPS_EXIT -eq 0 ]]; then
pass "No unused dependencies"
else
fail "Unused dependencies detected — remove them from Cargo.toml"
fi
else
skip "cargo-udeps → cargo install cargo-udeps (requires nightly)"
fi
# ─── 8. MSRV check ────────────────────────────────────────────────────────────
step "8 · Minimum supported Rust version (cargo msrv)"
if has_cmd cargo-msrv; then
MSRV_EXIT=0
cargo msrv verify 2>&1 || MSRV_EXIT=$?
if [[ $MSRV_EXIT -eq 0 ]]; then
pass "MSRV satisfied"
else
warn "MSRV check failed — your rust-version in Cargo.toml may need updating"
fi
else
skip "cargo-msrv → cargo install cargo-msrv"
fi
# ─── 9. Duplicate dependencies ────────────────────────────────────────────────
step "9 · Duplicate dependencies (cargo tree -d)"
DUPES=$(cargo tree -d 2>&1 || true)
if echo "$DUPES" | grep -q '\['; then
warn "Duplicate crate versions detected:"
echo "$DUPES" | grep '^\[' | sort -u | while IFS= read -r line; do
echo -e " ${YELLOW}${line}${RESET}"
done
else
pass "No duplicate crate versions"
fi
# ─── 10. Build check (release) ────────────────────────────────────────────────
step "10 · Release build check (cargo build --release)"
BUILD_EXIT=0
cargo build --release --all-features 2>&1 || BUILD_EXIT=$?
if [[ $BUILD_EXIT -eq 0 ]]; then
pass "Release build clean"
else
fail "Release build failed"
fi
# ─── Summary ──────────────────────────────────────────────────────────────────
TOTAL_SECS=$(( SECONDS - SCRIPT_START ))
{
echo "dev-check summary — $(date)"
echo "Duration: ${TOTAL_SECS}s"
echo "Passed: $PASS_COUNT"
echo "Failed: $FAIL_COUNT"
echo "Skipped: $SKIP_COUNT"
if [[ ${#FAILED_STEPS[@]} -gt 0 ]]; then
echo ""
echo "Failed steps:"
for s in "${FAILED_STEPS[@]}"; do
echo " - $s"
done
fi
} | tee "$SUMMARY_FILE"
echo ""
if [[ $FAIL_COUNT -eq 0 ]]; then
echo -e "${BOLD}${GREEN}"
echo "╔══════════════════════════════════════════════════╗"
echo "║ ✓ All checks passed ($(elapsed())) ║"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${RESET}"
exit 0
else
echo -e "${BOLD}${RED}"
echo "╔══════════════════════════════════════════════════╗"
echo "║ ✗ $FAIL_COUNT check(s) failed ($(elapsed())) ║"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${RESET}"
echo -e "${RED}Failed steps:${RESET}"
for s in "${FAILED_STEPS[@]}"; do
echo -e " ${RED}•${RESET} $s"
done
echo ""
echo -e " ${DIM}Reports saved to: $REPORT_DIR/${RESET}"
exit 1
fi