build: 添加 Windows 打包方案

- 更新 PyInstaller spec 配置
- 简化 build.sh 构建脚本
- 更新 build.bat Windows 打包脚本
- 添加 docs/BUILD.md 打包说明文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
congsh
2026-02-12 15:59:23 +08:00
parent e853161975
commit 0ce1d71a90
4 changed files with 223 additions and 189 deletions

View File

@@ -1,74 +1,136 @@
# CutThenThink 打包指南
## 方式一:使用 build.sh 脚本(推荐)
## Windows 打包
在您的本地环境(有管理员权限)中运行:
### 方法一:使用批处理脚本(推荐)
```bash
cd /path/to/CutThenThink
bash build.sh
```cmd
# 双击运行
build.bat
```
---
### 方法二:手动命令
## 方式二:手动打包
### 1. 安装 PyInstaller
```bash
# 使用 pipx推荐
pipx install pyinstaller
# 或使用系统包管理器
sudo apt install python3-pyinstaller
```
### 2. 安装项目依赖
```bash
```cmd
# 1. 安装依赖
pip install -r requirements.txt
# 2. 安装 PyInstaller
pip install pyinstaller
# 3. 构建
python -m PyInstaller CutThenThink.spec --clean
```
### 3. 执行打包
### 输出位置
```
dist/
└── CutThenThink/
├── CutThenThink.exe # 主程序
└── _internal/ # 运行时依赖
```
### 可选:安装 OCR 支持
```cmd
pip install -r requirements-ocr.txt
```
## Linux/macOS 打包
### 使用 Shell 脚本
```bash
python3 -m PyInstaller \
--name "CutThenThink" \
--windowed \
--onefile \
--add-data "src:src" \
--hidden-import=PyQt6.QtCore \
--hidden-import=PyQt6.QtGui \
--hidden-import=PyQt6.QtWidgets \
--hidden-import=sqlalchemy \
src/main.py
# 添加执行权限
chmod +x build.sh
# 运行
./build.sh
```
### 4. 测试运行
### 手动命令
```bash
./dist/CutThenThink
# 1. 安装依赖
pip install -r requirements.txt
# 2. 安装 PyInstaller
pip install pyinstaller
# 3. 构建
python -m PyInstaller CutThenThink.spec --clean
```
---
## 打包说明
## 打包参数说明
### PyInstaller 配置
| 参数 | 说明 |
|------|------|
| `--name` | 应用名称 |
| `--windowed` | 无控制台窗口 |
| `--onefile` | 单文件打包 |
| `--add-data` | 添加数据文件(源代码) |
| `--hidden-import` | 隐式导入模块 |
- `--onefile`: 打包成单个 EXE
- `--windowed`: 无控制台窗口
- `--upx`: 使用 UPX 压缩(减小体积)
- `--clean`: 清理旧的构建
---
### 包含的隐式导入
## 打包后
- PyQt6.QtCore
- PyQt6.QtGui
- PyQt6.QtWidgets
- yaml
可执行文件位置:`dist/CutThenThink`
### 排除的模块
分发时建议:
1.`dist/CutThenThink` 打包为 tar.gz
2. 创建安装脚本
3. 包含 README 说明
- test, tests, pytest
## 首次运行配置
程序首次运行时会创建配置文件:
**Windows**: `%USERPROFILE%\.cutthenthink\config.yaml`
**Linux/macOS**: `~/.cutthenthink/config.yaml`
```yaml
upload:
provider: custom
endpoint: https://your-server.com/upload
api_key: your-api-key
auto_copy: true
screenshot:
format: png
save_path: ~/Pictures/Screenshots
hotkeys:
capture: Ctrl+Shift+A
region: Ctrl+Shift+R
upload: Ctrl+Shift+U
ocr:
enabled: false
auto_copy: false
```
## 故障排除
### PyInstaller 构建失败
1. 确保 PyInstaller 已安装:`pip list | grep pyinstaller`
2. 检查 Python 版本:需要 Python 3.8+
3. 清理缓存:删除 `build/``dist/` 目录后重试
### 运行时错误
1. 检查是否有防火墙阻止
2. 检查 OCR 功能是否可选安装
3. 查看日志文件(如果有)
## 体积优化建议
当前打包体积预估:
- 核心依赖:~50MB
- 可选 OCR+10MB
如需进一步减小体积:
1. 使用 `--exclude-module` 排除不需要的模块
2. 启用 UPX 压缩(已启用)
3. 使用虚拟环境减少依赖