refactor: shift aliases to separate file

This commit is contained in:
2026-06-20 19:28:32 +05:30
parent e658d6d375
commit 4318cd8536
5 changed files with 83 additions and 7 deletions

View File

@@ -1 +1 @@
1.1.2
1.1.3

View File

@@ -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)

View File

@@ -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}"

View File

@@ -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

View File

@@ -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