Calci/run.sh
2025-09-19 11:41:24 +02:00

211 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
APP_NAME="calci"
BUILD_DIR="build"
MAIN_CLASS="Main"
USE_UI=0
IGNORE_PATTERNS=()
########################################
# USER-FACING (CLI surface)
########################################
print_help() {
cat <<EOF
$APP_NAME runner
Usage: ./run.sh [options]
Options:
--main <Class> Set the main class to run (default: Main)
--ui Enable JavaFX modules (tries to auto-detect JAVAFX_HOME)
--ignore a|b|c Ignore files/dirs (split by |) when copying resources
--help Show this help message
EOF
}
########################################
# BACKEND (work logic)
########################################
compile_sources() {
echo "🛠️ Compiling sources..."
if [ $USE_UI -eq 1 ]; then
detect_javafx
javac --module-path "$JAVAFX_HOME/lib" \
--add-modules javafx.controls,javafx.fxml,javafx.swing \
-d "$BUILD_DIR" $(find src -name "*.java")
else
javac -d "$BUILD_DIR" $(find src -name "*.java")
fi
}
copy_resources() {
echo "📦 Copying resources..."
# Build rsync exclude flags
local excludes=( "--exclude=*.java" )
for pat in "${IGNORE_PATTERNS[@]}"; do
excludes+=( "--exclude=$pat" )
done
rsync -a "${excludes[@]}" ./ "$BUILD_DIR"/
}
run_app() {
echo "🚀 Running $MAIN_CLASS..."
if [ $USE_UI -eq 1 ]; then
java --module-path "$JAVAFX_HOME/lib" \
--add-modules javafx.controls,javafx.fxml,javafx.swing \
-cp "$BUILD_DIR" "$MAIN_CLASS"
else
java -cp "$BUILD_DIR" "$MAIN_CLASS"
fi
}
detect_javafx() {
if [ -n "$JAVAFX_HOME" ]; then
return
fi
echo "🔍 Trying to auto-detect JavaFX SDK..."
for path in \
"/usr/share/openjfx" \
"/usr/share/java/openjfx" \
"/usr/lib/jvm/javafx-sdk-21" \
"/usr/lib/jvm/javafx-sdk-17" \
"$HOME/javafx-sdk-21" \
"$HOME/javafx-sdk-17"; do
if [ -d "$path/lib" ]; then
JAVAFX_HOME="$path"
echo "✅ Found JavaFX SDK at $JAVAFX_HOME"
return
fi
done
ask_user_install_javafx
if [ ! -d "$JAVAFX_HOME/lib" ]; then
echo "Warning: Installed JavaFX but still cannot find lib/. Please set JAVAFX_HOME manually."
exit 1
fi
}
########################################
# HELPERS (system checks, installs)
########################################
ask_install() {
local tool=$1
if ! command -v "$tool" >/dev/null 2>&1; then
echo "Error: $tool is not installed."
read -rp "Do you want to install $tool now? (y/n) " choice
case "$choice" in
y|Y)
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update && sudo apt-get install -y default-jdk
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y java-17-openjdk-devel
elif command -v pacman >/dev/null 2>&1; then
sudo pacman -S --noconfirm jdk-openjdk
else
echo "Package manager not supported, please install $tool manually."
exit 1
fi
;;
*) exit 1 ;;
esac
fi
}
try_install_javafx() {
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update && sudo apt-get install -y openjfx
JAVAFX_HOME="/usr/share/openjfx"
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y openjfx
JAVAFX_HOME="/usr/share/openjfx"
elif command -v pacman >/dev/null 2>&1; then
sudo pacman -S --noconfirm javafx21-openjdk
JAVAFX_HOME="/usr/lib/jvm/javafx-sdk-21"
else
return 1
fi
return 0
}
ask_user_install_javafx() {
echo "❌ Could not find JavaFX SDK."
read -rp "Do you want to install JavaFX now? (y/n) " choice
case "$choice" in
y|Y)
try_install_javafx || ask_user_custom_command
;;
*)
echo "Please install JavaFX manually and set JAVAFX_HOME."
exit 1
;;
esac
}
ask_user_custom_command() {
echo "⚠️ No supported package manager detected."
read -rp "Do you want to enter a custom install command? (y/n) " custom
case "$custom" in
y|Y)
read -rp "Enter install command: " cmd
if [ -n "$cmd" ]; then
echo "Running: $cmd"
if ! eval "$cmd"; then
echo "❌ Installation command failed."
exit 1
fi
else
echo "No command entered. Aborting."
exit 1
fi
;;
*)
echo "Please download JavaFX manually:"
echo "👉 https://gluonhq.com/products/javafx/"
exit 1
;;
esac
}
########################################
# ENTRYPOINT
########################################
while [[ $# -gt 0 ]]; do
case "$1" in
--main)
MAIN_CLASS="$2"
shift 2
;;
--ui)
USE_UI=1
shift
;;
--ignore)
IFS='|' read -ra IGNORE_PATTERNS <<< "$2"
shift 2
;;
--help)
print_help
exit 0
;;
*)
echo "Unknown option: $1"
print_help
exit 1
;;
esac
done
ask_install javac
ask_install java
mkdir -p "$BUILD_DIR"
compile_sources
copy_resources
run_app