Files
2026-10-03 16:32:36 +07:00

547 lines
15 KiB
Batchfile

@echo off
setlocal EnableExtensions EnableDelayedExpansion
cd /d "%~dp0"
REM ============================================================
REM Handcrafted - Unified Workflow
REM Compile + Git Push + Mod Testing + Gitea Release
REM Windows
REM ============================================================
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=handcrafted"
set "PROPS=%CD%\gradle.properties"
if not exist "%PROPS%" (
echo [ERROR] gradle.properties tidak ditemukan.
pause
exit /b 1
)
call :refresh_properties
call :check_tools
if exist "%JAR_PATH%" (
echo [INFO] JAR untuk versi %MOD_VERSION% sudah ditemukan.
goto main_menu
)
echo [INFO] JAR untuk versi %MOD_VERSION% belum ditemukan.
echo [INFO] Compile otomatis akan dijalankan.
call :compile_mod
if errorlevel 1 goto build_failed_menu
:main_menu
call :refresh_properties
echo.
echo ============================================
echo Handcrafted Workflow
echo Minecraft : %MINECRAFT_VERSION%
echo Version : %MOD_VERSION%
echo JAR : %JAR_NAME%
echo ============================================
echo [1] Push Git
echo [2] Mod Testing
echo [3] Make Release
echo [4] Exit
choice /C 1234 /N /M "Pilih: "
if errorlevel 4 exit /b 0
if errorlevel 3 (
call :make_release
goto after_release
)
if errorlevel 2 (
call :test_mod
goto after_test
)
if errorlevel 1 (
call :push_git
goto after_push
)
:after_push
echo.
echo ============================================
echo Action berikutnya
echo ============================================
echo [1] Mod Testing
echo [2] Make Release
echo [3] Exit
choice /C 123 /N /M "Pilih: "
if errorlevel 3 exit /b 0
if errorlevel 2 (
call :make_release
goto after_release
)
if errorlevel 1 (
call :test_mod
goto after_test
)
:after_test
echo.
echo ============================================
echo Action berikutnya
echo ============================================
echo [1] Push Git
echo [2] Make Release
echo [3] Exit
choice /C 123 /N /M "Pilih: "
if errorlevel 3 exit /b 0
if errorlevel 2 (
call :make_release
goto after_release
)
if errorlevel 1 (
call :push_git
goto after_push
)
:after_release
echo.
echo ============================================
echo Action berikutnya
echo ============================================
echo [1] Push Git
echo [2] Mod Testing
echo [3] Exit
choice /C 123 /N /M "Pilih: "
if errorlevel 3 exit /b 0
if errorlevel 2 (
call :test_mod
goto after_test
)
if errorlevel 1 (
call :push_git
goto after_push
)
:build_failed_menu
echo.
echo [ERROR] Build gagal atau JAR belum tersedia.
echo [1] Coba Compile lagi
echo [2] Push Git
echo [3] Mod Testing
echo [4] Exit
choice /C 1234 /N /M "Pilih: "
if errorlevel 4 exit /b 1
if errorlevel 3 (
call :test_mod
goto after_test
)
if errorlevel 2 (
call :push_git
goto after_push
)
if errorlevel 1 (
call :compile_mod
if errorlevel 1 goto build_failed_menu
goto main_menu
)
:refresh_properties
set "MOD_VERSION="
set "MINECRAFT_VERSION="
set "ARCHIVES_BASE_NAME="
for /f "usebackq tokens=1,* delims==" %%A in ("%PROPS%") 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 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=handcrafted"
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"
exit /b 0
:check_tools
where git.exe >nul 2>&1 || echo [WARN] Git tidak ditemukan di PATH. Fitur Git/Release tidak akan bekerja.
where curl.exe >nul 2>&1 || echo [WARN] curl.exe tidak ditemukan di PATH. Gitea Release tidak akan bekerja.
exit /b 0
:compile_mod
call :refresh_properties
echo.
echo ============================================
echo COMPILE
echo Handcrafted %MOD_VERSION% / Minecraft %MINECRAFT_VERSION%
echo ============================================
if not exist "%CD%\gradlew.bat" (
echo [ERROR] gradlew.bat tidak ditemukan.
exit /b 1
)
call "%CD%\gradlew.bat" clean build --console=plain
if errorlevel 1 (
echo [ERROR] Compile gagal.
exit /b 1
)
call :refresh_properties
if not exist "%JAR_PATH%" (
echo [ERROR] Build sukses tetapi JAR tidak ditemukan:
echo %JAR_PATH%
exit /b 1
)
echo [OK] JAR: %JAR_PATH%
exit /b 0
:push_git
call :refresh_properties
echo.
echo ============================================
echo GIT PUSH
echo ============================================
where git.exe >nul 2>&1 || (
echo [ERROR] Git tidak ditemukan di PATH.
exit /b 1
)
git rev-parse --is-inside-work-tree >nul 2>&1 || (
echo [ERROR] Folder ini bukan Git repository.
echo [INFO] Jika repository baru, jalankan: git init
exit /b 1
)
git remote get-url origin >nul 2>&1 || (
echo [ERROR] Remote origin belum dikonfigurasi.
exit /b 1
)
set "CURRENT_BRANCH="
for /f "delims=" %%B in ('git branch --show-current') do set "CURRENT_BRANCH=%%B"
if not defined CURRENT_BRANCH (
echo [ERROR] Branch aktif tidak ditemukan.
exit /b 1
)
set "HAS_CHANGES="
for /f "delims=" %%G in ('git status --porcelain') do set "HAS_CHANGES=1"
if defined HAS_CHANGES (
echo.
echo Perubahan yang akan di-commit:
git status --short
echo.
set "COMMIT_MESSAGE="
set /p "COMMIT_MESSAGE=Commit message [Handcrafted %MOD_VERSION%]: "
if not defined COMMIT_MESSAGE set "COMMIT_MESSAGE=Handcrafted %MOD_VERSION%"
git add -A
if errorlevel 1 exit /b 1
git commit -m "!COMMIT_MESSAGE!"
if errorlevel 1 exit /b 1
) else (
echo [INFO] Working tree bersih. Tidak ada commit baru.
)
call :push_branch_with_retry
if errorlevel 1 (
echo.
echo [ERROR] Push Git belum berhasil.
call :push_failure_menu
exit /b !ERRORLEVEL!
)
echo [OK] Push branch %CURRENT_BRANCH% selesai.
exit /b 0
:push_failure_menu
echo.
echo ============================================
echo Push masih gagal
echo ============================================
echo [1] Coba Push Git lagi
echo [2] Mod Testing
echo [3] Make Release
echo [4] Exit
choice /C 1234 /N /M "Pilih: "
if errorlevel 4 exit /b 1
if errorlevel 3 (
call :make_release
exit /b !ERRORLEVEL!
)
if errorlevel 2 (
call :test_mod
exit /b !ERRORLEVEL!
)
if errorlevel 1 (
call :push_branch_with_retry
if errorlevel 1 goto push_failure_menu
echo [OK] Push branch %CURRENT_BRANCH% selesai.
exit /b 0
)
:push_branch_with_retry
set /a PUSH_ATTEMPT=1
:push_branch_attempt
echo [INFO] Push branch attempt !PUSH_ATTEMPT!/3...
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_branch_attempt
)
exit /b 1
:test_mod
echo.
echo ============================================
echo MOD TESTING
echo ============================================
if not exist "%CD%\gradlew.bat" (
echo [ERROR] gradlew.bat tidak ditemukan.
exit /b 1
)
call "%CD%\gradlew.bat" runClient --console=plain
set "TEST_RC=%ERRORLEVEL%"
if not "%TEST_RC%"=="0" (
echo [WARN] Test client selesai dengan exit code %TEST_RC%.
) else (
echo [OK] Test client selesai.
)
exit /b 0
:make_release
call :refresh_properties
echo.
echo ============================================
echo GITEA RELEASE
echo Minecraft : %MINECRAFT_VERSION%
echo Version : %MOD_VERSION%
echo Tag : %TAG%
echo Asset : %JAR_NAME%
echo Gitea : %GITEA_BASE_URL%/%GITEA_OWNER%/%GITEA_REPO%
echo ============================================
if not exist "%JAR_PATH%" (
echo [ERROR] JAR release tidak ditemukan:
echo %JAR_PATH%
echo [INFO] Compile versi ini lebih dulu.
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 || (
echo [ERROR] Folder ini bukan Git repository.
exit /b 1
)
git remote get-url origin >nul 2>&1 || (
echo [ERROR] Remote origin belum dikonfigurasi.
exit /b 1
)
set "CURRENT_BRANCH="
for /f "delims=" %%B in ('git branch --show-current') do set "CURRENT_BRANCH=%%B"
if not defined CURRENT_BRANCH (
echo [ERROR] Branch aktif tidak ditemukan.
exit /b 1
)
REM Release harus merepresentasikan source saat ini.
set "HAS_CHANGES="
for /f "delims=" %%G in ('git status --porcelain') do set "HAS_CHANGES=1"
if defined HAS_CHANGES (
echo.
echo [WARN] Ada perubahan source yang belum di-commit:
git status --short
echo.
choice /C YN /N /M "Commit dan push sebelum release? [Y/N]: "
if errorlevel 2 (
echo [INFO] Release dibatalkan agar tag tidak berbeda dari source build.
exit /b 0
)
set "COMMIT_MESSAGE="
set /p "COMMIT_MESSAGE=Commit message [Prepare Handcrafted %TAG% release]: "
if not defined COMMIT_MESSAGE set "COMMIT_MESSAGE=Prepare Handcrafted %TAG% release"
git add -A
if errorlevel 1 exit /b 2
git commit -m "!COMMIT_MESSAGE!"
if errorlevel 1 exit /b 2
)
REM Selalu pastikan branch sudah ada di remote sebelum tagging/release.
call :push_branch_with_retry
if errorlevel 1 (
echo [ERROR] Push branch gagal 3x. Release belum dibuat.
call :release_push_failure_menu
exit /b !ERRORLEVEL!
)
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 benar pada HEAD saat ini.
) else (
echo [INFO] Membuat tag %TAG%...
git tag -a "%TAG%" -m "Handcrafted %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 (
echo [ERROR] Push tag gagal.
exit /b 2
)
) else (
echo [INFO] Tag %TAG% sudah ada di origin.
)
echo.
choice /C YN /N /M "Buat Gitea Release %TAG% dan upload %JAR_NAME%? [Y/N]: "
if errorlevel 2 (
echo [INFO] Gitea Release dilewati. Tag tetap 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%\handcrafted-release-%RANDOM%.json"
set "CREATE_JSON=%TEMP%\handcrafted-create-%RANDOM%.json"
set "ASSET_JSON=%TEMP%\handcrafted-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] Gitea Release %TAG% sudah ada. Release tersebut akan digunakan.
) 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": "Handcrafted %TAG% for Fabric %MINECRAFT_VERSION%",
>>"!CREATE_JSON!" echo "body": "Fabric port of Handcrafted for Minecraft %MINECRAFT_VERSION%.",
>>"!CREATE_JSON!" echo "draft": false,
>>"!CREATE_JSON!" echo "prerelease": %PRERELEASE%
>>"!CREATE_JSON!" echo }
echo [INFO] Membuat Gitea Release %TAG%...
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 Gitea 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 Gitea.
type "!RELEASE_JSON!"
call :cleanup_temp
exit /b 2
)
REM Cek apakah asset dengan nama yang sama sudah ada.
set "ASSET_EXISTS="
for /f "delims=" %%E in ('powershell.exe -NoProfile -Command "$r=Get-Content -Raw -LiteralPath '!RELEASE_JSON!' ^| ConvertFrom-Json; if($r.assets ^| Where-Object { $_.name -eq '%JAR_NAME%' }) { 'yes' }"') do set "ASSET_EXISTS=%%E"
if /I "!ASSET_EXISTS!"=="yes" (
echo [INFO] Asset %JAR_NAME% sudah ada di release. Tidak diupload ulang.
call :cleanup_temp
echo [SUCCESS] Gitea Release %TAG% siap.
exit /b 0
)
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 [WARN] Asset %JAR_NAME% sudah ada di release.
) 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
:release_push_failure_menu
echo.
echo [1] Coba Push Git lagi
echo [2] Mod Testing
echo [3] Exit
choice /C 123 /N /M "Pilih: "
if errorlevel 3 exit /b 1
if errorlevel 2 (
call :test_mod
exit /b 1
)
if errorlevel 1 (
call :push_branch_with_retry
if errorlevel 1 goto release_push_failure_menu
echo [OK] Push berhasil. Jalankan Make Release lagi dari workflow.
exit /b 1
)
:push_tag_with_retry
set /a TAG_ATTEMPT=1
:push_tag_attempt
echo [INFO] Push tag attempt !TAG_ATTEMPT!/3...
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 push_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_ATTEMPT=1
goto push_tag_attempt
:cleanup_temp
if defined RELEASE_JSON del /q "!RELEASE_JSON!" >nul 2>&1
if defined CREATE_JSON del /q "!CREATE_JSON!" >nul 2>&1
if defined ASSET_JSON del /q "!ASSET_JSON!" >nul 2>&1
exit /b 0