|
| 1 | +# PromptComposer 打包与发布指南 |
| 2 | + |
| 3 | +## 📦 打包说明 |
| 4 | + |
| 5 | +### 环境要求 |
| 6 | +- Python 3.8+ |
| 7 | +- PyInstaller 6.0+ |
| 8 | + |
| 9 | +### 快速打包 |
| 10 | + |
| 11 | +```bash |
| 12 | +# 1. 进入项目目录 |
| 13 | +cd PromptComposer |
| 14 | + |
| 15 | +# 2. 安装打包工具(如未安装) |
| 16 | +pip install pyinstaller |
| 17 | + |
| 18 | +# 3. 执行打包命令 |
| 19 | +pyinstaller prompt_composer.spec --clean |
| 20 | +``` |
| 21 | + |
| 22 | +### 输出位置 |
| 23 | +- 可执行文件:`dist/PromptComposer.exe` |
| 24 | +- 打包日志:`build/prompt_composer/` |
| 25 | +- 警告信息:`build/prompt_composer/warn-prompt_composer.txt` |
| 26 | + |
| 27 | +## 🔧 关键技术方案 |
| 28 | + |
| 29 | +### 1. 用户数据持久化 |
| 30 | + |
| 31 | +**问题**:PyInstaller 单文件模式下,所有资源文件被解压到临时目录(如 `AppData\Local\Temp\_MEI323402\`),程序退出后自动清理,导致用户保存的模板丢失。 |
| 32 | + |
| 33 | +**解决方案**:根据运行环境动态选择模板目录 |
| 34 | +- **开发环境**(`python prompt_composer.py`):使用脚本所在目录的 `templates/` 文件夹 |
| 35 | +- **打包环境**(`PromptComposer.exe`):使用 exe 文件所在目录的 `templates/` 文件夹 |
| 36 | + |
| 37 | +```python |
| 38 | +if getattr(sys, 'frozen', False): |
| 39 | + # 打包后的 exe 环境:使用 exe 文件所在目录 |
| 40 | + exe_dir = os.path.dirname(sys.executable) |
| 41 | + self.templates_dir = os.path.join(exe_dir, "templates") |
| 42 | +else: |
| 43 | + # 开发环境 |
| 44 | + self.templates_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") |
| 45 | +``` |
| 46 | + |
| 47 | +### 2. 用户数据位置 |
| 48 | + |
| 49 | +用户的模板保存在 exe 同目录下: |
| 50 | +``` |
| 51 | +你的目录/ |
| 52 | +├── PromptComposer.exe |
| 53 | +└── templates/ |
| 54 | + ├── demo.md # 内置示例 |
| 55 | + ├── 模板1.md # 用户保存的模板 |
| 56 | + └── 模板2.md |
| 57 | +``` |
| 58 | + |
| 59 | +用户可以通过以下方式访问: |
| 60 | +1. **在程序中**:保存或加载模板时自动使用此目录 |
| 61 | +2. **手动访问**:直接打开 exe 所在文件夹的 `templates` 子文件夹 |
| 62 | +3. **快速打开**:在 PowerShell 中运行 `explorer (Split-Path (Get-Command .\PromptComposer.exe).Source)\templates` |
| 63 | + |
| 64 | +### 3. 打包配置文件(prompt_composer.spec) |
| 65 | + |
| 66 | +关键配置说明: |
| 67 | + |
| 68 | +```python |
| 69 | +a = Analysis( |
| 70 | + ['prompt_composer.py'], |
| 71 | + datas=[('templates', 'templates')], # 将初始模板打包进 exe |
| 72 | + # ... |
| 73 | +) |
| 74 | + |
| 75 | +exe = EXE( |
| 76 | + # ... |
| 77 | + name='PromptComposer', |
| 78 | + console=False, # 不显示控制台窗口(GUI 应用) |
| 79 | + upx=True, # 启用 UPX 压缩减小文件体积 |
| 80 | + # ... |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +**注意**:`datas` 中的 `templates` 仅用于初始模板(如 demo.md),用户保存的模板存储在 AppData 目录。 |
| 85 | + |
| 86 | +## 🚀 发布流程 |
| 87 | + |
| 88 | +### 1. 测试开发版本 |
| 89 | +```bash |
| 90 | +python prompt_composer.py |
| 91 | +``` |
| 92 | + |
| 93 | +### 2. 打包测试 |
| 94 | +```bash |
| 95 | +pyinstaller prompt_composer.spec --clean |
| 96 | +dist/PromptComposer.exe # 启动测试 |
| 97 | +``` |
| 98 | + |
| 99 | +### 3. 验证用户数据持久化 |
| 100 | +- 启动程序 |
| 101 | +- 保存一个测试模板 |
| 102 | +- 关闭程序 |
| 103 | +- 再次启动程序 |
| 104 | +- 确认模板仍然存在 |
| 105 | + |
| 106 | +### 4. 创建压缩包(可选) |
| 107 | +```powershell |
| 108 | +# PowerShell |
| 109 | +Compress-Archive -Path "dist\PromptComposer.exe" -DestinationPath "PromptComposer-v1.0.1-Windows-x64.zip" |
| 110 | +``` |
| 111 | + |
| 112 | +### 5. 提交代码 |
| 113 | +```bash |
| 114 | +git add . |
| 115 | +git commit -m "fix: 修复 exe 模板持久化问题,使用 AppData 存储" |
| 116 | +git tag -a v1.0.1 -m "Release v1.0.1: 修复模板持久化问题" |
| 117 | +git push origin main |
| 118 | +git push origin v1.0.1 |
| 119 | +``` |
| 120 | + |
| 121 | +### 6. 创建 GitHub Release |
| 122 | +1. 访问 https://github.com/<username>/DevToolkit/releases/new |
| 123 | +2. 选择标签:`v1.0.1` |
| 124 | +3. 填写标题和说明 |
| 125 | +4. 上传 `PromptComposer.exe` 或压缩包 |
| 126 | +5. 发布 |
| 127 | + |
| 128 | +## 🐛 常见问题 |
| 129 | + |
| 130 | +### Q1: 为什么打包后文件这么大(11 MB)? |
| 131 | +A: 单文件打包包含了完整的 Python 解释器和所有依赖库(包括 Tkinter 和 Tcl/Tk)。 |
| 132 | + |
| 133 | +**可选优化方案**: |
| 134 | +- 使用目录打包模式(去掉 `--onefile`):文件更小但需要多个文件 |
| 135 | +- 启用 UPX 压缩(已启用) |
| 136 | +- 使用虚拟环境减少不必要的依赖 |
| 137 | + |
| 138 | +### Q2: 如何卸载或重置程序? |
| 139 | +A: 删除以下内容: |
| 140 | +1. 可执行文件:`PromptComposer.exe` |
| 141 | +2. 用户数据:exe 同目录下的 `templates/` 文件夹 |
| 142 | + |
| 143 | +### Q3: 如何备份模板? |
| 144 | +A: 复制 exe 同目录下的 templates 文件夹: |
| 145 | +```powershell |
| 146 | +# 备份(假设 exe 在 D:\Tools\PromptComposer\) |
| 147 | +Copy-Item "D:\Tools\PromptComposer\templates" -Destination "D:\Backup\PromptComposer_templates" -Recurse |
| 148 | +
|
| 149 | +# 恢复 |
| 150 | +Copy-Item "D:\Backup\PromptComposer_templates\*" -Destination "D:\Tools\PromptComposer\templates" -Recurse -Force |
| 151 | +``` |
| 152 | + |
| 153 | +### Q4: 如何在多台电脑间同步模板? |
| 154 | +A: 可选方案: |
| 155 | +1. **手动同步**:复制 exe 同目录下的 `templates\` 文件夹 |
| 156 | +2. **云盘同步**:将 exe 和 templates 文件夹放在 OneDrive、Dropbox 等云盘目录中 |
| 157 | +3. **Git 同步**:将模板文件夹初始化为 Git 仓库 |
| 158 | +4. **便携使用**:将整个文件夹(exe + templates)放在 U 盘中随身携带 |
| 159 | + |
| 160 | +### Q5: 打包时出现警告怎么办? |
| 161 | +A: 查看 `build/prompt_composer/warn-prompt_composer.txt` 文件。常见警告: |
| 162 | +- **缺少隐藏导入**:通常不影响运行,如有问题在 `.spec` 中添加 `hiddenimports` |
| 163 | +- **模块未找到**:检查依赖是否正确安装 |
| 164 | + |
| 165 | +## 📝 版本记录 |
| 166 | + |
| 167 | +### v1.0.1 (2026-02-10) |
| 168 | +- 🐛 修复:exe 打包后模板持久化问题 |
| 169 | +- ✨ 改进:使用 AppData 目录存储用户数据 |
| 170 | +- 📝 文档:添加详细的打包与发布指南 |
| 171 | + |
| 172 | +### v1.0.0 (2026-02-10) |
| 173 | +- 🎉 首次发布 |
| 174 | +- ✨ 支持结构化提示词编辑 |
| 175 | +- ✨ 模板管理系统 |
| 176 | +- ✨ 实时预览与复制 |
| 177 | + |
| 178 | +## 📚 参考资料 |
| 179 | + |
| 180 | +- [PyInstaller 官方文档](https://pyinstaller.org/) |
| 181 | +- [单文件打包最佳实践](https://pyinstaller.org/en/stable/operating-mode.html#bundling-to-one-file) |
| 182 | +- [处理数据文件](https://pyinstaller.org/en/stable/spec-files.html#adding-data-files) |
0 commit comments