Files
msgbot/build/build.bat
T

117 lines
3.0 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@echo off
setlocal enabledelayedexpansion
REM === WindowMsgBot 构建脚本 ===
REM 自动化打包和签名流程
REM 用法: build.bat /skip-cert /skip-sign
REM build.bat --skip-cert --skip-sign (PowerShell兼容)
REM build.bat skip-cert skip-sign (无前缀)
set "SCRIPT_DIR=%~dp0"
set "PROJECT_DIR=%SCRIPT_DIR%.."
set "PYTHON=%PROJECT_DIR%\.venv311\Scripts\python.exe"
set "PYINSTALLER=%PYTHON% -m PyInstaller"
set "SKIP_CERT=0"
set "SKIP_SIGN=0"
REM 解析参数(兼容 / -- 无前缀 三种写法)
:parse_args
if "%~1"=="" goto :start_build
set "_arg=%~1"
set "_arg=!_arg:/=!"
set "_arg=!_arg:--=!"
if "!_arg!"=="skip-cert" set "SKIP_CERT=1"
if "!_arg!"=="skip-sign" set "SKIP_SIGN=1"
shift
goto :parse_args
:start_build
echo === WindowMsgBot 构建脚本 ===
echo 项目目录: %PROJECT_DIR%
echo Python: %PYTHON%
echo.
REM 检查Python
if not exist "%PYTHON%" (
echo 错误: Python不存在: %PYTHON%
echo 请先创建虚拟环境
pause
exit /b 1
)
REM 检查PyInstaller
"%PYTHON%" -c "import PyInstaller" >nul 2>&1
if %errorlevel% neq 0 (
echo 安装PyInstaller...
"%PYTHON%" -m pip install pyinstaller
)
REM 1. 生成证书(可选)
if "%SKIP_CERT%"=="0" (
echo 1. 生成自签名证书...
if exist "%SCRIPT_DIR%code_signing_cert.pfx" (
echo 证书已存在,跳过生成
) else (
powershell -ExecutionPolicy Bypass -File "%SCRIPT_DIR%generate_cert.ps1"
if %errorlevel% neq 0 (
echo ⚠ 证书生成失败,继续构建...
)
)
) else (
echo 1. 跳过证书生成
)
REM 2. 清理旧构建(只清理PyInstaller输出,保留build目录里的spec和hooks)
echo.
echo 2. 清理旧构建文件...
if exist "%PROJECT_DIR%\build\build" rmdir /s /q "%PROJECT_DIR%\build\build"
if exist "%PROJECT_DIR%\dist" rmdir /s /q "%PROJECT_DIR%\dist"
REM 3. PyInstaller打包
echo.
echo 3. PyInstaller打包...
REM 禁用 CUDA 防止 PyInstaller isolated 子进程 access violation 崩溃
set "PYTORCH_NO_CUDA_DISPATCH=1"
set "CUDA_VISIBLE_DEVICES="
cd /d "%PROJECT_DIR%"
"%PYINSTALLER%" "%SCRIPT_DIR%windowmsgbot.spec" --noconfirm --clean
if %errorlevel% neq 0 (
echo 错误: PyInstaller打包失败!
pause
exit /b 1
)
echo ✓ 打包完成
REM 4. 签名(可选)
if "%SKIP_SIGN%"=="0" (
echo.
echo 4. 签名exe文件...
if exist "%SCRIPT_DIR%code_signing_cert.pfx" (
call "%SCRIPT_DIR%sign.bat"
) else (
echo ⚠ 证书不存在,跳过签名
)
) else (
echo.
echo 4. 跳过签名
)
REM 5. 输出结果
echo.
echo 5. 构建结果...
if exist "%PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe" (
echo ✓ 可执行文件: %PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe
for %%A in ("%PROJECT_DIR%\dist\WindowMsgBot\WindowMsgBot.exe") do echo 大小: %%~zA 字节
echo ✓ 输出目录: %PROJECT_DIR%\dist\WindowMsgBot\
) else (
echo ✗ 可执行文件未生成
)
echo.
echo === 构建完成 ===
echo 输出目录: %PROJECT_DIR%\dist
echo.
pause