Update ry.bat script

This commit is contained in:
wkc
2026-03-11 15:25:13 +08:00
parent d9f3165872
commit 281d919e57
2 changed files with 196 additions and 52 deletions

View File

@@ -0,0 +1,64 @@
$ErrorActionPreference = 'Stop'
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..\..\..')
$expectedJar = Join-Path $repoRoot 'ruoyi-admin\target\ruoyi-admin.jar'
$originalJar = Join-Path $repoRoot 'ruoyi-admin\target\ruoyi-admin.jar.original'
$backupOriginalJar = "$originalJar.codex-test-backup"
Push-Location $repoRoot
try {
$output = cmd /c "set RY_DRY_RUN=1&& call ry.bat start" 2>&1 | Out-String
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
throw "ry.bat dry-run failed with exit code $exitCode.`n$output"
}
if ($output -notmatch 'START_CMD=java ') {
throw "Expected START_CMD output was not found.`n$output"
}
if ($output -notmatch [regex]::Escape($expectedJar)) {
throw "Expected jar path was not found in dry-run output.`n$output"
}
foreach ($unsupportedToken in @('PrintGCDateStamps', 'UseParallelOldGC', 'javaw')) {
if ($output -match [regex]::Escape($unsupportedToken)) {
throw "Unexpected token [$unsupportedToken] found in dry-run output.`n$output"
}
}
if (Test-Path $originalJar) {
Move-Item -Path $originalJar -Destination $backupOriginalJar -Force
try {
$packageOutput = cmd /c "set RY_DRY_RUN=1&& call ry.bat start" 2>&1 | Out-String
$packageExitCode = $LASTEXITCODE
if ($packageExitCode -ne 0) {
throw "ry.bat package dry-run failed with exit code $packageExitCode.`n$packageOutput"
}
if ($packageOutput -notmatch 'BUILD_CMD=mvn -pl ruoyi-admin -am package -DskipTests') {
throw "Expected BUILD_CMD output was not found.`n$packageOutput"
}
if ($packageOutput -notmatch [regex]::Escape($expectedJar)) {
throw "Expected jar path was not found in package dry-run output.`n$packageOutput"
}
}
finally {
if (Test-Path $backupOriginalJar) {
Move-Item -Path $backupOriginalJar -Destination $originalJar -Force
}
}
}
Write-Host 'ry.bat dry-run verification passed.'
}
finally {
if (Test-Path $backupOriginalJar) {
Move-Item -Path $backupOriginalJar -Destination $originalJar -Force
}
Pop-Location
}

184
ry.bat
View File

