From b2421c9b848a9bc17d6e61062ed834b562f9b41d Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 12 Dec 2025 21:30:03 +0100 Subject: [PATCH] **Refactor OS detection and normalize Manjaro to Arch** * Centralize OS detection and normalization in a dedicated resolver module * Treat Manjaro consistently as Arch across dependencies and package install * Remove duplicated OS logic and legacy lib.sh * Rename installation entrypoint to init.sh and update Makefile accordingly https://chatgpt.com/share/693c7b50-3be0-800f-8aeb-daf3ee929ea3 --- Makefile | 2 +- scripts/installation/dependencies.sh | 21 +++--- scripts/installation/{main.sh => init.sh} | 0 scripts/installation/lib.sh | 12 ---- scripts/installation/os_resolver.sh | 82 +++++++++++++++++++++++ scripts/installation/package.sh | 27 +++----- 6 files changed, 101 insertions(+), 43 deletions(-) rename scripts/installation/{main.sh => init.sh} (100%) delete mode 100755 scripts/installation/lib.sh create mode 100644 scripts/installation/os_resolver.sh diff --git a/Makefile b/Makefile index deb75f8..6ae9ab0 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ export TEST_PATTERN # ------------------------------------------------------------ install: @echo "Building and installing distro-native package-manager for this system..." - @bash scripts/installation/main.sh + @bash scripts/installation/init.sh # ------------------------------------------------------------ # PKGMGR setup diff --git a/scripts/installation/dependencies.sh b/scripts/installation/dependencies.sh index 41187b7..f16f330 100755 --- a/scripts/installation/dependencies.sh +++ b/scripts/installation/dependencies.sh @@ -3,22 +3,19 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=/dev/null -source "${SCRIPT_DIR}/lib.sh" +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/os_resolver.sh" -OS_ID="$(detect_os_id)" +OS_ID="$(osr_get_os_id)" echo "[run-dependencies] Detected OS: ${OS_ID}" -case "${OS_ID}" in - arch|debian|ubuntu|fedora|centos) - DEP_SCRIPT="${SCRIPT_DIR}/${OS_ID}/dependencies.sh" - ;; - *) - echo "[run-dependencies] Unsupported OS: ${OS_ID}" - exit 1 - ;; -esac +if ! osr_is_supported "${OS_ID}"; then + echo "[run-dependencies] Unsupported OS: ${OS_ID}" + exit 1 +fi + +DEP_SCRIPT="$(osr_script_path_for "${SCRIPT_DIR}" "${OS_ID}" "dependencies")" if [[ ! -f "${DEP_SCRIPT}" ]]; then echo "[run-dependencies] Dependency script not found: ${DEP_SCRIPT}" diff --git a/scripts/installation/main.sh b/scripts/installation/init.sh similarity index 100% rename from scripts/installation/main.sh rename to scripts/installation/init.sh diff --git a/scripts/installation/lib.sh b/scripts/installation/lib.sh deleted file mode 100755 index 9838647..0000000 --- a/scripts/installation/lib.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -detect_os_id() { - if [[ -f /etc/os-release ]]; then - # shellcheck disable=SC1091 - . /etc/os-release - echo "${ID:-unknown}" - else - echo "unknown" - fi -} diff --git a/scripts/installation/os_resolver.sh b/scripts/installation/os_resolver.sh new file mode 100644 index 0000000..8b1005d --- /dev/null +++ b/scripts/installation/os_resolver.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +set -euo pipefail + +# ----------------------------------------------------------------------------- +# OsResolver (bash "class-style" module) +# Centralizes OS detection + normalization + supported checks + script paths. +# ----------------------------------------------------------------------------- + +osr_detect_raw_id() { + if [[ -f /etc/os-release ]]; then + # shellcheck disable=SC1091 + . /etc/os-release + echo "${ID:-unknown}" + else + echo "unknown" + fi +} + +osr_detect_id_like() { + if [[ -f /etc/os-release ]]; then + # shellcheck disable=SC1091 + . /etc/os-release + echo "${ID_LIKE:-}" + else + echo "" + fi +} + +osr_normalize_id() { + local raw="${1:-unknown}" + local like="${2:-}" + + # Explicit mapping first (your bugfix: manjaro -> arch everywhere) + case "${raw}" in + manjaro) echo "arch"; return 0 ;; + esac + + # Keep direct IDs when they are already supported + case "${raw}" in + arch|debian|ubuntu|fedora|centos) echo "${raw}"; return 0 ;; + esac + + # Fallback mapping via ID_LIKE for better portability + # Example: many Arch derivatives expose ID_LIKE="arch" + if [[ " ${like} " == *" arch "* ]]; then + echo "arch"; return 0 + fi + if [[ " ${like} " == *" debian "* ]]; then + echo "debian"; return 0 + fi + if [[ " ${like} " == *" fedora "* ]]; then + echo "fedora"; return 0 + fi + if [[ " ${like} " == *" rhel "* || " ${like} " == *" centos "* ]]; then + echo "centos"; return 0 + fi + + echo "${raw}" +} + +osr_get_os_id() { + local raw like + raw="$(osr_detect_raw_id)" + like="$(osr_detect_id_like)" + osr_normalize_id "${raw}" "${like}" +} + +osr_is_supported() { + local id="${1:-unknown}" + case "${id}" in + arch|debian|ubuntu|fedora|centos) return 0 ;; + *) return 1 ;; + esac +} + +osr_script_path_for() { + local script_dir="${1:?script_dir required}" + local os_id="${2:?os_id required}" + local kind="${3:?kind required}" # "dependencies" or "package" + + echo "${script_dir}/${os_id}/${kind}.sh" +} diff --git a/scripts/installation/package.sh b/scripts/installation/package.sh index b006046..8bf05c2 100755 --- a/scripts/installation/package.sh +++ b/scripts/installation/package.sh @@ -3,28 +3,19 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=/dev/null -source "${SCRIPT_DIR}/lib.sh" +# shellcheck disable=SC1091 +source "${SCRIPT_DIR}/os_resolver.sh" -OS_ID="$(detect_os_id)" - -# Map Manjaro to Arch -if [[ "${OS_ID}" == "manjaro" ]]; then - echo "[package] Mapping OS 'manjaro' → 'arch'" - OS_ID="arch" -fi +OS_ID="$(osr_get_os_id)" echo "[package] Detected OS: ${OS_ID}" -case "${OS_ID}" in - arch|debian|ubuntu|fedora|centos) - PKG_SCRIPT="${SCRIPT_DIR}/${OS_ID}/package.sh" - ;; - *) - echo "[package] Unsupported OS: ${OS_ID}" - exit 1 - ;; -esac +if ! osr_is_supported "${OS_ID}"; then + echo "[package] Unsupported OS: ${OS_ID}" + exit 1 +fi + +PKG_SCRIPT="$(osr_script_path_for "${SCRIPT_DIR}" "${OS_ID}" "package")" if [[ ! -f "${PKG_SCRIPT}" ]]; then echo "[package] Package script not found: ${PKG_SCRIPT}"