You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iTi-Flask/scripts/iti.cmd

138 lines
3.4 KiB
Batchfile

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

@echo off
setlocal enabledelayedexpansion
set "SCRIPT_DIR=%~dp0"
set "ROOT_DIR=%SCRIPT_DIR%.."
pushd "%ROOT_DIR%" >nul
set "ROOT_DIR=%CD%"
set "PROJECT_VENV=%ROOT_DIR%\.venv"
if defined VIRTUAL_ENV (
if /I not "%VIRTUAL_ENV%"=="%PROJECT_VENV%" (
if /I not "%VIRTUAL_ENV%"=="%PROJECT_VENV%\" set "VIRTUAL_ENV="
)
)
set "COMMAND=%~1"
if "%COMMAND%"=="" set "COMMAND=help"
shift /1
if "%COMMAND%"=="help" goto help
if "%COMMAND%"=="-h" goto help
if "%COMMAND%"=="--help" goto help
if "%COMMAND%"=="install" goto install
if "%COMMAND%"=="test" goto test
if "%COMMAND%"=="check" goto check
if "%COMMAND%"=="serve" goto serve
if "%COMMAND%"=="migrate" goto migrate
if "%COMMAND%"=="migration" goto migration
if "%COMMAND%"=="heads" goto heads
if "%COMMAND%"=="current" goto current
if "%COMMAND%"=="make-app" goto make_app
if "%COMMAND%"=="make-system-app" goto make_system_app
echo 未知命令:%COMMAND% 1>&2
echo. 1>&2
goto help_error
:help
echo iTi-Flask 开发脚本
echo.
echo 用法:
echo scripts\iti.cmd ^<命令^> [参数]
echo.
echo 常用命令:
echo help 显示帮助
echo install 安装开发依赖uv sync --extra dev
echo test 运行测试uv run pytest -q
echo check 运行测试和 mypy
echo serve [端口] 启动框架示例应用,默认 8000
echo migrate 执行当前仓库 Alembic upgrade head
echo migration ^<说明^> 生成 migration说明建议以作者名开头
echo heads 查看 Alembic heads
echo current 查看当前 Alembic 版本
echo make-app ^<目录^> [包名] 从 copier-template 生成业务项目
echo make-system-app ^<目录^> [包名] 生成带 iti-system 的业务项目
echo.
echo 示例:
echo scripts\iti.cmd install
echo scripts\iti.cmd test
echo scripts\iti.cmd serve 8000
echo scripts\iti.cmd make-app ..\hsyh-erp hsyh_erp
echo scripts\iti.cmd make-system-app ..\hsyh-mes-phase2 hsyh_mes_phase2
popd >nul
exit /b 0
:help_error
call :help
exit /b 2
:install
uv sync --extra dev
goto end
:test
uv run pytest -q
goto end
:check
uv run pytest -q
if errorlevel 1 goto end
uv run mypy
goto end
:serve
set "PORT=%~1"
if "%PORT%"=="" set "PORT=8000"
uv run uvicorn iti.app:create_app --factory --reload --port "%PORT%"
goto end
:migrate
uv run alembic upgrade head
goto end
:migration
set "MESSAGE=%~1"
if "%MESSAGE%"=="" (
echo 缺少 migration 说明。示例scripts\iti.cmd migration "alice add order table" 1>&2
exit /b 2
)
uv run alembic revision --autogenerate -m "%MESSAGE%"
goto end
:heads
uv run alembic heads
goto end
:current
uv run alembic current
goto end
:make_app
set "INCLUDE_SYSTEM=false"
goto make_project
:make_system_app
set "INCLUDE_SYSTEM=true"
goto make_project
:make_project
set "TARGET=%~1"
set "PACKAGE=%~2"
if "%TARGET%"=="" (
echo 缺少目标目录。示例scripts\iti.cmd make-app ..\my-app my_app 1>&2
exit /b 2
)
if "%PACKAGE%"=="" (
for %%I in ("%TARGET%") do set "PACKAGE=%%~nxI"
set "PACKAGE=!PACKAGE:-=_!"
)
for %%I in ("%TARGET%") do set "PROJECT_NAME=%%~nxI"
uvx copier copy --defaults "%CD%\copier-template" "%TARGET%" -d project_name="!PROJECT_NAME!" -d project_slug="!PACKAGE!" -d include_system="%INCLUDE_SYSTEM%"
goto end
:end
set "EXIT_CODE=%ERRORLEVEL%"
popd >nul
exit /b %EXIT_CODE%