chore: cli tools

main
NoahLan 1 week ago
parent ab6b2197da
commit 631e06f70b

@ -96,6 +96,9 @@ seed 必须可重复执行。
- 安装开发依赖:`uv sync --extra dev`
- 运行测试:`uv run pytest -q`
- 通过外部工具安装开发依赖:`iticli install`
- 通过外部工具运行测试:`iticli test`
- 发布系统包:`iticli release`
- 同步 migration 到宿主项目:`uv run iti-system migrations sync --target migrations/versions`
- 初始化或更新宿主项目 seed`uv run iti-system seed system app:app`

@ -86,8 +86,8 @@ seed 会写入默认角色、默认管理员、系统菜单、系统配置、系
## 本地开发
```bash
uv sync --extra dev
uv run pytest -q
iticli install
iticli test
```
## 发布
@ -96,13 +96,13 @@ uv run pytest -q
它不跟随 `iTi-Flask` 自动发版。
```bash
./iti-system.sh release
./iti-system.sh release v0.2.4
iticli release
iticli release v0.2.4
```
Windows:
```bat
iti-system.cmd release
iti-system.cmd release v0.2.4
iticli release
iticli release v0.2.4
```

@ -1,6 +0,0 @@
@echo off
setlocal
set "SCRIPT_DIR=%~dp0"
call "%SCRIPT_DIR%scripts\iti-system.cmd" %*
exit /b %ERRORLEVEL%

@ -1,5 +0,0 @@
#!/usr/bin/env sh
set -eu
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec sh "$SCRIPT_DIR/scripts/iti-system.sh" "$@"

@ -1,77 +0,0 @@
@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%"=="release" goto release
echo 未知命令:%COMMAND% 1>&2
echo. 1>&2
goto help_error
:help
echo iTi-System 开发脚本
echo.
echo 用法:
echo iti-system.cmd ^<命令^> [参数]
echo.
echo 常用命令:
echo help 显示帮助
echo install 安装开发依赖uv sync --extra dev
echo test 运行测试uv run pytest -q
echo release [版本] 发布系统包:测试、改版本、提交、打 tag、推送
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
:release
set "SHELL_CMD="
where sh >nul 2>nul && set "SHELL_CMD=sh"
if not defined SHELL_CMD (
where bash >nul 2>nul && set "SHELL_CMD=bash"
)
if not defined SHELL_CMD (
echo 找不到 sh 或 bash无法运行 release 脚本 1>&2
exit /b 1
)
if "%~1"=="" (
"%SHELL_CMD%" scripts/release.sh
) else (
"%SHELL_CMD%" scripts/release.sh %*
)
goto end
:end
set "EXIT_CODE=%ERRORLEVEL%"
popd >nul
exit /b %EXIT_CODE%

@ -1,53 +0,0 @@
#!/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'
iTi-System 开发脚本
用法:
./iti-system.sh <命令> [参数]
常用命令:
help 显示帮助
install 安装开发依赖uv sync --extra dev
test 运行测试uv run pytest -q
release [版本] 发布系统包:测试、改版本、提交、打 tag、推送
EOF
}
command=${1:-help}
shift || true
case "$command" in
help|-h|--help)
show_help
;;
install)
uv sync --extra dev
;;
test)
uv run pytest -q
;;
release)
sh scripts/release.sh "$@"
;;
*)
echo "未知命令:$command" >&2
echo >&2
show_help >&2
exit 2
;;
esac

