swagger_generator_flutter/validate.sh

281 lines
7.5 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Augment 代码生成规范验证脚本
# 用于验证生成的代码是否符合规范要求
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_info() {
echo -e "${BLUE} $1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
print_header() {
echo -e "${BLUE}🔍 Augment 代码生成规范验证${NC}"
echo "=================================================="
}
# 检查必要的工具
check_prerequisites() {
print_info "检查必要工具..."
if ! command -v dart &> /dev/null; then
print_error "Dart SDK 未安装或不在 PATH 中"
exit 1
fi
print_success "Dart SDK 已安装"
}
# 验证 swagger.json
validate_swagger() {
print_info "验证 swagger.json..."
if [ ! -f "swagger.json" ]; then
print_error "swagger.json 文件不存在"
return 1
fi
# 检查 JSON 格式
if ! jq empty swagger.json 2>/dev/null; then
if ! python3 -m json.tool swagger.json > /dev/null 2>&1; then
print_error "swagger.json 格式错误"
return 1
fi
fi
print_success "swagger.json 格式正确"
}
# 验证生成的文件结构
validate_file_structure() {
print_info "验证文件结构..."
if [ ! -d "generator" ]; then
print_error "generator 目录不存在"
return 1
fi
if [ ! -d "generator/api" ]; then
print_error "generator/api 目录不存在"
return 1
fi
if [ ! -d "generator/api_models" ]; then
print_error "generator/api_models 目录不存在"
return 1
fi
print_success "文件结构正确"
}
# 验证代码质量
validate_code_quality() {
print_info "验证代码质量..."
# 检查 generator 目录是否存在
if [ ! -d "generator" ]; then
print_warning "generator 目录不存在,跳过代码质量检查"
return 0
fi
# 运行 dart analyze
if dart analyze generator/ 2>/dev/null; then
print_success "静态分析通过"
else
print_warning "静态分析发现问题,请检查生成的代码"
fi
}
# 验证命名规范
validate_naming_conventions() {
print_info "验证命名规范..."
local errors=0
# 检查 API 文件命名
if [ -d "generator/api" ]; then
for file in generator/api/*.dart; do
if [ -f "$file" ]; then
filename=$(basename "$file")
if [[ ! "$filename" =~ ^[a-z][a-z0-9_]*_api\.dart$ ]] && [[ "$filename" != "api_client.dart" ]]; then
print_error "API 文件命名不规范: $filename"
errors=$((errors + 1))
fi
fi
done
fi
# 检查模型文件命名
if [ -d "generator/api_models" ]; then
for file in generator/api_models/*.dart; do
if [ -f "$file" ]; then
filename=$(basename "$file")
if [[ ! "$filename" =~ ^[a-z][a-z0-9_]*\.dart$ ]] && [[ "$filename" != "index.dart" ]]; then
print_error "模型文件命名不规范: $filename"
errors=$((errors + 1))
fi
fi
done
fi
if [ $errors -eq 0 ]; then
print_success "命名规范检查通过"
else
print_error "发现 $errors 个命名规范问题"
return 1
fi
}
# 验证类型一致性
validate_type_consistency() {
print_info "验证类型一致性..."
local generator_file="lib/generators/retrofit_api_generator.dart"
if [ ! -f "$generator_file" ]; then
print_warning "生成器文件不存在,跳过类型一致性检查"
return 0
fi
# 检查是否有硬编码的类型推断
local hardcoded_patterns=(
"TaskInfoResult"
"if.*contains.*login.*return"
"if.*contains.*task.*return"
"if.*tag.*contains.*return"
)
local errors=0
for pattern in "${hardcoded_patterns[@]}"; do
if grep -qiE "$pattern" "$generator_file"; then
print_error "发现硬编码类型推断: $pattern"
errors=$((errors + 1))
fi
done
if [ $errors -eq 0 ]; then
print_success "类型一致性检查通过"
else
print_error "发现 $errors 个类型一致性问题"
return 1
fi
}
# 运行完整验证
run_full_validation() {
print_header
local total_errors=0
check_prerequisites || total_errors=$((total_errors + 1))
validate_swagger || total_errors=$((total_errors + 1))
validate_file_structure || total_errors=$((total_errors + 1))
validate_code_quality || total_errors=$((total_errors + 1))
validate_naming_conventions || total_errors=$((total_errors + 1))
validate_type_consistency || total_errors=$((total_errors + 1))
echo ""
echo "=================================================="
if [ $total_errors -eq 0 ]; then
print_success "所有检查通过!代码符合 Augment 规范。"
exit 0
else
print_error "验证失败,发现 $total_errors 个问题。"
echo ""
print_info "请参考以下文档进行修复:"
echo " - 代码生成规范: AUGMENT_CODE_GENERATION_STANDARDS.md"
echo " - 快速参考指南: QUICK_REFERENCE.md"
echo " - 代码审查清单: CODE_REVIEW_CHECKLIST.md"
exit 1
fi
}
# 显示帮助信息
show_help() {
echo "Augment 代码生成规范验证脚本"
echo ""
echo "用法:"
echo " $0 [选项]"
echo ""
echo "选项:"
echo " all, -a, --all 运行所有验证检查(默认)"
echo " swagger, -s, --swagger 只验证 swagger.json"
echo " files, -f, --files 只验证文件结构"
echo " quality, -q, --quality 只验证代码质量"
echo " naming, -n, --naming 只验证命名规范"
echo " types, -t, --types 只验证类型一致性"
echo " help, -h, --help 显示此帮助信息"
echo ""
echo "示例:"
echo " $0 # 运行所有检查"
echo " $0 swagger # 只验证 swagger.json"
echo " $0 --quality # 只验证代码质量"
}
# 主函数
main() {
case "${1:-all}" in
all|-a|--all)
run_full_validation
;;
swagger|-s|--swagger)
print_header
check_prerequisites
validate_swagger
print_success "Swagger 验证完成"
;;
files|-f|--files)
print_header
validate_file_structure
print_success "文件结构验证完成"
;;
quality|-q|--quality)
print_header
validate_code_quality
print_success "代码质量验证完成"
;;
naming|-n|--naming)
print_header
validate_naming_conventions
print_success "命名规范验证完成"
;;
types|-t|--types)
print_header
validate_type_consistency
print_success "类型一致性验证完成"
;;
help|-h|--help)
show_help
;;
*)
print_error "未知选项: $1"
show_help
exit 1
;;
esac
}
# 执行主函数
main "$@"