From 4318cd85366ddaeae1b98c268bfabbe1ccf146a8 Mon Sep 17 00:00:00 2001 From: Aditya Gupta Date: Sat, 20 Jun 2026 19:28:32 +0530 Subject: [PATCH] refactor: shift aliases to separate file --- VERSION | 2 +- bootstrap.sh | 21 +++++++++++++++++++++ commands/uninstall.sh | 26 ++++++++++++++++++++++++++ installers/install_bat.sh | 15 +++++++++++++-- lib/shell_config.sh | 26 ++++++++++++++++++++++---- 5 files changed, 83 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index 45a1b3f..781dcb0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.2 +1.1.3 diff --git a/bootstrap.sh b/bootstrap.sh index 16ad83d..3e9c644 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -160,6 +160,12 @@ install_bootstrap() { fi fi + # Create ~/.bash_aliases if it doesn't exist + if [ ! -f "$HOME/.bash_aliases" ]; then + log_info "Creating ~/.bash_aliases..." + touch "$HOME/.bash_aliases" + 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) @@ -177,6 +183,21 @@ export BOOTSTRAP_DIR="$HOME/.config/bootstrap" [ -f "$BOOTSTRAP_DIR/b.sh" ] && . "$BOOTSTRAP_DIR/b.sh" # <<< bootstrap-cli setup <<< EOF + + # 4. Ensure ~/.bash_aliases is sourced in ~/.bashrc if not already + if [ "$config_file" = "$HOME/.bashrc" ]; then + if ! grep -q "bash_aliases" "$config_file" 2>/dev/null; then + local alias_source_content + alias_source_content=$(cat << 'EOF' +# Source aliases file if it exists +if [ -f "$HOME/.bash_aliases" ]; then + . "$HOME/.bash_aliases" +fi +EOF +) + inject_block "$config_file" "bootstrap-cli bash_aliases setup" "$alias_source_content" + fi + fi done # Initialize the last update timestamp to prevent immediate update on first execution (Fix 2) diff --git a/commands/uninstall.sh b/commands/uninstall.sh index 718316b..1ab3077 100644 --- a/commands/uninstall.sh +++ b/commands/uninstall.sh @@ -18,8 +18,34 @@ for config_file in "${target_files[@]}"; do # Remove old embedded b function block remove_block "$config_file" "bootstrap-cli b function" + + # Remove bash_aliases setup block if present + remove_block "$config_file" "bootstrap-cli bash_aliases setup" done +# Clean up bootstrap-specific aliases from ~/.bash_aliases if the file exists +if [ -f "$HOME/.bash_aliases" ]; then + log_info "Cleaning up bootstrap aliases from ~/.bash_aliases..." + + # 1. Remove the 'bat alias' block if it was injected there + remove_block "$HOME/.bash_aliases" "bat alias" + + # 2. Remove specific aliases added by bootstrap (e.g. vim -> nvim) + if grep -q '^alias vim="nvim"$' "$HOME/.bash_aliases" 2>/dev/null; then + local tmp_file + tmp_file=$(mktemp) + sed '/^alias vim="nvim"$/d' "$HOME/.bash_aliases" > "$tmp_file" + cat "$tmp_file" > "$HOME/.bash_aliases" + rm -f "$tmp_file" + fi + + # Remove ~/.bash_aliases entirely if it is empty (size 0) after our cleanup + if [ ! -s "$HOME/.bash_aliases" ]; then + log_info "Removing empty ~/.bash_aliases..." + rm -f "$HOME/.bash_aliases" + fi +fi + # Remove the installation directory rm -rf "${BOOTSTRAP_DIR:-$HOME/.config/bootstrap}" diff --git a/installers/install_bat.sh b/installers/install_bat.sh index e9c209c..2219cbe 100644 --- a/installers/install_bat.sh +++ b/installers/install_bat.sh @@ -100,8 +100,19 @@ configure_shell() { local content="alias cat='bat --paging=never -p'" for config_file in "${target_files[@]}"; do - log_info "Adding bat alias to $config_file..." - inject_block "$config_file" "bat alias" "$content" + local target_file="$config_file" + if [ "$config_file" = "$HOME/.bashrc" ]; then + # Clean up old block from ~/.bashrc if present to avoid duplication + remove_block "$config_file" "bat alias" + target_file="$HOME/.bash_aliases" + # Ensure the file exists + if [ ! -f "$target_file" ]; then + touch "$target_file" + fi + fi + + log_info "Adding bat alias to $target_file..." + inject_block "$target_file" "bat alias" "$content" # Source if modified (only for bashrc) if [ "$config_file" = "$HOME/.bashrc" ]; then diff --git a/lib/shell_config.sh b/lib/shell_config.sh index e3a4876..715bdfc 100644 --- a/lib/shell_config.sh +++ b/lib/shell_config.sh @@ -61,12 +61,30 @@ add_alias_if_missing() { local name="$2" local val="$3" - if [ -f "$config_file" ] && ! grep -q "alias $name=" "$config_file" 2>/dev/null; then - log_info "Adding alias $name to $config_file" - echo "alias ${name}=\"${val}\"" >> "$config_file" + local target_file="$config_file" + if [ "$config_file" = "$HOME/.bashrc" ]; then + # Migrate existing alias from ~/.bashrc if present to avoid duplicates/shadowing + if [ -f "$HOME/.bashrc" ] && grep -q "alias ${name}=" "$HOME/.bashrc" 2>/dev/null; then + log_info "Migrating alias $name from ~/.bashrc to ~/.bash_aliases" + local tmp_file + tmp_file=$(mktemp) + sed "/^alias ${name}=/d" "$HOME/.bashrc" > "$tmp_file" + cat "$tmp_file" > "$HOME/.bashrc" + rm -f "$tmp_file" + fi + target_file="$HOME/.bash_aliases" + fi + + if [ ! -f "$target_file" ]; then + touch "$target_file" + fi + + if ! grep -q "alias $name=" "$target_file" 2>/dev/null; then + log_info "Adding alias $name to $target_file" + echo "alias ${name}=\"${val}\"" >> "$target_file" return 0 # Added fi - return 1 # Not added (already existed or file doesn't exist) + return 1 # Not added (already existed) } # Add environment variable if not present