the_goth_cardgame_collection/build_scripts/build.sh

233 lines
9.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# Lokales Build-Skript (nur Linux & Windows via Wine)
# Ablageort: build_scripts/build.sh
# Targets: --Win | --Unix | --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"
VERSION_TXT="${PROJECT_ROOT}/build_scripts/version_info.txt"
UNAME_OUT="$(uname -s)"
case "${UNAME_OUT}" in
Linux*) ;;
*) echo "❌ Dieses Skript unterstützt nur Linux-Systeme (gefunden: ${UNAME_OUT})."; exit 1 ;;
esac
DO_WIN=0
DO_UNIX=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 ;;
--current) DO_UNIX=1 ;;
--all) DO_WIN=1; DO_UNIX=1 ;;
*)
echo "❌ Unbekannte Option: $arg (Erlaubt: --Win | --Unix | --current | --all | --clean)"
exit 1
;;
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
# Standardziel ohne Parameter: nativer Linux-Build
if [ "$DO_WIN" -eq 0 ] && [ "$DO_UNIX" -eq 0 ]; then
DO_UNIX=1
fi
# Root/Sudo Check unter Linux falls Systempakete fehlen
NEEDS_SYS_INSTALL=0
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
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. NATIVER LINUX BUILD (--Unix)
if [ "$DO_UNIX" -eq 1 ]; 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 --noupx --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 (--Win)
if [ "$DO_WIN" -eq 1 ]; 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 curl
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
if ! "${USER_RUNNER[@]}" wine "$WINE_PY" -c "import PIL, pygame, PyInstaller" &>/dev/null; then
echo "---> [WIN] Installiere fehlende Python-Pakete in Wine..."
"${USER_RUNNER[@]}" wine "$WINE_PY" -m pip install --upgrade pip pillow pygame pyinstaller
fi
WINE_ICON_ARG=()
[ -f "$ICON_ICO" ] && WINE_ICON_ARG=(--icon "${WIN_ROOT}\\resources\\images\\game_icon.ico")
WINE_VER_ARG=()
[ -f "$VERSION_TXT" ] && WINE_VER_ARG=(--version-file "${WIN_ROOT}\\build_scripts\\version_info.txt")
"${USER_RUNNER[@]}" wine "$WINE_PY" -m PyInstaller \
--noconfirm --noconsole --onefile --noupx --clean \
--distpath "${WIN_ROOT}\\dist\\windows" \
--workpath "${WIN_ROOT}\\build\\windows" \
--specpath "${WIN_ROOT}\\build\\windows" \
"${WINE_ICON_ARG[@]}" \
"${WINE_VER_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
if [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "${SUDO_USER:-}" ]; then
chown -R "$SUDO_USER:$SUDO_USER" "${PROJECT_ROOT}/dist" 2>/dev/null || true
fi