chore: 开发脚本自动进入虚拟环境

This commit is contained in:
Nixevol
2026-07-13 16:28:08 +08:00
parent 182f0e5612
commit 3c74f03e7e
4 changed files with 57 additions and 10 deletions
+2 -4
View File
@@ -40,17 +40,15 @@ artveris/
### 一键脚本(推荐) ### 一键脚本(推荐)
```bash ```bash
python -m venv .venv
.\.venv\Scripts\activate # Windows;macOS/Linux 为 source .venv/bin/activate
python -m pip install -r scripts/requirements.txt
python scripts/dev.py --install # 首次:安装依赖 + 启动三端 python scripts/dev.py --install # 首次:安装依赖 + 启动三端
python scripts/dev.py # 日常启动 python scripts/dev.py # 日常启动
``` ```
- `dev.py` 会先检查当前是否已在虚拟环境中;若没有,会自动复用项目根目录的 `.venv`/`venv`,不存在则创建 `.venv` 并安装 `scripts/requirements.txt`,随后用虚拟环境 Python 重新执行脚本。
- **全新检出(无 `.env`)**:自动进入安装向导,浏览器打开 `http://localhost:5173` 按页面提示填写 MySQL/Redis 与管理员账号,提交后自动建库、迁移、初始化并写入根 `.env`,随后转入正常开发模式。 - **全新检出(无 `.env`)**:自动进入安装向导,浏览器打开 `http://localhost:5173` 按页面提示填写 MySQL/Redis 与管理员账号,提交后自动建库、迁移、初始化并写入根 `.env`,随后转入正常开发模式。
- **已有 `.env`**:直接构建共享包、同步数据库并启动三端。加 `--no-seed` 跳过种子数据,`--stop` 停止全部进程。 - **已有 `.env`**:直接构建共享包、同步数据库并启动三端。加 `--no-seed` 跳过种子数据,`--stop` 停止全部进程。
若 PowerShell 阻止激活虚拟环境,先执行 `Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`。更多细节见 `scripts/README.md`。 更多细节见 `scripts/README.md`。
### 手动命令 ### 手动命令
+5
View File
@@ -348,3 +348,8 @@
- 在开发分支 `codex/compress-prisma-migrations` 上将 `apps/api/prisma/migrations` 从 9 段历史迁移压缩为单个 `20260713090000_init` 初始迁移,迁移 SQL 由当前 `schema.prisma` 通过 Prisma 5.22 `migrate diff --from-empty` 生成。 - 在开发分支 `codex/compress-prisma-migrations` 上将 `apps/api/prisma/migrations` 从 9 段历史迁移压缩为单个 `20260713090000_init` 初始迁移,迁移 SQL 由当前 `schema.prisma` 通过 Prisma 5.22 `migrate diff --from-empty` 生成。
- 此压缩仅适用于正式安装前/无既有生产库迁移历史的交付包;若已有线上库执行过旧迁移,不应直接套用该分支,否则 `_prisma_migrations` 历史会与代码迁移目录不一致。 - 此压缩仅适用于正式安装前/无既有生产库迁移历史的交付包;若已有线上库执行过旧迁移,不应直接套用该分支,否则 `_prisma_migrations` 历史会与代码迁移目录不一致。
- 已用临时 `DATABASE_URL` 验证 `prisma validate` 通过,并确认新初始迁移与当前 schema 生成结果一致。 - 已用临时 `DATABASE_URL` 验证 `prisma validate` 通过,并确认新初始迁移与当前 schema 生成结果一致。
## 2026-07-13 · dev.py 自动进入虚拟环境
- `scripts/dev.py` 在导入 `_lib` 前先执行标准库自举:若当前 Python 已在虚拟环境中则继续;否则优先复用项目根目录 `.venv`/`venv`,不存在时自动创建 `.venv` 并安装 `scripts/requirements.txt`。
- 自举完成后通过虚拟环境 Python 重新执行原命令,避免用户手动激活 PowerShell venv;如需调试跳过该逻辑,可设置 `ARTVERIS_SKIP_VENV=1`。
+6 -6
View File
@@ -7,16 +7,16 @@
首次运行前: 首次运行前:
```bash ```bash
python -m venv .venv python scripts/dev.py --install
.\.venv\Scripts\activate python scripts/dev.py
python -m pip install -r scripts/requirements.txt
``` ```
如果 PowerShell 阻止激活脚本,可在当前终端临时放行后再激活: `dev.py` 会先检查当前 Python 是否已在虚拟环境中;若没有,会自动复用项目根目录的 `.venv`/`venv`;若两者都不存在,会创建 `.venv` 并安装 `scripts/requirements.txt`,随后用虚拟环境 Python 重新执行当前命令。
```bash ```bash
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass python scripts/dev.py --install # 首次:安装 pnpm 依赖 + 启动
.\.venv\Scripts\activate python scripts/dev.py --no-seed # 跳过种子数据
python scripts/dev.py --install-mode # 调试首次安装向导
``` ```
| 脚本 | 作用 | | 脚本 | 作用 |
+44
View File
@@ -26,12 +26,56 @@
""" """
from __future__ import annotations from __future__ import annotations
import os
import re import re
import subprocess
import sys import sys
import threading import threading
import time import time
from pathlib import Path from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parent.parent
def in_virtualenv() -> bool:
return (
bool(os.environ.get("VIRTUAL_ENV"))
or sys.prefix != getattr(sys, "base_prefix", sys.prefix)
or hasattr(sys, "real_prefix")
)
def venv_python(venv_dir: Path) -> Path:
return venv_dir / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
def ensure_virtualenv() -> None:
"""Re-run dev.py inside the project virtualenv before importing script dependencies."""
if in_virtualenv() or os.environ.get("ARTVERIS_SKIP_VENV") == "1":
return
existing = next(
(path for path in (ROOT_DIR / ".venv", ROOT_DIR / "venv") if venv_python(path).exists()),
None,
)
venv_dir = existing or (ROOT_DIR / ".venv")
created = existing is None
if existing is None:
print(f"[artveris] 未检测到虚拟环境,正在创建 {venv_dir} ...")
subprocess.run([sys.executable, "-m", "venv", str(venv_dir)], cwd=str(ROOT_DIR), check=True)
python = venv_python(venv_dir)
requirements = ROOT_DIR / "scripts" / "requirements.txt"
if created and requirements.exists():
print(f"[artveris] 使用虚拟环境安装脚本依赖:{requirements}")
subprocess.run([str(python), "-m", "pip", "install", "-r", str(requirements)], cwd=str(ROOT_DIR), check=True)
print(f"[artveris] 切换到虚拟环境 Python:{python}")
os.execv(str(python), [str(python), str(Path(__file__).resolve()), *sys.argv[1:]])
ensure_virtualenv()
from _lib import ( from _lib import (
ROOT, ROOT,
create_kill_job, create_kill_job,