#!/usr/bin/env bash # # Builds the qol-tweaks plugin jar. # # The build pins a Java toolchain version in build.gradle.kts. This script # finds that version on your machine, and if it isn't installed it transparently # falls back to the highest JDK that is (so `./build.sh` just works). # # Usage: ./build.sh [extra gradle args...] set -euo pipefail cd "$(dirname "$0")" # --- 1. which Java version does the build pin? -------------------------------- pinned="$(grep -oE 'JavaLanguageVersion\.of\([0-9]+\)' build.gradle.kts | grep -oE '[0-9]+' | head -1 || true)" pinned="${pinned:-25}" # --- 2. find an installed JDK for a given version ------------------------------ java_major() { # $1 = path to java binary; echoes its major version (e.g. 26) "$1" -version 2>&1 | grep -oE '"([0-9]+)' | head -1 | tr -d '"' } find_java_home() { # $1 = version (e.g. 25); echoes a home whose java matches, or fails local -a candidates=() if [[ -x /usr/libexec/java_home ]]; then local mac_home if mac_home="$(/usr/libexec/java_home -v "$1" 2>/dev/null)"; then candidates+=("$mac_home") fi fi [[ -n "${JAVA_HOME:-}" ]] && candidates+=("$JAVA_HOME") local home bin ver for home in "${candidates[@]}"; do bin="$home/bin/java" [[ -x "$bin" ]] || continue ver="$(java_major "$bin")" if [[ -n "$ver" && "$ver" == "$1" ]]; then echo "$home" return 0 fi done return 1 } # --- 3. pick the JDK to build with -------------------------------------------- selected="" selected_home="" for v in "$pinned" 26 25 24 23 22 21 20 17; do if h="$(find_java_home "$v")"; then selected="$v" selected_home="$h" break fi done if [[ -z "$selected" ]]; then echo "error: no JDK found. Install any JDK 17+ (e.g. Java 25) and try again." >&2 exit 1 fi export JAVA_HOME="$selected_home" export PATH="$JAVA_HOME/bin:$PATH" gradle_args=() override_init="" if [[ "$selected" != "$pinned" ]]; then echo "note: pinned JDK $pinned not found; building with installed JDK $selected." override_init="${TMPDIR:-/tmp}/qol-tweaks-toolchain-$pinned-$selected.gradle" cat > "$override_init" < if (p.plugins.hasPlugin('java')) { p.extensions.findByName('java').toolchain.languageVersion.set(JavaLanguageVersion.of($selected)) p.tasks.withType(JavaCompile).configureEach { options.release.set($pinned) } } } EOF gradle_args+=(-I "$override_init") # the init script is external input, so a cached configuration would be stale gradle_args+=(--no-configuration-cache) fi trap '[[ -n "$override_init" ]] && rm -f "$override_init"' EXIT if [[ $# -eq 0 ]]; then gradle_args+=(build) else gradle_args+=("$@") fi echo "Using JDK $selected at $JAVA_HOME" ./gradlew "${gradle_args[@]}" jar="$(ls -1 build/libs/*.jar 2>/dev/null | grep -Ev '(-sources|-javadoc)\.jar$' | head -1 || true)" if [[ -n "$jar" ]]; then echo echo "Done. Plugin jar: $(pwd)/$jar" fi