Created
September 5, 2025 11:13
-
-
Save pboling/81df68ef66bf969f97935bf82c7462a6 to your computer and use it in GitHub Desktop.
bundler shim for helping with upgrade of very old Ruby code into modern RubyGems / Bundler environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Safe shim for Bundler that delegates to the system-installed `bundle`. | |
# This avoids relying on a project binstub and prevents recursion if bin/ is ahead of the real Bundler in PATH. | |
# | |
# Rationale: | |
# - Historically, some setups added ./bin to PATH (see .envrc). When a binstub existed, `bundle` could resolve here. | |
# - Modern Bundler does not require a binstub for itself. If this file is invoked, we forward to the real Bundler. | |
# - We temporarily remove this script's directory from PATH to avoid calling ourselves again. | |
set -euo pipefail | |
# Compute this script's directory | |
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
# Build a PATH without SCRIPT_DIR to avoid recursion | |
PRUNED_PATH="" | |
OLD_IFS="$IFS" | |
IFS=":" | |
for dir in $PATH; do | |
if [[ "$dir" != "$SCRIPT_DIR" && -n "$dir" ]]; then | |
if [[ -z "$PRUNED_PATH" ]]; then | |
PRUNED_PATH="$dir" | |
else | |
PRUNED_PATH="$PRUNED_PATH:$dir" | |
fi | |
fi | |
done | |
IFS="$OLD_IFS" | |
export PATH="$PRUNED_PATH" | |
# Exec the real Bundler from the pruned PATH | |
if command -v bundle >/dev/null 2>&1; then | |
exec bundle "$@" | |
else | |
echo "Error: Could not find Bundler ('bundle') in PATH after removing $SCRIPT_DIR." >&2 | |
echo "Please ensure Bundler is installed (e.g.,: gem install bundler) and available on your PATH." >&2 | |
exit 127 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment