#!/usr/bin/env sh set -eu ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) cd "$ROOT_DIR" PROJECT_VENV="$ROOT_DIR/.venv" case "${VIRTUAL_ENV:-}" in ""|"$PROJECT_VENV"|"$PROJECT_VENV/") ;; *) unset VIRTUAL_ENV ;; esac show_help() { cat <<'EOF' {{ project_name }} 项目脚本 用法: ./app.sh <命令> [参数] 常用命令: help 显示帮助 install 安装开发依赖:uv sync --extra dev test 运行测试:uv run pytest -q serve [端口] 本地启动,默认 8000 migrate 执行 Alembic upgrade head migration <说明> 生成 migration,说明建议以作者名开头 heads 查看 Alembic heads current 查看当前 Alembic 版本 {% if include_system %} system-sync 同步 iti-system migration 到当前项目 system-seed 初始化 / 更新系统 seed init-system system-sync + migrate + system-seed {% endif %} init install + migrate{% if include_system %} + system-seed{% endif %} 示例: ./app.sh install ./app.sh serve 8000 ./app.sh migration "alice add order table" {% if include_system %} ./app.sh init-system {% endif %} EOF } command=${1:-help} shift || true case "$command" in help|-h|--help) show_help ;; install) uv sync --extra dev ;; test) uv run pytest -q ;; serve) port=${1:-8000} uv run uvicorn app:app --reload --port "$port" ;; migrate) uv run alembic upgrade head ;; migration) message=${1:-} if [ -z "$message" ]; then echo "缺少 migration 说明。示例:./app.sh migration \"alice add order table\"" >&2 exit 2 fi uv run alembic revision --autogenerate -m "$message" ;; heads) uv run alembic heads ;; current) uv run alembic current ;; {% if include_system %} system-sync) uv run iti-system migrations sync --target migrations/versions ;; system-seed) uv run iti-system seed system app:app ;; init-system) uv run iti-system migrations sync --target migrations/versions uv run alembic upgrade head uv run iti-system seed system app:app ;; {% endif %} init) uv sync --extra dev {% if include_system %} uv run iti-system migrations sync --target migrations/versions {% endif %} uv run alembic upgrade head {% if include_system %} uv run iti-system seed system app:app {% endif %} ;; *) echo "未知命令:$command" >&2 echo >&2 show_help >&2 exit 2 ;; esac