@ -1,213 +0,0 @@
#!/usr/bin/env sh
set -eu
ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT_DIR"
ABOUT_FILE="$ROOT_DIR/iti_system/__about__.py"
PYPROJECT_FILE="$ROOT_DIR/pyproject.toml"
README_FILE="$ROOT_DIR/README.md"
die() {
printf '%s\n' "$*" >&2
exit 1
}
parse_version() {
raw=$1
version=${raw#v}
old_ifs=$IFS
IFS=.
set -- $version
IFS=$old_ifs
if [ "$#" -ne 3 ]; then
die "invalid version: $raw"
fi
for part in "$@"; do
case $part in
''|*[!0-9]*)
die "invalid version: $raw"
;;
esac
done
printf '%s %s %s\n' "$1" "$2" "$3"
}
normalize_version() {
set -- $(parse_version "$1")
printf '%s.%s.%s\n' "$1" "$2" "$3"
}
bump_patch() {
set -- $(parse_version "$1")
printf '%s.%s.%s\n' "$1" "$2" "$(( $3 + 1 ))"
}
version_is_newer() {
current_version=$1
target_version=$2
current_parts=$(parse_version "$current_version")
set -- $current_parts
current_major=$1
current_minor=$2
current_patch=$3
target_parts=$(parse_version "$target_version")
set -- $target_parts
target_major=$1
target_minor=$2
target_patch=$3
if [ "$target_major" -gt "$current_major" ]; then
return 0
fi
if [ "$target_major" -lt "$current_major" ]; then
return 1
fi
if [ "$target_minor" -gt "$current_minor" ]; then
return 0
fi
if [ "$target_minor" -lt "$current_minor" ]; then
return 1
fi
[ "$target_patch" -gt "$current_patch" ]
}
read_current_version() {
current_version=$(sed -n 's/^__version__[[:space:]]*=[[:space:]]*"\([^"]*\)".*$/\1/p' "$ABOUT_FILE" | sed -n '1p')
[ -n "$current_version" ] || die "version not found in $ABOUT_FILE"
printf '%s\n' "$current_version"
}
replace_first_exact_line() {
file=$1
old_line=$2
new_line=$3
tmp_file=$(mktemp)
if awk -v old="$old_line" -v new="$new_line" '
BEGIN { done = 0 }
{
if (!done && $0 == old) {
print new
done = 1
} else {
print
}
}
END { if (!done) exit 1 }
' "$file" >"$tmp_file"; then
mv "$tmp_file" "$file"
else
rm -f "$tmp_file"
die "line not updated in $file"
fi
}
replace_first_line_with_prefix() {
file=$1
prefix=$2
new_line=$3
tmp_file=$(mktemp)
if awk -v prefix="$prefix" -v new="$new_line" '
BEGIN { done = 0 }
index($0, prefix) == 1 && !done {
print new
done = 1
next
}
{ print }
END { if (!done) exit 1 }
' "$file" >"$tmp_file"; then
mv "$tmp_file" "$file"
else
rm -f "$tmp_file"
die "line not updated in $file"
fi
}
write_about_version() {
target_version=$1
replace_first_line_with_prefix "$ABOUT_FILE" "__version__ = " "__version__ = \"$target_version\""
}
write_pyproject_framework_tag() {
target_version=$1
replace_first_line_with_prefix "$PYPROJECT_FILE" 'iti-flask = { git = "https://git.noahlan.cn/iti-framework/iTi-Flask.git", tag = "' "iti-flask = { git = \"https://git.noahlan.cn/iti-framework/iTi-Flask.git\", tag = \"v$target_version\" }"
}
write_readme_dependency_tags() {
target_version=$1
replace_first_line_with_prefix "$README_FILE" ' "iti-flask @ git+https://git.noahlan.cn/iti-framework/iTi-Flask.git@v' ' "iti-flask @ git+https://git.noahlan.cn/iti-framework/iTi-Flask.git@v'"$target_version"'",'
replace_first_line_with_prefix "$README_FILE" ' "iti-system @ git+https://git.noahlan.cn/iti-framework/iTi-System.git@v' ' "iti-system @ git+https://git.noahlan.cn/iti-framework/iTi-System.git@v'"$target_version"'",'
}
write_readme_release_examples() {
target_version=$1
replace_first_line_with_prefix "$README_FILE" './iti-system.sh release v' "./iti-system.sh release v$target_version"
replace_first_line_with_prefix "$README_FILE" 'iti-system.cmd release v' "iti-system.cmd release v$target_version"
}
ensure_clean_tree() {
if [ -n "$(git status --porcelain)" ]; then
die "working tree is not clean"
fi
}
ensure_branch() {
if branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null); then
[ -n "$branch" ] || die "release requires a branch checkout"
printf '%s\n' "$branch"
else
die "release requires a branch checkout"
fi
}
ensure_tag_absent() {
tag=$1
if git rev-parse --verify --quiet "refs/tags/$tag" >/dev/null; then
die "tag already exists: $tag"
fi
}
main() {
version_arg=${1:-}
current_version=$(read_current_version)
if [ -n "$version_arg" ]; then
target_version=$(normalize_version "$version_arg")
else
target_version=$(bump_patch "$current_version")
fi
target_tag="v$target_version"
if [ "$target_version" = "$current_version" ]; then
die "version already set to $target_version"
fi
if ! version_is_newer "$current_version" "$target_version"; then
die "target version must be newer than $current_version"
fi
ensure_clean_tree
branch=$(ensure_branch)
ensure_tag_absent "$target_tag"
uv run pytest -q
write_about_version "$target_version"
write_pyproject_framework_tag "$target_version"
write_readme_dependency_tags "$target_version"
write_readme_release_examples "$target_version"
git add iti_system/__about__.py pyproject.toml README.md
git commit -m "chore: release $target_tag"
git tag -a "$target_tag" -m "release $target_tag"
git push origin "$branch" "$target_tag"
printf 'released %s\n' "$target_tag"
}
main "$@"
Loading…
Cancel
Save