compile & release script feature

This commit is contained in:
Darto KLoning
2026-09-08 20:05:25 +07:00
parent d406a6ffd8
commit ba1a799da4
9 changed files with 604 additions and 270 deletions
+237 -125
View File
@@ -2,176 +2,288 @@
setlocal EnableExtensions EnableDelayedExpansion
REM ============================================================
REM LesRaisinsArmor - Fabric Release Helper (Windows)
REM Version is read exclusively from gradle.properties.
REM LesRaisinsArmor - Gitea Release Script
REM Windows
REM ============================================================
cd /d "%~dp0"
set "GRADLE_PROPERTIES=%CD%\gradle.properties"
if not defined GITEA_BASE_URL set "GITEA_BASE_URL=https://git.darto.id"
if not defined GITEA_OWNER set "GITEA_OWNER=dartokloning"
if not defined GITEA_REPO set "GITEA_REPO=LesRaisinsArmor-Fabric"
set "MOD_VERSION="
set "MINECRAFT_VERSION="
set "ARCHIVES_BASE_NAME="
if not exist "%GRADLE_PROPERTIES%" (
echo [ERROR] gradle.properties tidak ditemukan.
goto :failed
)
for /f "usebackq tokens=1,* delims==" %%A in ("%GRADLE_PROPERTIES%") do (
for /f "usebackq tokens=1,* delims==" %%A in ("gradle.properties") do (
if "%%A"=="mod_version" set "MOD_VERSION=%%B"
if "%%A"=="minecraft_version" set "MINECRAFT_VERSION=%%B"
if "%%A"=="archives_base_name" set "ARCHIVES_BASE_NAME=%%B"
)
if not defined MOD_VERSION (
echo [ERROR] mod_version tidak ditemukan atau kosong di gradle.properties.
goto :failed
echo [ERROR] mod_version tidak ditemukan di gradle.properties.
exit /b 1
)
if not defined MINECRAFT_VERSION set "MINECRAFT_VERSION=26.2"
if not defined ARCHIVES_BASE_NAME set "ARCHIVES_BASE_NAME=lrarmor"
set "JAR_PATH=%CD%\build\libs\%ARCHIVES_BASE_NAME%-%MOD_VERSION%.jar"
set "SOURCES_JAR_PATH=%CD%\build\libs\%ARCHIVES_BASE_NAME%-%MOD_VERSION%-sources.jar"
set "RELEASE_ROOT=%CD%\release"
set "RELEASE_DIR=%RELEASE_ROOT%\%MOD_VERSION%"
set "RELEASE_JAR=%RELEASE_DIR%\%ARCHIVES_BASE_NAME%-%MOD_VERSION%.jar"
set "TAG=v%MOD_VERSION%"
set "JAR_NAME=%ARCHIVES_BASE_NAME%-%MOD_VERSION%.jar"
set "JAR_PATH=%CD%\build\libs\%JAR_NAME%"
set "PRERELEASE=false"
echo %MOD_VERSION% | findstr /I /C:"alpha" /C:"beta" /C:"rc" >nul && set "PRERELEASE=true"
echo ============================================
echo LesRaisinsArmor - Release Helper
echo Minecraft : %MINECRAFT_VERSION%
echo Version : %MOD_VERSION%
echo Tag : v%MOD_VERSION%
echo Tag : %TAG%
echo Gitea : %GITEA_BASE_URL%/%GITEA_OWNER%/%GITEA_REPO%
echo ============================================
echo.
if not exist "%JAR_PATH%" (
echo [INFO] JAR v%MOD_VERSION% belum ditemukan.
choice /C YN /N /M "Compile sekarang? [Y/N]: "
if errorlevel 2 goto :cancelled
call "%CD%\compile.bat" --build-only
if errorlevel 1 (
echo [ERROR] Compile gagal. Release dibatalkan.
goto :failed
)
)
if not exist "%JAR_PATH%" (
echo [ERROR] JAR release tetap tidak ditemukan:
echo %JAR_PATH%
goto :failed
)
if exist "%RELEASE_DIR%" (
echo [INFO] Folder release v%MOD_VERSION% sudah ada:
echo %RELEASE_DIR%
choice /C YN /N /M "Timpa artifact release versi ini? [Y/N]: "
if errorlevel 2 goto :cancelled
rmdir /S /Q "%RELEASE_DIR%"
)
mkdir "%RELEASE_DIR%" >nul 2>&1
copy /Y "%JAR_PATH%" "%RELEASE_JAR%" >nul
if errorlevel 1 goto :failed
if exist "%SOURCES_JAR_PATH%" copy /Y "%SOURCES_JAR_PATH%" "%RELEASE_DIR%\" >nul
if exist "%CD%\README.md" copy /Y "%CD%\README.md" "%RELEASE_DIR%\README.md" >nul
if exist "%CD%\PORT-CHANGELOG.md" copy /Y "%CD%\PORT-CHANGELOG.md" "%RELEASE_DIR%\PORT-CHANGELOG.md" >nul
if exist "%CD%\LICENSE" copy /Y "%CD%\LICENSE" "%RELEASE_DIR%\LICENSE" >nul
where certutil.exe >nul 2>&1
if not errorlevel 1 (
certutil -hashfile "%RELEASE_JAR%" SHA256 | findstr /V /I "hash CertUtil" > "%RELEASE_DIR%\SHA256SUMS.txt"
)
echo.
echo [SUCCESS] Artifact release siap:
echo %RELEASE_DIR%
echo.
dir /B "%RELEASE_DIR%"
echo.
choice /C YN /N /M "Buat dan push Git tag v%MOD_VERSION%? [Y/N]: "
if errorlevel 2 goto :success
where git.exe >nul 2>&1
REM Build lebih dulu. compile.bat --build-only tidak masuk Git integration.
echo [INFO] Menjalankan build release...
call "%CD%\compile.bat" --build-only
if errorlevel 1 (
echo [ERROR] Git tidak ditemukan di PATH. Artifact tetap aman.
goto :git_failed
echo [ERROR] Release dibatalkan karena build gagal.
exit /b 1
)
if not exist "%JAR_PATH%" (
echo [ERROR] JAR release tidak ditemukan:
echo %JAR_PATH%
exit /b 1
)
where git.exe >nul 2>&1 || (
echo [ERROR] Git tidak ditemukan di PATH.
exit /b 1
)
where curl.exe >nul 2>&1 || (
echo [ERROR] curl.exe tidak ditemukan di PATH.
exit /b 1
)
git rev-parse --is-inside-work-tree >nul 2>&1
if errorlevel 1 (
echo [ERROR] Folder ini bukan Git repository. Artifact tetap aman.
goto :git_failed
echo [ERROR] Folder ini bukan Git repository.
exit /b 1
)
set "DIRTY="
for /f "delims=" %%G in ('git status --porcelain') do set "DIRTY=1"
if defined DIRTY (
echo [ERROR] Working tree masih memiliki perubahan yang belum di-commit.
echo [INFO] Commit/push dulu dengan compile.bat agar tag menunjuk source release yang benar.
git status --short
goto :git_failed
set "CURRENT_BRANCH="
for /f "delims=" %%B in ('git branch --show-current') do set "CURRENT_BRANCH=%%B"
if not defined CURRENT_BRANCH (
echo [ERROR] Tidak dapat menentukan branch Git aktif.
exit /b 1
)
git rev-parse "v%MOD_VERSION%" >nul 2>&1
if not errorlevel 1 (
echo [INFO] Tag v%MOD_VERSION% sudah ada secara lokal; tidak membuat ulang.
git remote get-url origin >nul 2>&1
if errorlevel 1 (
echo [ERROR] Remote 'origin' belum dikonfigurasi.
exit /b 1
)
echo.
echo ============================================
echo Git Release Preparation
echo ============================================
REM build/, release/, .gradle*, dan run/ semestinya ada di .gitignore.
REM Kalau release/ lama masih untracked, jangan anggap sebagai perubahan source.
set "HAS_SOURCE_CHANGES="
for /f "delims=" %%G in ('git status --porcelain ^| findstr /V /B /C:"?? release/"') do set "HAS_SOURCE_CHANGES=1"
if defined HAS_SOURCE_CHANGES (
echo.
echo Perubahan source belum di-commit:
git status --short | findstr /V /B /C:"?? release/"
echo.
choice /C YN /N /M "Commit dan push perubahan sebelum membuat release? [Y/N]: "
if errorlevel 2 (
echo [WARN] Release dibatalkan. Source harus bersih agar tag sesuai dengan build.
exit /b 0
)
set "COMMIT_MESSAGE="
set /p "COMMIT_MESSAGE=Commit message [Prepare LesRaisinsArmor %TAG% release]: "
if not defined COMMIT_MESSAGE set "COMMIT_MESSAGE=Prepare LesRaisinsArmor %TAG% release"
git add -A -- . ":(exclude)release"
if errorlevel 1 exit /b 2
git commit -m "!COMMIT_MESSAGE!"
if errorlevel 1 exit /b 2
)
call :push_branch_with_retry
if errorlevel 1 (
echo [ERROR] Push branch gagal. Release dibatalkan sebelum tagging.
exit /b 2
)
set "HEAD_COMMIT="
for /f "delims=" %%H in ('git rev-parse HEAD') do set "HEAD_COMMIT=%%H"
set "TAG_COMMIT="
for /f "delims=" %%T in ('git rev-list -n 1 "%TAG%" 2^>nul') do set "TAG_COMMIT=%%T"
if defined TAG_COMMIT (
if /I not "!TAG_COMMIT!"=="!HEAD_COMMIT!" (
echo [ERROR] Tag %TAG% sudah ada tetapi menunjuk commit lain.
echo Tag : !TAG_COMMIT!
echo HEAD: !HEAD_COMMIT!
echo [INFO] Naikkan mod_version sebelum membuat release baru.
exit /b 2
)
echo [INFO] Tag lokal %TAG% sudah ada pada commit saat ini.
) else (
git tag -a "v%MOD_VERSION%" -m "LesRaisinsArmor v%MOD_VERSION%"
if errorlevel 1 goto :git_failed
echo [INFO] Membuat tag %TAG%...
git tag -a "%TAG%" -m "LesRaisinsArmor %TAG%"
if errorlevel 1 exit /b 2
)
git ls-remote --exit-code --tags origin "refs/tags/%TAG%" >nul 2>&1
if errorlevel 1 (
call :push_tag_with_retry
if errorlevel 1 exit /b 2
) else (
echo [INFO] Tag %TAG% sudah ada di origin.
)
echo.
echo ============================================
echo Gitea Release
echo ============================================
choice /C YN /N /M "Buat Release %TAG% dan upload %JAR_NAME%? [Y/N]: "
if errorlevel 2 (
echo [INFO] Pembuatan Gitea Release dilewati. Tag %TAG% tetap sudah tersimpan.
exit /b 0
)
if not defined GITEA_TOKEN (
echo.
set /p "GITEA_TOKEN=Gitea Personal Access Token: "
)
if not defined GITEA_TOKEN (
echo [ERROR] Token Gitea kosong.
exit /b 2
)
set "API_BASE=%GITEA_BASE_URL%/api/v1/repos/%GITEA_OWNER%/%GITEA_REPO%"
set "RELEASE_JSON=%TEMP%\lrarmor-release-%RANDOM%.json"
set "CREATE_JSON=%TEMP%\lrarmor-create-%RANDOM%.json"
set "ASSET_JSON=%TEMP%\lrarmor-asset-%RANDOM%.json"
set "HTTP_CODE="
for /f "delims=" %%H in ('curl.exe -sS -o "!RELEASE_JSON!" -w "%%{http_code}" -H "Authorization: token !GITEA_TOKEN!" "!API_BASE!/releases/tags/%TAG%"') do set "HTTP_CODE=%%H"
if "!HTTP_CODE!"=="200" (
echo [INFO] Release %TAG% sudah ada. Akan menggunakan release tersebut.
) else if "!HTTP_CODE!"=="404" (
>"!CREATE_JSON!" echo {
>>"!CREATE_JSON!" echo "tag_name": "%TAG%",
>>"!CREATE_JSON!" echo "target_commitish": "!CURRENT_BRANCH!",
>>"!CREATE_JSON!" echo "name": "LesRaisinsArmor %TAG%",
>>"!CREATE_JSON!" echo "body": "Unofficial private Fabric port for Minecraft %MINECRAFT_VERSION%.",
>>"!CREATE_JSON!" echo "draft": false,
>>"!CREATE_JSON!" echo "prerelease": %PRERELEASE%
>>"!CREATE_JSON!" echo }
echo [INFO] Membuat Release %TAG% di Gitea...
set "HTTP_CODE="
for /f "delims=" %%H in ('curl.exe -sS -o "!RELEASE_JSON!" -w "%%{http_code}" -X POST -H "Authorization: token !GITEA_TOKEN!" -H "Content-Type: application/json" --data-binary "@!CREATE_JSON!" "!API_BASE!/releases"') do set "HTTP_CODE=%%H"
if not "!HTTP_CODE!"=="201" (
echo [ERROR] Gitea gagal membuat release. HTTP !HTTP_CODE!
type "!RELEASE_JSON!"
call :cleanup_temp
exit /b 2
)
) else (
echo [ERROR] Gagal memeriksa release. HTTP !HTTP_CODE!
type "!RELEASE_JSON!"
call :cleanup_temp
exit /b 2
)
set "RELEASE_ID="
for /f "delims=" %%I in ('powershell.exe -NoProfile -Command "$r=Get-Content -Raw -LiteralPath '!RELEASE_JSON!' ^| ConvertFrom-Json; $r.id"') do set "RELEASE_ID=%%I"
if not defined RELEASE_ID (
echo [ERROR] Tidak dapat membaca release ID dari response Gitea.
type "!RELEASE_JSON!"
call :cleanup_temp
exit /b 2
)
echo [INFO] Release ID: !RELEASE_ID!
echo [INFO] Mengupload %JAR_NAME%...
set "ASSET_CODE="
for /f "delims=" %%H in ('curl.exe -sS -o "!ASSET_JSON!" -w "%%{http_code}" -X POST -H "Authorization: token !GITEA_TOKEN!" -F "attachment=@%JAR_PATH%" "!API_BASE!/releases/!RELEASE_ID!/assets?name=%JAR_NAME%"') do set "ASSET_CODE=%%H"
if "!ASSET_CODE!"=="201" (
echo.
echo [SUCCESS] Release %TAG% berhasil dibuat dan JAR berhasil diupload.
) else if "!ASSET_CODE!"=="409" (
echo.
echo [WARN] Release sudah ada dan asset %JAR_NAME% tampaknya sudah terupload.
) else (
echo [ERROR] Upload JAR gagal. HTTP !ASSET_CODE!
type "!ASSET_JSON!"
call :cleanup_temp
exit /b 2
)
call :cleanup_temp
echo.
echo ============================================
echo RELEASE SUCCESS
echo Tag : %TAG%
echo Asset : %JAR_NAME%
echo ============================================
exit /b 0
:push_branch_with_retry
set /a PUSH_ROUND=1
:push_round
:branch_round
set /a PUSH_ATTEMPT=1
:push_attempt
echo [INFO] Push tag attempt !PUSH_ATTEMPT!/3 ^(round !PUSH_ROUND!^)...
git push origin "v%MOD_VERSION%"
if not errorlevel 1 goto :success
:branch_attempt
echo [INFO] Push branch attempt !PUSH_ATTEMPT!/3 ^(round !PUSH_ROUND!^)...
git push -u origin "!CURRENT_BRANCH!"
if not errorlevel 1 exit /b 0
if !PUSH_ATTEMPT! LSS 3 (
set /a PUSH_ATTEMPT+=1
timeout /t 2 /nobreak >nul
goto :push_attempt
goto branch_attempt
)
echo [WARN] Push tag gagal setelah 3 percobaan.
choice /C YN /N /M "Coba push tag 3x lagi? [Y/N]: "
if errorlevel 2 goto :git_failed
choice /C YN /N /M "Push branch gagal 3x. Coba 3x lagi? [Y/N]: "
if errorlevel 2 exit /b 1
set /a PUSH_ROUND+=1
goto :push_round
goto branch_round
:git_failed
echo.
echo [WARN] Tahap Git/tag belum selesai, tetapi artifact release sudah dibuat.
goto :end_error
:push_tag_with_retry
set /a TAG_ROUND=1
:tag_round
set /a TAG_ATTEMPT=1
:tag_attempt
echo [INFO] Push tag attempt !TAG_ATTEMPT!/3 ^(round !TAG_ROUND!^)...
git push origin "%TAG%"
if not errorlevel 1 exit /b 0
if !TAG_ATTEMPT! LSS 3 (
set /a TAG_ATTEMPT+=1
timeout /t 2 /nobreak >nul
goto tag_attempt
)
choice /C YN /N /M "Push tag gagal 3x. Coba 3x lagi? [Y/N]: "
if errorlevel 2 exit /b 1
set /a TAG_ROUND+=1
goto tag_round
:success
echo.
echo ============================================
echo RELEASE v%MOD_VERSION% SIAP
echo ============================================
echo Artifact: %RELEASE_DIR%
echo ============================================
goto :end_ok
:cancelled
echo.
echo [INFO] Release dibatalkan oleh pengguna.
goto :end_ok
:failed
echo.
echo [ERROR] Release gagal.
goto :end_error
:end_ok
echo.
pause
:cleanup_temp
del /q "!RELEASE_JSON!" "!CREATE_JSON!" "!ASSET_JSON!" >nul 2>&1
exit /b 0
:end_error
echo.
pause
exit /b 1