Files
lgp-admin-plus-api/scripts/lint.sh

42 lines
1023 B
Bash
Raw Normal View History

#!/usr/bin/env bash
# 语法检查 + 行尾规整
#
# 从 Windows 侧的 UNC 路径写文件会带 CRLFPHP 能跑但和仓库里其余 LF 文件混排,
# diff 会整片变红。这里统一转成 LF 再 php -l。
#
# 用法bash scripts/lint.sh [目录或文件 ...] 不带参数则检查 app/ routes/ config/
set -uo pipefail
cd "$(diname "$0")/.." || exit 1
TARGETS=("$@")
if [ ${#TARGETS[@]} -eq 0 ]; then
TARGETS=(app routes config database)
fi
mapfile -t FILES < <(find "${TARGETS[@]}" -type f -name '*.php' 2>/dev/null | sort)
if [ ${#FILES[@]} -eq 0 ]; then
echo "no php files under: ${TARGETS[*]}"
exit 0
fi
CRLF_FIXED=0
for f in "${FILES[@]}"; do
if grep -qU $'\r' "$f" 2>/dev/null; then
perl -pi -e 's/\r\n/\n/' "$f"
CRLF_FIXED=$((CRLF_FIXED + 1))
fi
done
FAILED=0
for f in "${FILES[@]}"; do
if ! out=$(php -l "$f" 2>&1); then
echo "$out"
FAILED=$((FAILED + 1))
fi
done
echo "checked=${#FILES[@]} crlf_fixed=${CRLF_FIXED} syntax_errors=${FAILED}"
[ "$FAILED" -eq 0 ]