From 7ec8741ba800974418e4bbc808f95b9f55049d50 Mon Sep 17 00:00:00 2001 From: Saravana Achu Mac Date: Tue, 5 May 2026 10:48:53 -0700 Subject: [PATCH] Add alias installer --- aliases/README.md | 16 +++- aliases/install.sh | 192 +++++++++++++++++++++++++++++++++++++++++++++ docs/repo-map.md | 1 + 3 files changed, 208 insertions(+), 1 deletion(-) create mode 100755 aliases/install.sh diff --git a/aliases/README.md b/aliases/README.md index b395264..edeeda7 100644 --- a/aliases/README.md +++ b/aliases/README.md @@ -4,7 +4,21 @@ Reusable Bash/Zsh aliases for common shell, git, tmux, directory, and listing sh ## Install -Source the aggregate loader from your shell startup file. +Run the installer: + +```bash +/Users/saravana/BytelystAI/bytelyst-devops-tools/aliases/install.sh +``` + +The installer detects macOS/Linux and Bash/Zsh, then adds a managed source block to the right startup file. It is safe to run more than once and creates a timestamped backup before changing the startup file. + +Preview the change without writing files: + +```bash +/Users/saravana/BytelystAI/bytelyst-devops-tools/aliases/install.sh --dry-run +``` + +You can also source the aggregate loader manually. For Zsh, add this to `~/.zshrc`: diff --git a/aliases/install.sh b/aliases/install.sh new file mode 100755 index 0000000..83733a0 --- /dev/null +++ b/aliases/install.sh @@ -0,0 +1,192 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'USAGE' +Install ByteLyst shell aliases into your shell startup file. + +Usage: + ./install.sh [--dry-run] [--shell bash|zsh] [--rc-file PATH] + +Options: + --dry-run Show what would change without writing files. + --shell SHELL Override detected shell. Supported: bash, zsh. + --rc-file PATH Override the target startup file. + --help Show this help. +USAGE +} + +die() { + echo "Error: $*" >&2 + exit 1 +} + +detect_os() { + case "$(uname -s)" in + Darwin) echo "macos" ;; + Linux) echo "linux" ;; + *) echo "unknown" ;; + esac +} + +detect_shell() { + local shell_name + shell_name="$(basename "${SHELL:-}")" + + case "$shell_name" in + bash | zsh) echo "$shell_name" ;; + *) + if command -v zsh >/dev/null 2>&1; then + echo "zsh" + elif command -v bash >/dev/null 2>&1; then + echo "bash" + else + die "could not detect bash or zsh; pass --shell bash or --shell zsh" + fi + ;; + esac +} + +default_rc_file() { + local shell_name="$1" + local os_name="$2" + + case "$shell_name" in + zsh) + echo "$HOME/.zshrc" + ;; + bash) + if [ "$os_name" = "macos" ]; then + echo "$HOME/.bash_profile" + else + echo "$HOME/.bashrc" + fi + ;; + *) + die "unsupported shell: $shell_name" + ;; + esac +} + +resolve_alias_dir() { + local source_path="${BASH_SOURCE[0]}" + cd -- "$(dirname -- "$source_path")" >/dev/null 2>&1 + pwd -P +} + +remove_existing_block() { + local input_file="$1" + local output_file="$2" + + awk ' + /^# >>> bytelyst aliases >>>$/ { skip = 1; next } + /^# <<< bytelyst aliases <<<$/{ skip = 0; next } + !skip { lines[++count] = $0 } + END { + while (count > 0 && lines[count] == "") { + count-- + } + for (i = 1; i <= count; i++) { + print lines[i] + } + } + ' "$input_file" > "$output_file" +} + +write_block() { + local alias_loader="$1" + + cat <>> bytelyst aliases >>> +source "$alias_loader" +# <<< bytelyst aliases <<< +BLOCK +} + +dry_run=false +shell_override="" +rc_file="" + +while [ "$#" -gt 0 ]; do + case "$1" in + --dry-run) + dry_run=true + shift + ;; + --shell) + [ "$#" -ge 2 ] || die "--shell requires bash or zsh" + shell_override="$2" + shift 2 + ;; + --rc-file) + [ "$#" -ge 2 ] || die "--rc-file requires a path" + rc_file="$2" + shift 2 + ;; + --help | -h) + usage + exit 0 + ;; + *) + die "unknown option: $1" + ;; + esac +done + +os_name="$(detect_os)" +shell_name="${shell_override:-$(detect_shell)}" + +case "$shell_name" in + bash | zsh) ;; + *) die "unsupported shell: $shell_name" ;; +esac + +alias_dir="$(resolve_alias_dir)" +alias_loader="$alias_dir/_source_all.alias" +[ -f "$alias_loader" ] || die "alias loader not found: $alias_loader" + +target_rc="${rc_file:-$(default_rc_file "$shell_name" "$os_name")}" + +echo "Detected OS: $os_name" +echo "Target shell: $shell_name" +echo "Target startup file: $target_rc" +echo "Alias loader: $alias_loader" + +if [ "$dry_run" = true ]; then + echo + echo "Would add or replace this managed block:" + write_block "$alias_loader" + exit 0 +fi + +mkdir -p "$(dirname -- "$target_rc")" +touch "$target_rc" + +tmp_file="$(mktemp)" +new_file="$(mktemp)" +trap 'rm -f "$tmp_file" "$new_file"' EXIT + +remove_existing_block "$target_rc" "$tmp_file" +{ + cat "$tmp_file" + if [ -s "$tmp_file" ]; then + echo + fi + write_block "$alias_loader" +} > "$new_file" + +if cmp -s "$target_rc" "$new_file"; then + echo + echo "ByteLyst aliases are already installed." + exit 0 +fi + +backup_file="$target_rc.bytelyst-aliases.bak.$(date +%Y%m%d%H%M%S).$$" +cp "$target_rc" "$backup_file" +cat "$new_file" > "$target_rc" + +echo +echo "Installed ByteLyst aliases." +echo "Backup created: $backup_file" +echo "Open a new shell or run:" +echo " source \"$target_rc\"" diff --git a/docs/repo-map.md b/docs/repo-map.md index cdd4315..cfac83f 100644 --- a/docs/repo-map.md +++ b/docs/repo-map.md @@ -87,6 +87,7 @@ Reusable Bash/Zsh alias bundle for common git, tmux, shell, directory, and listi Key files: - `_source_all.alias` +- `install.sh` - `README.md` ### `github_access_scripts/`