231 lines
5.6 KiB
Bash
Executable File
231 lines
5.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# 代码质量检查脚本
|
|
# 检查脚本语法、代码风格等
|
|
|
|
set -euo pipefail
|
|
|
|
# 颜色定义
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 计数器
|
|
TOTAL_FILES=0
|
|
PASSED_FILES=0
|
|
FAILED_FILES=0
|
|
|
|
# 日志函数
|
|
log_info() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
log_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# 检查 Shell 脚本语法
|
|
check_shell_syntax() {
|
|
log_info "检查 Shell 脚本语法..."
|
|
|
|
local shell_files
|
|
shell_files=$(find scripts/ -name "*.sh" -type f)
|
|
|
|
if [ -z "$shell_files" ]; then
|
|
log_warning "未找到 Shell 脚本文件"
|
|
return 0
|
|
fi
|
|
|
|
while IFS= read -r file; do
|
|
((TOTAL_FILES++))
|
|
log_info "检查: $file"
|
|
|
|
if bash -n "$file"; then
|
|
log_success "✓ $file"
|
|
((PASSED_FILES++))
|
|
else
|
|
log_error "✗ $file - 语法错误"
|
|
((FAILED_FILES++))
|
|
fi
|
|
done <<< "$shell_files"
|
|
}
|
|
|
|
# 检查 Python 脚本语法
|
|
check_python_syntax() {
|
|
log_info "检查 Python 脚本语法..."
|
|
|
|
local python_files
|
|
python_files=$(find scripts/ -name "*.py" -type f)
|
|
|
|
if [ -z "$python_files" ]; then
|
|
log_warning "未找到 Python 脚本文件"
|
|
return 0
|
|
fi
|
|
|
|
while IFS= read -r file; do
|
|
((TOTAL_FILES++))
|
|
log_info "检查: $file"
|
|
|
|
if python3 -m py_compile "$file" 2>/dev/null; then
|
|
log_success "✓ $file"
|
|
((PASSED_FILES++))
|
|
else
|
|
log_error "✗ $file - 语法错误"
|
|
((FAILED_FILES++))
|
|
fi
|
|
done <<< "$python_files"
|
|
}
|
|
|
|
# 检查脚本权限
|
|
check_script_permissions() {
|
|
log_info "检查脚本执行权限..."
|
|
|
|
local script_files
|
|
script_files=$(find scripts/ -name "*.sh" -type f)
|
|
|
|
if [ -z "$script_files" ]; then
|
|
log_warning "未找到脚本文件"
|
|
return 0
|
|
fi
|
|
|
|
local permission_issues=0
|
|
|
|
while IFS= read -r file; do
|
|
if [ ! -x "$file" ]; then
|
|
log_warning "⚠ $file - 缺少执行权限"
|
|
((permission_issues++))
|
|
fi
|
|
done <<< "$script_files"
|
|
|
|
if [ "$permission_issues" -eq 0 ]; then
|
|
log_success "所有脚本都有执行权限"
|
|
else
|
|
log_warning "发现 $permission_issues 个权限问题"
|
|
log_info "运行以下命令修复权限: find scripts/ -name '*.sh' -exec chmod +x {} \\;"
|
|
fi
|
|
}
|
|
|
|
# 检查脚本头部
|
|
check_script_headers() {
|
|
log_info "检查脚本头部..."
|
|
|
|
local script_files
|
|
script_files=$(find scripts/ -name "*.sh" -type f)
|
|
|
|
if [ -z "$script_files" ]; then
|
|
log_warning "未找到脚本文件"
|
|
return 0
|
|
fi
|
|
|
|
local header_issues=0
|
|
|
|
while IFS= read -r file; do
|
|
local first_line
|
|
first_line=$(head -n 1 "$file")
|
|
|
|
if [[ ! "$first_line" =~ ^#!/bin/bash ]] && [[ ! "$first_line" =~ ^#!/usr/bin/env\ bash ]]; then
|
|
log_warning "⚠ $file - 缺少或错误的 shebang"
|
|
((header_issues++))
|
|
fi
|
|
done <<< "$script_files"
|
|
|
|
if [ "$header_issues" -eq 0 ]; then
|
|
log_success "所有脚本都有正确的 shebang"
|
|
else
|
|
log_warning "发现 $header_issues 个 shebang 问题"
|
|
fi
|
|
}
|
|
|
|
# 检查配置文件语法
|
|
check_config_syntax() {
|
|
log_info "检查配置文件语法..."
|
|
|
|
# 检查 JSON 文件
|
|
local json_files
|
|
json_files=$(find . -name "*.json" -type f -not -path "./.git/*")
|
|
|
|
if [ -n "$json_files" ]; then
|
|
while IFS= read -r file; do
|
|
((TOTAL_FILES++))
|
|
log_info "检查 JSON: $file"
|
|
|
|
if jq empty "$file" 2>/dev/null; then
|
|
log_success "✓ $file"
|
|
((PASSED_FILES++))
|
|
else
|
|
log_error "✗ $file - JSON 语法错误"
|
|
((FAILED_FILES++))
|
|
fi
|
|
done <<< "$json_files"
|
|
fi
|
|
|
|
# 检查 YAML 文件
|
|
local yaml_files
|
|
yaml_files=$(find . -name "*.yml" -o -name "*.yaml" -type f -not -path "./.git/*")
|
|
|
|
if [ -n "$yaml_files" ] && command -v yamllint &> /dev/null; then
|
|
while IFS= read -r file; do
|
|
((TOTAL_FILES++))
|
|
log_info "检查 YAML: $file"
|
|
|
|
if yamllint "$file" 2>/dev/null; then
|
|
log_success "✓ $file"
|
|
((PASSED_FILES++))
|
|
else
|
|
log_error "✗ $file - YAML 语法错误"
|
|
((FAILED_FILES++))
|
|
fi
|
|
done <<< "$yaml_files"
|
|
elif [ -n "$yaml_files" ]; then
|
|
log_warning "yamllint 未安装,跳过 YAML 检查"
|
|
fi
|
|
}
|
|
|
|
# 生成报告
|
|
generate_report() {
|
|
log_info "生成检查报告..."
|
|
|
|
echo
|
|
echo "=================================="
|
|
echo " 代码质量检查报告"
|
|
echo "=================================="
|
|
echo "总文件数: $TOTAL_FILES"
|
|
echo "通过: $PASSED_FILES"
|
|
echo "失败: $FAILED_FILES"
|
|
echo "成功率: $(( PASSED_FILES * 100 / (TOTAL_FILES == 0 ? 1 : TOTAL_FILES) ))%"
|
|
echo "=================================="
|
|
|
|
if [ "$FAILED_FILES" -eq 0 ]; then
|
|
log_success "所有检查都通过了!"
|
|
return 0
|
|
else
|
|
log_error "发现 $FAILED_FILES 个问题,请修复后重新运行"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# 主函数
|
|
main() {
|
|
log_info "开始代码质量检查..."
|
|
|
|
check_shell_syntax
|
|
check_python_syntax
|
|
check_script_permissions
|
|
check_script_headers
|
|
check_config_syntax
|
|
|
|
generate_report
|
|
}
|
|
|
|
# 执行主函数
|
|
main "$@" |