decoupled bootstrapper added uninstaller
This commit is contained in:
123
bootstrap.sh
123
bootstrap.sh
@@ -8,64 +8,99 @@ if [ -z "${BASH_VERSION:-}" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Add a shortcut function 'b' to user's shell configurations if not already present
|
||||
add_b_alias() {
|
||||
# Detect if the script is sourced
|
||||
is_sourced=false
|
||||
if [ -n "${ZSH_VERSION:-}" ]; then
|
||||
case $ZSH_EVAL_CONTEXT in
|
||||
*file*) is_sourced=true ;;
|
||||
esac
|
||||
elif [ -n "${BASH_VERSION:-}" ]; then
|
||||
if [ "${BASH_SOURCE[0]}" != "$0" ]; then
|
||||
is_sourced=true
|
||||
fi
|
||||
fi
|
||||
|
||||
# Install/update the bootstrap loader and download b.sh & routes.sh
|
||||
install_bootstrap() {
|
||||
local target_files=()
|
||||
[ -f "$HOME/.bashrc" ] && target_files+=("$HOME/.bashrc")
|
||||
[ -f "$HOME/.zshrc" ] && target_files+=("$HOME/.zshrc")
|
||||
|
||||
for config_file in "${target_files[@]}"; do
|
||||
# 1. Clean up old unmarkered function if it exists
|
||||
if grep -q "Shortcut for downloading bootstrap/install scripts" "$config_file" 2>/dev/null; then
|
||||
sed -i '/# Shortcut for downloading bootstrap\/install scripts/,/^}/d' "$config_file"
|
||||
fi
|
||||
local routes_dir="$HOME/.config/bootstrap"
|
||||
mkdir -p "$routes_dir"
|
||||
|
||||
# 2. Clean up old markered function if it exists
|
||||
# Download b.sh and routes.sh from the repository, with fallback to local files if running inside the repo
|
||||
echo "Downloading bootstrap scripts..."
|
||||
local b_url="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/b.sh"
|
||||
local routes_url="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/routes.sh"
|
||||
|
||||
local script_dir
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
if [ -f "$script_dir/b.sh" ] && [ -f "$script_dir/routes.sh" ]; then
|
||||
echo "Using local files from repository..."
|
||||
cp "$script_dir/b.sh" "$routes_dir/b.sh"
|
||||
cp "$script_dir/routes.sh" "$routes_dir/routes.sh"
|
||||
else
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
curl -fsSL "$b_url" -o "$routes_dir/b.sh"
|
||||
curl -fsSL "$routes_url" -o "$routes_dir/routes.sh"
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
wget -qO "$routes_dir/b.sh" "$b_url"
|
||||
wget -qO "$routes_dir/routes.sh" "$routes_url"
|
||||
else
|
||||
echo "Error: Neither curl nor wget is installed." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Set up shell configuration files
|
||||
for config_file in "${target_files[@]}"; do
|
||||
# 1. Clean up old embedded function block if it exists (from previous setup)
|
||||
if grep -q "# >>> bootstrap-cli b function >>>" "$config_file" 2>/dev/null; then
|
||||
sed -i '/# >>> bootstrap-cli b function >>>/,/# <<< bootstrap-cli b function <<</d' "$config_file"
|
||||
fi
|
||||
|
||||
# 3. Append the new markered function block
|
||||
echo "Adding/updating helper function 'b' to $config_file..."
|
||||
# 2. Clean up old loader block if it exists
|
||||
if grep -q "# >>> bootstrap-cli setup >>>" "$config_file" 2>/dev/null; then
|
||||
sed -i '/# >>> bootstrap-cli setup >>>/,/# <<< bootstrap-cli setup <<</d' "$config_file"
|
||||
fi
|
||||
|
||||
# 3. Append the new lightweight loader block
|
||||
echo "Adding bootstrap loader to $config_file..."
|
||||
cat << 'EOF' >> "$config_file"
|
||||
|
||||
# >>> bootstrap-cli b function >>>
|
||||
# Shortcut for downloading and running bootstrap/install scripts
|
||||
b() {
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: b <script_name> [args...]" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local routes_dir="$HOME/.config/bootstrap"
|
||||
local routes_file="$routes_dir/routes.sh"
|
||||
local routes_url="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master/routes.sh"
|
||||
|
||||
mkdir -p "$routes_dir"
|
||||
|
||||
# Attempt to update the routes.sh file
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
curl -fsSL "$routes_url" -o "$routes_file" 2>/dev/null
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
wget -qO "$routes_file" "$routes_url" 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ ! -f "$routes_file" ]; then
|
||||
echo "Error: Routes file not found at $routes_file and could not be downloaded." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
bash "$routes_file" "$@"
|
||||
}
|
||||
# <<< bootstrap-cli b function <<<
|
||||
# >>> bootstrap-cli setup >>>
|
||||
export BOOTSTRAP_DIR="$HOME/.config/bootstrap"
|
||||
[ -f "$BOOTSTRAP_DIR/b.sh" ] && . "$BOOTSTRAP_DIR/b.sh"
|
||||
# <<< bootstrap-cli setup <<<
|
||||
EOF
|
||||
done
|
||||
}
|
||||
|
||||
add_b_alias
|
||||
install_bootstrap
|
||||
|
||||
# Source ~/.bashrc to make changes immediately available
|
||||
if [ -f "$HOME/.bashrc" ]; then
|
||||
echo "Sourcing ~/.bashrc..."
|
||||
. "$HOME/.bashrc"
|
||||
# Load the b function immediately in the current subshell
|
||||
if [ -f "$HOME/.config/bootstrap/b.sh" ]; then
|
||||
. "$HOME/.config/bootstrap/b.sh"
|
||||
fi
|
||||
|
||||
# Handle sourcing the shell configuration file
|
||||
if [ "$is_sourced" = true ]; then
|
||||
if [ -n "${ZSH_VERSION:-}" ] && [ -f "$HOME/.zshrc" ]; then
|
||||
echo "Sourcing ~/.zshrc..."
|
||||
. "$HOME/.zshrc"
|
||||
elif [ -n "${BASH_VERSION:-}" ] && [ -f "$HOME/.bashrc" ]; then
|
||||
echo "Sourcing ~/.bashrc..."
|
||||
. "$HOME/.bashrc"
|
||||
fi
|
||||
else
|
||||
echo
|
||||
echo "Bootstrap CLI installed successfully!"
|
||||
echo "To start using the 'b' command in this terminal session, run:"
|
||||
if [ -n "${ZSH_VERSION:-}" ] || [ -f "$HOME/.zshrc" ]; then
|
||||
echo " source ~/.zshrc"
|
||||
else
|
||||
echo " source ~/.bashrc"
|
||||
fi
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user