From 7e48f01cca9aec0247a29ccd46cc2385500256a6 Mon Sep 17 00:00:00 2001 From: Aditya Gupta Date: Sun, 21 Jun 2026 15:55:11 +0530 Subject: [PATCH] refactor: Improve directory resolution logic --- bootstrap.sh | 46 +++++++++++++++++++--------------------------- lib/routes.sh | 34 ++++++++++++---------------------- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index e6349da..78b4254 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -25,19 +25,16 @@ _SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd 2>/dev/null || pwd)" if [ -f "$_SCRIPT_DIR/lib/common.sh" ]; then # Dev/local mode: source directly from repo - . "$_SCRIPT_DIR/lib/common.sh" - . "$_SCRIPT_DIR/lib/platform.sh" - . "$_SCRIPT_DIR/lib/shell_config.sh" + BOOTSTRAP_SOURCE_DIR="$_SCRIPT_DIR" elif [ -f "$BOOTSTRAP_DIR/lib/common.sh" ]; then # Installed mode: source from bootstrap dir - . "$BOOTSTRAP_DIR/lib/common.sh" - . "$BOOTSTRAP_DIR/lib/platform.sh" - . "$BOOTSTRAP_DIR/lib/shell_config.sh" + BOOTSTRAP_SOURCE_DIR="$BOOTSTRAP_DIR" else # Standalone/remote mode: download to a temp directory and source export BOOTSTRAP_TMP_DIR BOOTSTRAP_TMP_DIR="$(mktemp -d)" trap 'rm -rf "$BOOTSTRAP_TMP_DIR"' EXIT + BOOTSTRAP_SOURCE_DIR="$BOOTSTRAP_TMP_DIR" _BASE_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master" _LIBS=("lib/common.sh" "lib/platform.sh" "lib/shell_config.sh") @@ -48,15 +45,15 @@ else _curl_args+=("-o" "$BOOTSTRAP_TMP_DIR/$_lib" "$_BASE_URL/$_lib") done curl -fsSL "${_curl_args[@]}" 2>/dev/null - - if [ -f "$BOOTSTRAP_TMP_DIR/lib/common.sh" ]; then - . "$BOOTSTRAP_TMP_DIR/lib/common.sh" - . "$BOOTSTRAP_TMP_DIR/lib/platform.sh" - . "$BOOTSTRAP_TMP_DIR/lib/shell_config.sh" - else - echo "Error: Failed to download bootstrap libraries." >&2 - exit 1 - fi +fi + +if [ -f "$BOOTSTRAP_SOURCE_DIR/lib/common.sh" ]; then + . "$BOOTSTRAP_SOURCE_DIR/lib/common.sh" + . "$BOOTSTRAP_SOURCE_DIR/lib/platform.sh" + . "$BOOTSTRAP_SOURCE_DIR/lib/shell_config.sh" +else + echo "Error: Failed to locate or download bootstrap libraries." >&2 + exit 1 fi # Install/update the bootstrap loader and download all necessary files @@ -172,19 +169,14 @@ if [ "$is_sourced" = false ]; then clear 2>/dev/null || true # Locate or download pixel_art.ansi and VERSION - _art_file="$_SCRIPT_DIR/assets/pixel_art.ansi" - _version_file="$_SCRIPT_DIR/VERSION" + _art_file="$BOOTSTRAP_SOURCE_DIR/assets/pixel_art.ansi" + _version_file="$BOOTSTRAP_SOURCE_DIR/VERSION" - if [ ! -f "$_art_file" ]; then - if [ -n "${BOOTSTRAP_TMP_DIR:-}" ] && [ -d "$BOOTSTRAP_TMP_DIR" ]; then - _art_file="$BOOTSTRAP_TMP_DIR/pixel_art.ansi" - _version_file="$BOOTSTRAP_TMP_DIR/VERSION" - _base_url="${_BASE_URL:-https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master}" - if [ ! -f "$_art_file" ]; then - curl -fsSL -o "$_art_file" "$_base_url/assets/pixel_art.ansi" 2>/dev/null - curl -fsSL -o "$_version_file" "$_base_url/VERSION" 2>/dev/null - fi - fi + if [ ! -f "$_art_file" ] && [ -n "${BOOTSTRAP_TMP_DIR:-}" ]; then + _base_url="${_BASE_URL:-https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master}" + mkdir -p "$(dirname "$_art_file")" + curl -fsSL -o "$_art_file" "$_base_url/assets/pixel_art.ansi" 2>/dev/null || true + curl -fsSL -o "$_version_file" "$_base_url/VERSION" 2>/dev/null || true fi if [ -f "$_art_file" ]; then diff --git a/lib/routes.sh b/lib/routes.sh index ef3cfb8..b987991 100755 --- a/lib/routes.sh +++ b/lib/routes.sh @@ -2,42 +2,32 @@ # Central routing script for bootstrap installers. # This file is updated automatically by the 'b' command. -BOOTSTRAP_DIR="${BOOTSTRAP_DIR:-$HOME/.config/bootstrap}" +_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd 2>/dev/null || pwd)" +BOOTSTRAP_DIR="${BOOTSTRAP_DIR:-$(dirname "$_LIB_DIR")}" + +# Fallback to ~/.config/bootstrap if not found locally +if [ ! -d "$BOOTSTRAP_DIR/lib" ]; then + BOOTSTRAP_DIR="$HOME/.config/bootstrap" +fi # Source common library if [ -f "$BOOTSTRAP_DIR/lib/common.sh" ]; then . "$BOOTSTRAP_DIR/lib/common.sh" else - # Fallback/Bootstrap case if lib is not installed yet echo "Error: Bootstrap libraries not found at $BOOTSTRAP_DIR/lib/" >&2 exit 1 fi require_bash -_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd 2>/dev/null || pwd)" - # Source registry -if [ -f "$_SCRIPT_DIR/registry.sh" ]; then - . "$_SCRIPT_DIR/registry.sh" -elif [ -f "$BOOTSTRAP_DIR/lib/registry.sh" ]; then +if [ -f "$BOOTSTRAP_DIR/lib/registry.sh" ]; then . "$BOOTSTRAP_DIR/lib/registry.sh" else - # Standalone/remote fallback: download registry - _tmp_registry=$(mktemp) - BOOTSTRAP_BASE_URL="${BOOTSTRAP_BASE_URL:-https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master}" - BOOTSTRAP_FALLBACK_URL="${BOOTSTRAP_FALLBACK_URL:-https://raw.githubusercontent.com/sortedcord/bootstrap/refs/heads/master}" - curl -fsSL "${BOOTSTRAP_BASE_URL}/lib/registry.sh" -o "$_tmp_registry" 2>/dev/null || \ - curl -fsSL "${BOOTSTRAP_FALLBACK_URL}/lib/registry.sh" -o "$_tmp_registry" 2>/dev/null - if [ -s "$_tmp_registry" ]; then - . "$_tmp_registry" - else - # Critical fallback - declare -A INSTALLERS - declare -A INSTALLER_DISPLAYS - INSTALLER_KEYS=() - fi - rm -f "$_tmp_registry" + # Critical fallback + declare -A INSTALLERS + declare -A INSTALLER_DISPLAYS + INSTALLER_KEYS=() fi # Helper function to run/edit installer scripts