MilliSim/build.bat

98 lines
2.8 KiB
Batchfile
Raw Permalink 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
chcp 65001 >nul 2>&1
cd /d "%~dp0"
title MilliSim4 前后端打包工具
echo ========================================
echo MilliSim4 前后端打包脚本
echo 后端 -^> publish\backend\
echo 前端 -^> publish\frontend\
echo ========================================
echo.
REM ===== 1. 停止后端进程(释放 bin 目录锁)=====
echo [1/4] 停止后端进程...
set "killed=0"
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":5000 " ^| findstr "LISTENING"') do (
echo - 停止进程 PID: %%a
taskkill /F /PID %%a >nul 2>&1
set "killed=1"
)
taskkill /F /IM MilliSim.Api.exe >nul 2>&1
if "%killed%"=="1" (
echo - 已停止后端进程,等待端口释放...
timeout /t 2 /nobreak >nul
) else (
echo - 没有发现运行中的后端进程
)
echo.
REM ===== 2. 打包后端 (Release) =====
echo [2/4] 打包后端 (Release)...
if not exist "%~dp0backend\MilliSim.Api\MilliSim.Api.csproj" (
echo [错误] 未找到后端项目文件!
pause
exit /b 1
)
dotnet publish "%~dp0backend\MilliSim.Api\MilliSim.Api.csproj" -c Release -o "%~dp0publish\backend" --nologo
if %errorlevel% neq 0 (
echo.
echo [错误] 后端打包失败!请检查编译错误。
pause
exit /b 1
)
echo 后端打包完成 -^> publish\backend\
echo.
REM ===== 3. 打包前端 (Production) =====
echo [3/4] 打包前端 (Production)...
if not exist "%~dp0frontend\package.json" (
echo [错误] 未找到前端项目文件!
pause
exit /b 1
)
cd /d "%~dp0frontend"
call npm run build
if %errorlevel% neq 0 (
echo.
echo [错误] 前端打包失败!请检查编译错误。
pause
exit /b 1
)
cd /d "%~dp0"
echo 前端编译完成
echo.
REM ===== 4. 复制前端产物到 publish\frontend =====
echo [4/4] 复制前端产物到 publish\frontend\...
REM 清空旧的产物目录
if exist "%~dp0publish\frontend\" rmdir /s /q "%~dp0publish\frontend\" 2>nul
mkdir "%~dp0publish\frontend\" 2>nul
REM 复制新产物(含 frontend\public\web.configVite 构建时会自动拷贝到 dist\
xcopy "%~dp0frontend\dist\*" "%~dp0publish\frontend\" /E /Y /Q >nul 2>&1
if %errorlevel% neq 0 (
echo [错误] 复制前端产物失败!
pause
exit /b 1
)
echo - 已复制前端产物(含 web.config
echo.
REM ===== 完成 =====
echo ========================================
echo 打包完成!
echo ========================================
echo.
echo 后端产物: %~dp0publish\backend\
echo 前端产物: %~dp0publish\frontend\
echo.
echo 部署说明:
echo 1. 服务器需安装 .NET 10 Hosting Bundle
echo 2. 服务器需安装 IIS URL Rewrite 模块
echo 3. 后端publish\backend\ 部署为 IIS 站点(应用池: 无托管代码)
echo 4. 前端publish\frontend\ 部署为独立 IIS 站点
echo 5. 配置前端的 API 反向代理指向后端地址
echo.
pause