Files
nezhacursor/build.bat
2025-02-14 16:15:04 +08:00

119 lines
2.9 KiB
Batchfile

@echo off
chcp 65001
setlocal EnableDelayedExpansion
echo 开始正式版本打包...
:: 设置工作目录为脚本所在目录
cd /d "%~dp0"
:: 激活虚拟环境
if exist "venv\Scripts\activate.bat" (
echo 激活虚拟环境...
call venv\Scripts\activate.bat
) else (
echo 警告: 未找到虚拟环境,使用系统 Python
)
:: 确保安装了必要的包
echo 检查依赖包...
pip install -r requirements.txt
:: 更新版本号
python update_version.py
:: 读取版本号
set /p VERSION=<version.txt
echo 当前版本: !VERSION!
:: 提取主版本号和次版本号 (3.4.7 -> 3.4)
for /f "tokens=1,2 delims=." %%a in ("!VERSION!") do (
set MAJOR_VERSION=%%a.%%b
)
echo 主版本目录: !MAJOR_VERSION!
:: 创建版本目录
set VERSION_DIR=dist\!MAJOR_VERSION!
if not exist "!VERSION_DIR!" (
mkdir "!VERSION_DIR!"
echo 创建目录: !VERSION_DIR!
)
:: 清理 Python 缓存文件
echo 清理Python缓存文件...
for /d /r . %%d in (__pycache__) do @if exist "%%d" rd /s /q "%%d"
del /s /q *.pyc >nul 2>&1
del /s /q *.pyo >nul 2>&1
:: 清理旧的打包文件
echo 清理旧文件...
if exist "build" rd /s /q "build"
if exist "*.spec" del /f /q "*.spec"
:: 使用优化选项进行打包
echo 开始打包...
pyinstaller ^
--noconfirm ^
--clean ^
--onefile ^
--noconsole ^
--icon=icon/two.ico ^
--name "听泉cursor助手!VERSION!" ^
--add-data "icon;icon" ^
--add-data "version.txt;." ^
--add-data "testversion.txt;." ^
--add-data "requirements.txt;." ^
--exclude-module _tkinter ^
--exclude-module tkinter ^
--exclude-module PIL.ImageTk ^
--exclude-module PIL.ImageWin ^
--exclude-module numpy ^
--exclude-module pandas ^
--exclude-module matplotlib ^
--exclude "__pycache__" ^
--exclude "*.pyc" ^
--exclude "*.pyo" ^
--exclude "*.pyd" ^
main.py
:: 检查打包结果并移动文件
set TEMP_FILE=dist\听泉cursor助手!VERSION!.exe
set TARGET_FILE=!VERSION_DIR!\听泉cursor助手v!VERSION!.exe
echo 检查文件: !TEMP_FILE!
if exist "!TEMP_FILE!" (
echo 正式版本打包成功!
:: 移动到版本目录
echo 移动文件到: !TARGET_FILE!
move "!TEMP_FILE!" "!TARGET_FILE!"
:: 显示文件大小
for %%I in ("!TARGET_FILE!") do (
echo 文件大小: %%~zI 字节
)
echo.
echo 正式版本构建完成!
echo 版本号: v!VERSION!
echo 文件位置: !TARGET_FILE!
) else (
echo 错误: 打包失败,文件不存在
echo 预期文件路径: !TEMP_FILE!
dir /b dist
exit /b 1
)
:: 清理临时文件
echo 清理临时文件...
if exist "build" rd /s /q "build"
if exist "dist\听泉cursor助手!VERSION!.exe" del /f /q "dist\听泉cursor助手!VERSION!.exe"
if exist "*.spec" del /f /q "*.spec"
:: 退出虚拟环境
if exist "venv\Scripts\activate.bat" (
echo 退出虚拟环境...
deactivate
)
endlocal
pause