#!/usr/bin/env sh set -eu ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) cd "$ROOT_DIR" export UV_NO_SOURCES_PACKAGE=iti-flask 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 framework-sync 同步 iTi-Flask{% if include_system %} / iTi-System{% endif %} 依赖 template-check 检查 Copier 模板是否有更新 template-update 按 Copier 模板更新项目骨架 test 运行测试:uv run --extra dev pytest -q serve [环境] [端口] 本地启动,默认 dev / 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 ./app.sh serve test 8000 APP_ENV=prod ./app.sh migrate ./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 ;; framework-sync) uv sync --extra dev --upgrade-package iti-flask{% if include_system %} --upgrade-package iti-system{% endif %} ;; template-check) uvx copier update --defaults --pretend --vcs-ref HEAD "$ROOT_DIR" ;; template-update) uvx copier update --defaults --vcs-ref HEAD "$ROOT_DIR" ;; test) uv run --extra dev pytest -q ;; serve) env_name=${APP_ENV:-${ITI_ENV:-dev}} port=8000 if [ $# -gt 0 ]; then case "$1" in dev|test|prod|default) env_name=$1 port=${2:-8000} ;; ''|*[!0-9]*) env_name=$1 port=${2:-8000} ;; *) port=$1 ;; esac fi if [ "$env_name" = default ]; then env_name=dev fi APP_ENV="$env_name" ITI_ENV="$env_name" uv run uvicorn app:app --reload --port "$port" ;; migrate) uv run alembic -c migrations/alembic.ini 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 -c migrations/alembic.ini revision --autogenerate -m "$message" ;; heads) uv run alembic -c migrations/alembic.ini heads ;; current) uv run alembic -c migrations/alembic.ini 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 -c migrations/alembic.ini 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 -c migrations/alembic.ini 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