the_goth_cardgame_collection/build_scripts/build.bat

221 lines
7.5 KiB
Batchfile

@echo off
setlocal EnableDelayedExpansion
REM =============================================================================
REM The Goth Cardgame Collection - One-File Build Script (Windows + WSL)
REM Output: dist\windows\TheGothCardgameCollection.exe
REM Usage: build_scripts\build.bat [--current | --all | --clean]
REM =============================================================================
set "SCRIPT_FILE=%~f0"
cd /d "%~dp0.."
set "PROJECT_ROOT=%CD%"
set "APP_NAME=TheGothCardgameCollection"
set "MAIN_FILE=main.py"
set "RES_DIR=resources"
set "GAMES_DIR=games"
set "MODE=%~1"
if "%MODE%"=="" set "MODE=--current"
if /I "%MODE%"=="--clean" (
if exist "build" rmdir /s /q "build"
if exist ".venv_build_win" rmdir /s /q ".venv_build_win"
del /q "*.spec" 2>nul
echo [OK] Temporaere Build-Dateien bereinigt.
exit /b 0
)
REM ----------------------------------------------------
REM 0. ADMIN-RECHTE PRUEFEN (net session)
REM ----------------------------------------------------
set "IS_ADMIN=0"
net session >nul 2>nul
if not errorlevel 1 set "IS_ADMIN=1"
REM Kaputte Altlast-Venvs vorab bereinigen
if exist ".venv_build_win" (
if not exist ".venv_build_win\Scripts\python.exe" rmdir /s /q ".venv_build_win"
)
REM Vorhandenes Python ermitteln
call :FIND_PYTHON
REM Pruefen, ob System-Installationen noetig sind (Python fehlt oder WSL-Distro fehlt bei --all)
set "NEEDS_ADMIN=0"
if "%WIN_PY%"=="" set "NEEDS_ADMIN=1"
set "WSL_DISTRO="
if /I "%MODE%"=="--all" (
call :FIND_WSL_DISTRO
if "!WSL_DISTRO!"=="" set "NEEDS_ADMIN=1"
)
REM Falls etwas auf Systemebene installiert werden muss und KEINE Admin-Rechte vorliegen -> UAC anfordern!
if "%NEEDS_ADMIN%"=="1" if "%IS_ADMIN%"=="0" (
if /I "%~2"=="--elevated" (
echo [ERROR] Administrator-Rechte wurden verweigert oder konnten nicht erlangt werden!
echo Bitte starte die Konsole / PowerShell explizit "Als Administrator".
pause
exit /b 1
)
echo [ADMIN] Fehlende Systemkomponenten erkannt ^(Python oder WSL^).
echo Fordere Administrator-Rechte ueber Windows-UAC an...
powershell -NoProfile -Command "Start-Process -FilePath 'cmd.exe' -ArgumentList '/k \"\"%SCRIPT_FILE%\" %MODE% --elevated\"' -Verb RunAs"
exit /b 0
)
echo ====================================================
echo Starte Build fuer %APP_NAME% ^(Modus: %MODE% ^| Admin: %IS_ADMIN%^)
echo ====================================================
REM Falls Python fehlte -> jetzt mit garantierten Admin-Rechten systemweit installieren
if "%WIN_PY%"=="" (
echo [BOOTSTRAP] Installiere Python 3.11 via winget ^(Administrator^)...
winget install -e --id Python.Python.3.11 --scope machine --accept-source-agreements --accept-package-agreements
for %%P in (
"%ProgramFiles%\Python311"
"%LocalAppData%\Programs\Python\Python311"
"C:\Python311"
) do (
if exist "%%~P\python.exe" set "PATH=%%~P;%%~P\Scripts;!PATH!"
)
call :FIND_PYTHON
)
if "%WIN_PY%"=="" (
echo [ERROR] Python 3.11+ konnte nicht gefunden oder installiert werden!
exit /b 1
)
REM ----------------------------------------------------
REM TEIL 1: WINDOWS .EXE BAUEN
REM ----------------------------------------------------
echo.
echo ---^> [1] Baue Windows .exe mit %WIN_PY%...
REM Ohne Admin-Rechte & ausserhalb einer Venv installieren wir Pakete sauber mit --user
set "PIP_EXTRA="
if "%IS_ADMIN%"=="0" if "%USING_VENV%"=="0" set "PIP_EXTRA=--user"
%WIN_PY% -m pip install --upgrade pip
%WIN_PY% -m pip install %PIP_EXTRA% --upgrade pillow pygame pyinstaller
if errorlevel 1 (
echo [ERROR] Konnte Python-Pakete ^(PyInstaller/Pillow/Pygame^) nicht installieren!
exit /b 1
)
set "ICON_ARG="
if exist "resources\images\game_icon.ico" set "ICON_ARG=--icon resources\images\game_icon.ico"
%WIN_PY% -m PyInstaller ^
--noconfirm ^
--noconsole ^
--onefile ^
--clean ^
--distpath "dist\windows" ^
--workpath "build\windows" ^
--specpath "build\windows" ^
%ICON_ARG% ^
--add-data "%RES_DIR%;%RES_DIR%" ^
--add-data "%GAMES_DIR%;%GAMES_DIR%" ^
--collect-submodules games ^
--collect-submodules controller ^
--collect-submodules models ^
--collect-submodules view ^
--collect-submodules resources ^
--hidden-import tkinter ^
--hidden-import tkinter.ttk ^
--hidden-import tkinter.font ^
--hidden-import sqlite3 ^
--hidden-import PIL._tkinter_finder ^
--name "%APP_NAME%" ^
"%MAIN_FILE%"
if errorlevel 1 (
echo [ERROR] Windows-Build fehlgeschlagen!
exit /b 1
)
if exist "build\windows" rmdir /s /q "build\windows"
echo [OK] Windows-Build fertig: dist\windows\%APP_NAME%.exe
REM ----------------------------------------------------
REM TEIL 2: LINUX EXECUTABLE VIA WSL (nur bei --all)
REM ----------------------------------------------------
if /I "%MODE%"=="--all" (
echo.
echo ---^> [2] Baue Linux Executable ^(via WSL^)...
call :FIND_WSL_DISTRO
if "!WSL_DISTRO!"=="" (
echo [BOOTSTRAP] Keine Linux-Distribution in WSL gefunden. Installiere Ubuntu ^(als Administrator^)...
wsl.exe --install -d Ubuntu --no-launch
call :FIND_WSL_DISTRO
if "!WSL_DISTRO!"=="" (
echo [HINWEIS] WSL/Ubuntu wurde vorbereitet. Windows erfordert einmalig einen System-Neustart,
echo bevor Linux-Builds in WSL ausgefuehrt werden koennen!
goto :SUMMARY
)
)
echo [WSL] Nutze Distribution: !WSL_DISTRO! ^(als root fuer Paket-Rechte^)
set "WSL_PROJ_PATH="
for /f "usebackq delims=" %%P in (`wsl.exe -d "!WSL_DISTRO!" -u root -- wslpath -a "%PROJECT_ROOT%" 2^>nul`) do (
set "WSL_PROJ_PATH=%%P"
)
if "!WSL_PROJ_PATH!"=="" (
echo [ERROR] Konnte Projektpfad "%PROJECT_ROOT%" in WSL nicht aufloesen.
exit /b 1
)
REM Fuehrt build.sh in WSL direkt als root (-u root) aus -> keine Passwort-Abfrage & volle apt-Rechte!
wsl.exe -d "!WSL_DISTRO!" -u root -- bash -lc "cd '!WSL_PROJ_PATH!' && sed -i 's/\r$//' build_scripts/build.sh && chmod +x build_scripts/build.sh && ./build_scripts/build.sh --current"
if errorlevel 1 (
echo [ERROR] Linux-Build in WSL fehlgeschlagen!
exit /b 1
)
)
:SUMMARY
echo.
echo ====================================================
echo BUILD ABGESCHLOSSEN!
if exist "dist\windows\%APP_NAME%.exe" echo Windows: dist\windows\%APP_NAME%.exe
if exist "dist\linux\%APP_NAME%" echo Linux: dist\linux\%APP_NAME%
echo ====================================================
endlocal
exit /b 0
REM ====================================================
REM HILFSROUTINEN
REM ====================================================
:FIND_PYTHON
set "WIN_PY="
set "USING_VENV=0"
if exist ".venv\Scripts\python.exe" (
".venv\Scripts\python.exe" -m pip --version >nul 2>nul && (
set "WIN_PY=.venv\Scripts\python.exe"
set "USING_VENV=1"
)
)
if "%WIN_PY%"=="" (
where python >nul 2>nul && (
python -c "import sys, tkinter; sys.exit(0)" >nul 2>nul && set "WIN_PY=python"
)
)
if "%WIN_PY%"=="" (
where py >nul 2>nul && (
py -3 -c "import sys, tkinter; sys.exit(0)" >nul 2>nul && set "WIN_PY=py -3"
)
)
exit /b 0
:FIND_WSL_DISTRO
set "WSL_DISTRO="
where wsl.exe >nul 2>nul
if errorlevel 1 exit /b 0
for /f "usebackq delims=" %%L in (`powershell -NoProfile -Command "$d = (wsl.exe -l -q 2>$null) -replace '\x00','' | ForEach-Object { $_.Trim() } | Where-Object { $_ -and $_ -notmatch '^docker-desktop' }; if ($d) { $d | Select-Object -First 1 }"`) do (
if "!WSL_DISTRO!"=="" set "WSL_DISTRO=%%L"
)
exit /b 0