the_goth_cardgame_collection/build_scripts/build.sh

276 lines
11 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# Lokales Build-Skript (Linux / WSL / macOS-Zielsystem)
# Ablageort: build_scripts/build.sh
# Targets: --Win | --Unix | --OSX | --current | --all | --clean
set -e
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${PROJECT_ROOT}"
APP_NAME="TheGothCardgameCollection"
ICON_ICO="${PROJECT_ROOT}/resources/images/game_icon.ico"
UNAME_OUT="$(uname -s)"
case "${UNAME_OUT}" in
Linux*) HOST_OS="linux" ;;
Darwin*) HOST_OS="macos" ;;
*) echo "❌ Unbekanntes System: ${UNAME_OUT}"; exit 1 ;;
esac
DO_WIN=0
DO_UNIX=0
DO_OSX=0
DO_CLEAN=0
ORIG_ARGS=("$@")
for arg in "$@"; do
arg_lc="$(printf '%s' "$arg" | tr '[:upper:]' '[:lower:]')"
case "$arg_lc" in
--clean) DO_CLEAN=1 ;;
--win|--windows) DO_WIN=1 ;;
--unix|--linux) DO_UNIX=1 ;;
--osx|--macos) DO_OSX=1 ;;
--current)
if [ "$HOST_OS" = "macos" ]; then DO_OSX=1; else DO_UNIX=1; fi
;;
--all)
if [ "$HOST_OS" = "macos" ]; then
DO_OSX=1
else
DO_WIN=1; DO_UNIX=1
fi
;;
esac
done
if [ "$DO_CLEAN" -eq 1 ]; then
rm -rf "${PROJECT_ROOT}/build" "${PROJECT_ROOT}"/.venv_build_* "${PROJECT_ROOT}"/*.spec
echo "🧹 Temporäre Build-Ordner bereinigt."
exit 0
fi
if [ "$DO_WIN" -eq 0 ] && [ "$DO_UNIX" -eq 0 ] && [ "$DO_OSX" -eq 0 ]; then
if [ "$HOST_OS" = "macos" ]; then DO_OSX=1; else DO_UNIX=1; fi
fi
# Root/Sudo Check unter Linux falls Systempakete fehlen
NEEDS_SYS_INSTALL=0
if [ "$HOST_OS" = "linux" ]; then
if [ "$DO_UNIX" -eq 1 ]; then
if ! command -v python3 &>/dev/null || ! python3 -c "import tkinter" &>/dev/null || ! python3 -m pip --version &>/dev/null; then
NEEDS_SYS_INSTALL=1
fi
fi
if [ "$DO_WIN" -eq 1 ] && ! command -v wine &>/dev/null; then
NEEDS_SYS_INSTALL=1
fi
if [ "$NEEDS_SYS_INSTALL" -eq 1 ] && [ "${EUID:-$(id -u)}" -ne 0 ]; then
echo "🔒 Fehlende Systempakete erkannt – fordere sudo an..."
exec sudo -E bash "$SCRIPT_PATH" "${ORIG_ARGS[@]}"
fi
fi
if [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "${SUDO_USER:-}" ]; then
REAL_USER="$SUDO_USER"
REAL_HOME="$(getent passwd "$SUDO_USER" | cut -d: -f6)"
USER_RUNNER=(sudo -u "$REAL_USER" HOME="$REAL_HOME")
else
REAL_USER="$(whoami)"
REAL_HOME="$HOME"
USER_RUNNER=()
fi
install_linux_pkgs() {
if command -v apt-get &>/dev/null; then
apt-get update -qq && apt-get install -y "$@"
elif command -v dnf &>/dev/null; then
dnf install -y "$@"
elif command -v yum &>/dev/null; then
yum install -y "$@"
elif command -v zypper &>/dev/null; then
zypper --non-interactive install "$@"
elif command -v pacman &>/dev/null; then
pacman -Sy --noconfirm --needed "$@"
fi
}
rebuild_internal_db_if_present() {
local py_bin="$1"
local rebuild_py=""
if [ -f "${PROJECT_ROOT}/rebuild_internal_db.py" ]; then
rebuild_py="${PROJECT_ROOT}/rebuild_internal_db.py"
elif [ -f "${PROJECT_ROOT}/build_scripts/rebuild_internal_db.py" ]; then
rebuild_py="${PROJECT_ROOT}/build_scripts/rebuild_internal_db.py"
fi
if [ -n "$rebuild_py" ] && [ -d "${PROJECT_ROOT}/resources/code_res/sql" ]; then
echo "---> [DB] Aktualisiere internal_base.db aus modularen SQL-Dateien..."
"${USER_RUNNER[@]}" "$py_bin" "$rebuild_py"
fi
}
DB_REBUILT=0
# 1. LINUX BUILD
if [ "$DO_UNIX" -eq 1 ] && [ "$HOST_OS" = "linux" ]; then
echo "---> [UNIX] Baue Linux Executable..."
if [ "$NEEDS_SYS_INSTALL" -eq 1 ]; then
if command -v apt-get &>/dev/null; then
install_linux_pkgs python3 python3-pip python3-venv python3-tk wget
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
install_linux_pkgs python3 python3-pip python3-tkinter wget
elif command -v zypper &>/dev/null; then
install_linux_pkgs python3 python3-pip python3-tk wget
elif command -v pacman &>/dev/null; then
install_linux_pkgs python python-pip tk wget
fi
fi
for VDIR in "${PROJECT_ROOT}/.venv_build_linux" "${PROJECT_ROOT}/.venv" "${PROJECT_ROOT}/venv"; do
if [ -d "$VDIR" ] && [ ! -x "$VDIR/bin/python3" ]; then rm -rf "$VDIR"; fi
done
LINUX_PY="python3"
[ -x "${PROJECT_ROOT}/.venv/bin/python3" ] && LINUX_PY="${PROJECT_ROOT}/.venv/bin/python3"
if [ "$DB_REBUILT" -eq 0 ]; then
rebuild_internal_db_if_present "$LINUX_PY"
DB_REBUILT=1
fi
if ! "$LINUX_PY" -c "import PIL, pygame, PyInstaller" &>/dev/null; then
PIP_USER_FLAG=()
[ "$LINUX_PY" = "python3" ] && [ "${EUID:-$(id -u)}" -ne 0 ] && PIP_USER_FLAG=(--user)
"$LINUX_PY" -m pip install "${PIP_USER_FLAG[@]}" --upgrade pillow pygame pyinstaller --break-system-packages 2>/dev/null \
|| "$LINUX_PY" -m pip install "${PIP_USER_FLAG[@]}" --upgrade pillow pygame pyinstaller
fi
ICON_ARG=()
[ -f "$ICON_ICO" ] && ICON_ARG=(--icon "$ICON_ICO")
"${USER_RUNNER[@]}" "$LINUX_PY" -m PyInstaller \
--noconfirm --noconsole --onefile --clean \
--distpath "${PROJECT_ROOT}/dist/linux" \
--workpath "${PROJECT_ROOT}/build/linux" \
--specpath "${PROJECT_ROOT}/build/linux" \
"${ICON_ARG[@]}" \
--add-data "${PROJECT_ROOT}/resources:resources" \
--add-data "${PROJECT_ROOT}/games:games" \
--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}" "${PROJECT_ROOT}/main.py"
chmod +x "${PROJECT_ROOT}/dist/linux/${APP_NAME}"
rm -rf "${PROJECT_ROOT}/build/linux"
echo "✔ Linux-Build fertig: dist/linux/${APP_NAME}"
fi
# 2. WINDOWS BUILD VIA WINE (nur auf Linux)
if [ "$DO_WIN" -eq 1 ] && [ "$HOST_OS" = "linux" ]; then
echo "---> [WIN] Baue Windows .exe (via Wine)..."
if ! command -v wine &>/dev/null; then
command -v apt-get &>/dev/null && dpkg --add-architecture amd64 2>/dev/null || true
install_linux_pkgs wine wget
fi
if [ "$DB_REBUILT" -eq 0 ] && command -v python3 &>/dev/null; then
rebuild_internal_db_if_present "python3"
DB_REBUILT=1
fi
WINE_DRIVE_C="${REAL_HOME}/.wine/drive_c"
WINE_PY=$(find "$WINE_DRIVE_C" -iname "python.exe" 2>/dev/null | grep -i "Python3" | head -n 1 || true)
if [ -z "$WINE_PY" ]; then
INSTALLER="/tmp/python-3.11.9-amd64.exe"
[ ! -f "$INSTALLER" ] && curl -fsSL "https://www.python.org/ftp/python/3.11.9/python-3.11.9-amd64.exe" -o "$INSTALLER"
"${USER_RUNNER[@]}" wine "$INSTALLER" /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
"${USER_RUNNER[@]}" wineserver -w || true
rm -f "$INSTALLER"
WINE_PY=$(find "$WINE_DRIVE_C" -iname "python.exe" 2>/dev/null | grep -i "Python3" | head -n 1 || true)
fi
WIN_ROOT="$("${USER_RUNNER[@]}" winepath -w "${PROJECT_ROOT}")"
if [ "$DB_REBUILT" -eq 0 ]; then
if [ -f "${PROJECT_ROOT}/rebuild_internal_db.py" ]; then
echo "---> [DB] Aktualisiere internal_base.db via Wine-Python..."
"${USER_RUNNER[@]}" wine "$WINE_PY" "${WIN_ROOT}\\rebuild_internal_db.py"
elif [ -f "${PROJECT_ROOT}/build_scripts/rebuild_internal_db.py" ]; then
echo "---> [DB] Aktualisiere internal_base.db via Wine-Python..."
"${USER_RUNNER[@]}" wine "$WINE_PY" "${WIN_ROOT}\\build_scripts\\rebuild_internal_db.py"
fi
DB_REBUILT=1
fi
"${USER_RUNNER[@]}" wine "$WINE_PY" -m pip install --upgrade pip pillow pygame pyinstaller
WINE_ICON_ARG=()
[ -f "$ICON_ICO" ] && WINE_ICON_ARG=(--icon "${WIN_ROOT}\\resources\\images\\game_icon.ico")
"${USER_RUNNER[@]}" wine "$WINE_PY" -m PyInstaller \
--noconfirm --noconsole --onefile --clean \
--distpath "${WIN_ROOT}\\dist\\windows" \
--workpath "${WIN_ROOT}\\build\\windows" \
--specpath "${WIN_ROOT}\\build\\windows" \
"${WINE_ICON_ARG[@]}" \
--add-data "${WIN_ROOT}\\resources;resources" \
--add-data "${WIN_ROOT}\\games;games" \
--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}" "${WIN_ROOT}\\main.py"
rm -rf "${PROJECT_ROOT}/build/windows"
echo "✔ Windows-Build fertig: dist/windows/${APP_NAME}.exe"
fi
# 3. NATIVER MACOS BUILD (wird direkt in der macOS-VM ausgeführt)
if [ "$DO_OSX" -eq 1 ] && [ "$HOST_OS" = "macos" ]; then
echo "---> [OSX] Baue native macOS Binary + .app Bundle..."
export PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/opt/homebrew/bin:$PATH"
OSX_VENV="${PROJECT_ROOT}/.venv_build_macos"
if [ -d "$OSX_VENV" ] && [ ! -x "$OSX_VENV/bin/python3" ]; then rm -rf "$OSX_VENV"; fi
[ ! -d "$OSX_VENV" ] && python3 -m venv "$OSX_VENV"
if [ "$DB_REBUILT" -eq 0 ]; then
rebuild_internal_db_if_present "$OSX_VENV/bin/python3"
DB_REBUILT=1
fi
"$OSX_VENV/bin/python3" -m pip install --upgrade pip pillow pygame pyinstaller
OSX_ICON_ARG=()
if [ -f "${PROJECT_ROOT}/resources/images/game_icon.icns" ]; then
OSX_ICON_ARG=(--icon "${PROJECT_ROOT}/resources/images/game_icon.icns")
elif [ -f "$ICON_ICO" ]; then
OSX_ICON_ARG=(--icon "$ICON_ICO")
fi
"$OSX_VENV/bin/python3" -m PyInstaller \
--noconfirm --windowed --onefile --clean \
--osx-bundle-identifier "de.kuro.thegothcardgamecollection" \
--distpath "${PROJECT_ROOT}/dist/macos" \
--workpath "${PROJECT_ROOT}/build/macos" \
--specpath "${PROJECT_ROOT}/build/macos" \
"${OSX_ICON_ARG[@]}" \
--add-data "${PROJECT_ROOT}/resources:resources" \
--add-data "${PROJECT_ROOT}/games:games" \
--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}" "${PROJECT_ROOT}/main.py"
chmod +x "${PROJECT_ROOT}/dist/macos/${APP_NAME}" 2>/dev/null || true
rm -rf "${PROJECT_ROOT}/build/macos"
echo "✔ macOS-Build fertig: dist/macos/${APP_NAME} (.app)"
fi
if [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "${SUDO_USER:-}" ]; then
chown -R "$SUDO_USER:$SUDO_USER" "${PROJECT_ROOT}/dist" 2>/dev/null || true
fi