swagger_generator_flutter/example/as_dev_dependency/generate_api.sh

70 lines
2.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

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
# Swagger API 代码生成脚本
# 用于示例项目的 API 代码生成
set -e # 遇到错误立即退出
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🚀 Swagger API 代码生成器"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# 步骤 1: 生成 API 代码
echo "📝 步骤 1/4: 生成 API 代码..."
dart run swagger_generator_flutter generate --all
if [ $? -ne 0 ]; then
echo "❌ API 代码生成失败!"
exit 1
fi
echo "✅ API 代码生成成功"
echo ""
# 步骤 2: 运行 build_runner
echo "🔧 步骤 2/4: 运行 build_runner..."
dart run build_runner build --delete-conflicting-outputs
if [ $? -ne 0 ]; then
echo "⚠️ build_runner 执行失败(如果是首次运行,可能需要先修复基础类型)"
echo " 请检查 lib/common/api_response.dart 和 paged_response.dart"
else
echo "✅ build_runner 执行成功"
fi
echo ""
# 步骤 3: 格式化代码
echo "📐 步骤 3/4: 格式化代码..."
dart format lib/generated
echo "✅ 代码格式化完成"
echo ""
# 步骤 4: 分析代码
echo "🔍 步骤 4/4: 分析代码..."
dart analyze lib/generated --fatal-infos
if [ $? -ne 0 ]; then
echo "⚠️ 代码分析发现问题,请检查"
else
echo "✅ 代码分析通过"
fi
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✨ 代码生成完成!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📂 生成的文件位置:"
echo " lib/generated/api/"
echo " lib/generated/api_models/"
echo ""
echo "📚 下一步:"
echo " 1. 查看生成的代码lib/generated/"
echo " 2. 在 main.dart 中取消注释 import 语句"
echo " 3. 运行应用flutter run"
echo ""