@@ -1,67 +1,147 @@
@echo off
setlocal EnableExtensions EnableDelayedExpansion
cd ../ruoyi-admin/target
rem jar平级目录
set AppName=ruoyi-admin.jar
set "SCRIPT_DIR=%~dp0"
if "%SCRIPT_DIR:~-1%"=="\" set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%"
rem JVM参数
set JVM_OPTS="-Dname=%AppName% -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
set "APP_NAME=ruoyi-admin.jar"
set "JAVA_EXE=%JAVA_EXE%"
if not defined JAVA_EXE set "JAVA_EXE=java"
set "JPS_EXE=%JPS_EXE%"
if not defined JPS_EXE set "JPS_EXE=jps"
set "MVN_EXE=%MVN_EXE%"
if not defined MVN_EXE set "MVN_EXE=mvn"
set "MVN_ARGS=-pl ruoyi-admin -am package -DskipTests"
set "JVM_OPTS=-Dname=%APP_NAME% -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC"
set "DRY_RUN="
if /I "%RY_DRY_RUN%"=="1" set "DRY_RUN=1"
if /I "%~2"=="--dry-run" set "DRY_RUN=1"
if /I "%~1"=="start" goto start
if /I "%~1"=="stop" goto stop
if /I "%~1"=="restart" goto restart
if /I "%~1"=="status" goto status
if /I "%~1"=="help" goto help
if not "%~1"=="" goto help
:menu
ECHO.
ECHO. [1] 启动%AppName%
ECHO. [2] 关闭%AppName%
ECHO. [3] 重启%AppName%
ECHO. [4] 启动状态 %AppName%
ECHO. [5] 退 出
ECHO. [1] Start %APP_NAME%
ECHO. [2] Stop %APP_NAME%
ECHO. [3] Restart %APP_NAME%
ECHO. [4] Status %APP_NAME%
ECHO. [5] Exit
ECHO.
ECHO.请输入选择项目的序号:
ECHO.Select an action:
set /p ID=
IF "%id%"=="1" GOTO start
IF "%id%"=="2" GOTO stop
IF "%id%"=="3" GOTO restart
IF "%id%"=="4" GOTO status
IF "%id%"=="5" EXIT
PAUSE
if /I "%ID%"=="1" goto start
if /I "%ID%"=="2" goto stop
if /I "%ID%"=="3" goto restart
if /I "%ID%"=="4" goto status
if /I "%ID%"=="5" exit /b 0
echo Invalid selection.
pause
goto menu
:help
echo Usage: ry.bat ^<start^|stop^|restart^|status^> [--dry-run]
exit /b 1
:resolveAppPath
set "APP_PATH=%SCRIPT_DIR%\%APP_NAME%"
if exist "%APP_PATH%" exit /b 0
set "APP_PATH=%SCRIPT_DIR%\ruoyi-admin\target\%APP_NAME%"
if exist "%APP_PATH%" exit /b 0
set "APP_PATH="
exit /b 1
:shouldPackageJar
set "NEED_PACKAGE="
if not exist "%SCRIPT_DIR%\pom.xml" exit /b 0
if not exist "%SCRIPT_DIR%\ruoyi-admin\pom.xml" exit /b 0
if exist "%SCRIPT_DIR%\ruoyi-admin\target\%APP_NAME%.original" exit /b 0
set "NEED_PACKAGE=1"
exit /b 0
:findProcess
set "pid="
set "image_name="
for /f "tokens=1,*" %%a in ('%JPS_EXE% -l ^| findstr /I /C:"%APP_NAME%"') do (
set "pid=%%a"
set "image_name=%%b"
goto findProcessDone
)
:findProcessDone
exit /b 0
:start
for /f "usebackq tokens=1-2" %%a in (`jps -l ^| findstr %AppName%`) do (
set pid=%%a
set image_name=%%b
)
if defined pid (
echo %%is running
PAUSE
)
call :shouldPackageJar
if defined NEED_PACKAGE (
if defined DRY_RUN echo BUILD_CMD=%MVN_EXE% %MVN_ARGS%
if not defined DRY_RUN (
echo Packaging executable jar for %APP_NAME%...
pushd "%SCRIPT_DIR%"
call %MVN_EXE% %MVN_ARGS%
set "build_exit=!ERRORLEVEL!"
popd
if not "!build_exit!"=="0" exit /b !build_exit!
)
)
start javaw %JVM_OPTS% -jar %AppName%
call :resolveAppPath
if errorlevel 1 (
echo [ERROR] Unable to find %APP_NAME%.
echo Checked:
echo %SCRIPT_DIR%\%APP_NAME%
echo %SCRIPT_DIR%\ruoyi-admin\target\%APP_NAME%
exit /b 1
)
echo starting……
echo Start %AppName% success...
goto:eof
call :findProcess
if defined pid (
echo %APP_NAME% is already running. PID=!pid!
exit /b 0
)
if defined DRY_RUN (
echo START_CMD=%JAVA_EXE% %JVM_OPTS% -jar "%APP_PATH%"
exit /b 0
)
echo Starting %APP_NAME%...
%JAVA_EXE% %JVM_OPTS% -jar "%APP_PATH%"
exit /b %ERRORLEVEL%
rem 函数stop通过jps命令查找pid并结束进程
:stop
for /f "usebackq tokens=1-2" %%a in (`jps -l ^| findstr %AppName%`) do (
set pid=%%a
set image_name=%%b
)
if not defined pid (echo process %AppName% does not exists) else (
echo prepare to kill %image_name%
echo start kill %pid% ...
rem 根据进程IDkill进程
taskkill /f /pid %pid%
)
goto:eof
call :findProcess
if not defined pid (
echo process %APP_NAME% does not exist
exit /b 0
)
echo Stopping %APP_NAME% (PID !pid! )...
taskkill /f /pid !pid! >nul 2>nul
if errorlevel 1 (
echo [ERROR] Failed to stop %APP_NAME%.
exit /b 1
)
echo %APP_NAME% stopped.
exit /b 0
:restart
call :stop
call :start
goto:eof
call :stop
if errorlevel 1 exit /b 1
call :start
exit /b %ERRORLEVEL%
:status
for /f "usebackq tokens=1-2" %%a in (`jps -l ^| findstr %AppName%`) do (
set pid=%%a
set image_name=%%b
)
if not defined pid (echo process %AppName% is dead ) else (
echo %image_name% is running
)
goto:eof
call :findProcess
if not defined pid (
echo process %APP_NAME% is dead
exit /b 0
)
echo %image_name% is running with PID !pid!
exit /b 0