From a56bee0b9c102050af865a0d826c5e7e4afe9c04 Mon Sep 17 00:00:00 2001 From: Aditya Gupta Date: Mon, 22 Jun 2026 09:11:39 +0530 Subject: [PATCH] refactor: remove bootstrap spaghetti from installers --- .agents/skills/add_installer/SKILL.md | 22 ++++++---------------- bootstrap.sh | 4 ++++ installers/install_agy.sh | 18 ++++-------------- installers/install_bat.sh | 18 ++++-------------- installers/install_node.sh | 18 ++++-------------- installers/install_nvim.sh | 18 ++++-------------- installers/install_pnpm.sh | 18 ++++-------------- installers/install_rust.sh | 18 ++++-------------- installers/install_starship.sh | 18 ++++-------------- installers/install_uv.sh | 18 ++++-------------- installers/install_yay.sh | 18 ++++-------------- installers/install_yazi.sh | 18 ++++-------------- installers/install_zoxide.sh | 18 ++++-------------- lib/common.sh | 6 ++++++ lib/platform.sh | 5 +++++ lib/routes.sh | 8 ++++++++ lib/shell_config.sh | 5 +++++ 17 files changed, 78 insertions(+), 170 deletions(-) diff --git a/.agents/skills/add_installer/SKILL.md b/.agents/skills/add_installer/SKILL.md index 03714a9..c942dcd 100644 --- a/.agents/skills/add_installer/SKILL.md +++ b/.agents/skills/add_installer/SKILL.md @@ -77,20 +77,10 @@ Every installer follows this exact boilerplate structure. Copy this and fill in # Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail @@ -259,7 +249,7 @@ inject_block "$config_file" " init" 'eval "$(tool init bash)"' 3. **Confirmation prompts**: Always ask before installing. Check if already installed first. 4. **Idempotent**: Installers must be safe to re-run. Use `inject_block` (not append) for shell configs. 5. **No hardcoded paths**: Use `$HOME`, library functions, and `detect_*` helpers. -6. **Error handling**: Use `set -euo pipefail` after sourcing `bootstrap.sh`. -7. **Metascript boilerplate**: The first 22 lines of every installer are identical — always copy them verbatim. +6. **Error handling**: Use `set -euo pipefail` after the guard block. +7. **CLI Enforcement Guard**: Always copy the standalone execution guard block verbatim to the top of your installer script to prevent direct execution. 8. **`main "$@"`**: Always end with this pattern to pass through CLI arguments. 9. **Clean Official Scripts**: When implementing official curl/install scripts provided in the prompt, strip them of bloat, macOS/Windows support, and redundant shell setups before writing the script. diff --git a/bootstrap.sh b/bootstrap.sh index 78b4254..1ce7dd4 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -18,6 +18,10 @@ is_sourced=false if [ -n "${BASH_SOURCE[0]:-}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then is_sourced=true fi +# Detect eval from installers based on presence of specific variables +if [ -n "${METASCRIPT_URL:-}" ]; then + is_sourced=true +fi # Locate or download libraries so that sourced installers can use them BOOTSTRAP_DIR="${BOOTSTRAP_DIR:-$HOME/.config/bootstrap}" diff --git a/installers/install_agy.sh b/installers/install_agy.sh index a27da9e..b505b3d 100644 --- a/installers/install_agy.sh +++ b/installers/install_agy.sh @@ -6,20 +6,10 @@ # Antigravity CLI Installer Script (Linux Only) # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_bat.sh b/installers/install_bat.sh index d4262fa..51458f3 100644 --- a/installers/install_bat.sh +++ b/installers/install_bat.sh @@ -6,20 +6,10 @@ # Bat Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_node.sh b/installers/install_node.sh index 9df1414..7c029c5 100644 --- a/installers/install_node.sh +++ b/installers/install_node.sh @@ -6,20 +6,10 @@ # Node.js and NVM Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_nvim.sh b/installers/install_nvim.sh index 7031e0d..00f9f34 100644 --- a/installers/install_nvim.sh +++ b/installers/install_nvim.sh @@ -6,20 +6,10 @@ # Neovim Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_pnpm.sh b/installers/install_pnpm.sh index dc63bdc..e7ec949 100644 --- a/installers/install_pnpm.sh +++ b/installers/install_pnpm.sh @@ -17,20 +17,10 @@ # curl -fsSL https://get.pnpm.io/install.sh | ENV="$HOME/.bashrc" SHELL="$(which bash)" bash - # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_rust.sh b/installers/install_rust.sh index 8fc9e34..9cb7611 100644 --- a/installers/install_rust.sh +++ b/installers/install_rust.sh @@ -6,20 +6,10 @@ # Rust Installer Script (Simplified Local Rustup Init) # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_starship.sh b/installers/install_starship.sh index 822a2ac..fecefea 100644 --- a/installers/install_starship.sh +++ b/installers/install_starship.sh @@ -6,20 +6,10 @@ # Starship Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_uv.sh b/installers/install_uv.sh index 9cb614c..2f9f063 100644 --- a/installers/install_uv.sh +++ b/installers/install_uv.sh @@ -6,20 +6,10 @@ # uv Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_yay.sh b/installers/install_yay.sh index 81ef9db..603cb34 100755 --- a/installers/install_yay.sh +++ b/installers/install_yay.sh @@ -6,20 +6,10 @@ # Yay Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_yazi.sh b/installers/install_yazi.sh index 2d1d068..62e75c8 100755 --- a/installers/install_yazi.sh +++ b/installers/install_yazi.sh @@ -6,20 +6,10 @@ # Yazi Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/installers/install_zoxide.sh b/installers/install_zoxide.sh index 6f0ddbc..d9efac0 100755 --- a/installers/install_zoxide.sh +++ b/installers/install_zoxide.sh @@ -6,20 +6,10 @@ # Zoxide Installer Script # -# Run metascript to check if the shell is bash and load libraries -PARENT_DIR="$(dirname "$0")/.." -METASCRIPT_LOCAL="$PARENT_DIR/bootstrap.sh" -METASCRIPT_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/bootstrap.sh" - -if [ -f "$METASCRIPT_LOCAL" ]; then - . "$METASCRIPT_LOCAL" -else - if command -v curl >/dev/null 2>&1; then - eval "$(curl -fsSL "$METASCRIPT_URL")" - else - echo "Error: curl is not installed to fetch bootstrap.sh." >&2 - exit 1 - fi +# Prevent standalone execution +if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then + echo "Error: This script must be run through the 'b' CLI." >&2 + exit 1 fi set -euo pipefail diff --git a/lib/common.sh b/lib/common.sh index bedd540..4668ba1 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -84,3 +84,9 @@ version_lt() { done return 1 } + +# Export functions and variables for subshells +export _LIB_COMMON_SOURCED=1 +export RED GREEN YELLOW BLUE NC +export -f require_bash log_info log_success log_warn log_error confirm has_command make_temp_dir version_lt + diff --git a/lib/platform.sh b/lib/platform.sh index 549b411..3313e08 100644 --- a/lib/platform.sh +++ b/lib/platform.sh @@ -88,3 +88,8 @@ pkg_install() { ;; esac } + +# Export functions and variables for subshells +export _LIB_PLATFORM_SOURCED=1 +export -f detect_distro detect_arch pkg_install + diff --git a/lib/routes.sh b/lib/routes.sh index b987991..8588f20 100755 --- a/lib/routes.sh +++ b/lib/routes.sh @@ -9,6 +9,7 @@ BOOTSTRAP_DIR="${BOOTSTRAP_DIR:-$(dirname "$_LIB_DIR")}" if [ ! -d "$BOOTSTRAP_DIR/lib" ]; then BOOTSTRAP_DIR="$HOME/.config/bootstrap" fi +export BOOTSTRAP_DIR # Source common library if [ -f "$BOOTSTRAP_DIR/lib/common.sh" ]; then @@ -54,6 +55,13 @@ run_ware() { # Check for local installer first local local_installer="$BOOTSTRAP_DIR/installers/install_${tool}.sh" + + if [ "$bypass_edit" = "true" ] && [ -f "$local_installer" ]; then + log_info "Running ${display_name} installer..." + bash "$local_installer" "${cmd_args[@]}" + return $? + fi + local temp_script temp_script=$(mktemp --suffix=".sh" 2>/dev/null || mktemp) diff --git a/lib/shell_config.sh b/lib/shell_config.sh index 715bdfc..50005f3 100644 --- a/lib/shell_config.sh +++ b/lib/shell_config.sh @@ -109,3 +109,8 @@ create_fd_symlink() { sudo ln -sf "$(command -v fdfind)" /usr/local/bin/fd fi } + +# Export functions and variables for subshells +export _LIB_SHELL_CONFIG_SOURCED=1 +export -f get_shell_configs remove_block inject_block add_alias_if_missing add_env_if_missing create_fd_symlink +