diff --git a/base/bashrc b/base/bashrc index 3f1702b..38a31b7 100644 --- a/base/bashrc +++ b/base/bashrc @@ -27,6 +27,10 @@ x-load-configs # Import ssh keys in keychain ssh-add -A 2>/dev/null +x-have rbenv && { + eval "$(rbenv init - bash)" +} + # Enable the subsequent settings only in interactive sessions case $- in *i*) ;; diff --git a/base/zshrc b/base/zshrc index 13ba3a5..0d9b834 100755 --- a/base/zshrc +++ b/base/zshrc @@ -61,9 +61,9 @@ antigen bundle jreese/zsh-titles antigen bundle zsh-users/zsh-completions # these should be available if there's need -have brew && antigen bundle brew -have php && antigen bundle php -have python && antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv +x-have brew && antigen bundle brew +x-have php && antigen bundle php +x-have python && antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv # nvm is a strange beast zstyle ':omz:plugins:nvm' autoload yes @@ -81,14 +81,26 @@ antigen apply [ -f "${DOTFILES}/config/fzf/fzf.zsh" ] \ && source "${DOTFILES}/config/fzf/fzf.zsh" +x-have op && { + eval "$(op completion zsh)" + compdef _op op +} + +x-have rbenv && { + eval "$(rbenv init - zsh)" +} + # Autoupdate tmux window name TMUX_WINDOW_NAME_PLUGIN="$DOTFILES/config/tmux/plugins/tmux-window-name/scripts/rename_session_windows.py" [ -f "$TMUX_WINDOW_NAME_PLUGIN" ] && { - tmux-window-name() { - ($TMUX_WINDOW_NAME_PLUGIN &) + tmux-window-name() + { + ($TMUX_WINDOW_NAME_PLUGIN &) } add-zsh-hook chpwd tmux-window-name } # Start starship eval "$(starship init zsh)" + +source $DOTFILES/config/alias diff --git a/config/alias b/config/alias index 066ae6d..7c89ba1 100755 --- a/config/alias +++ b/config/alias @@ -1,68 +1,15 @@ #!/usr/bin/env bash # shellcheck source="../scripts/shared.sh" # shellcheck disable=1091,2139 -DOTFILES="$HOME/.dotfiles" - -# Get installed php versions from brew and setup aliases -function x-set-php-aliases -{ - x-have brew && { - local php_versions=() - while IFS="" read -r line; do php_versions+=("$line"); done < <(bkt -- brew list | grep '^php') - - php_error_reporting='-d error_reporting=22527' - - for version in "${php_versions[@]}"; do - # drop the dot from version (8.0 -> 80) - local php_abbr="${version//\./}" - # replace "php@" with "p" so "php@80" becomes "p80" - local php_alias="${php_abbr//php@/p}" - - # Fetch the exec path once - php_exec="$(brew --prefix "$version")/bin/php" - - # Raw PHP without error_reporting flag. - # shellcheck disable=SC2139 - alias "${php_alias}"r="$php_exec" - # PHP with error_reporting flag. - # shellcheck disable=SC2139,SC2140 - alias "$php_alias"="$php_exec $php_error_reporting" - # Local PHP Server. - # shellcheck disable=SC2139,SC2140 - alias "${php_alias}s"="$php_exec -S localhost:9000" - # Use composer with specific PHP and error_reporting flag on. - # shellcheck disable=SC2139,SC2140 - alias "${php_alias}c"="$php_exec $php_error_reporting $(which composer)" - done - } -} - -if [[ $(uname) == 'Darwin' ]]; then - - x-set-php-aliases - - # Laravel Sail shortcut - alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail' - - # Flush Directory Service cache - alias flushdns="sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder" - - # Lock the screen - alias afk="osascript -e 'tell application \"System Events\" to keystroke \"q\" using {command down,control down}'" - - # Empty the Trash on all mounted volumes and the main HDD - # Also, clear Apple’s System Logs to improve shell startup speed - alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl" -fi # Be nice alias please="sudo " # Color the grep output -alias grep='grep --color' +alias grep="grep --color" x-have eza && { - alias ls='eza -h -s=type --git --icons --group-directories-first' + alias ls="eza -h -s=type --git --icons --group-directories-first" } # Easier navigation: .., ..., .... @@ -138,22 +85,35 @@ alias zedit='$EDITOR ~/.dotfiles' alias irssi="irssi --config=$XDG_CONFIG_HOME/irssi/config --home=$XDG_CONFIG_HOME/irssi" -if [[ -f "$HOME/.aliases.local" ]]; then - # shellcheck disable=SC1091 - source "$HOME/.aliases.local" -fi - alias wget="wget --hsts-file=$XDG_DATA_HOME/wget-hsts" alias svn="svn --config-dir $XDG_CONFIG_HOME/subversion" +if [[ $(uname) == 'Darwin' ]]; then + + # Laravel Sail shortcut + alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail' + + # Flush Directory Service cache + alias flushdns="sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder" + + # Lock the screen + alias afk="osascript -e 'tell application \"System Events\" to keystroke \"q\" using {command down,control down}'" + + # Empty the Trash on all mounted volumes and the main HDD + # Also, clear Apple's System Logs to improve shell startup speed + alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl" + + x-set-php-aliases +fi + # Alacritty preexec hook to update dynamic title -function preexec() +preexec() { print -Pn "\e]0;$1%~\a" } # Update dotfiles -function dfu() +dfu() { ( cd "$DOTFILES" && git pull --ff-only && ./install -q @@ -161,7 +121,7 @@ function dfu() } # Weather in Tampere, or other city -function weather() +weather() { # https://github.com/chubin/wttr.in#usage local city="${1:-Tampere}" @@ -169,19 +129,19 @@ function weather() } # Docker -function ssh-docker() +ssh-docker() { docker exec -it "$@" bash } # All the dig info -function digga() +digga() { dig +nocmd "$1" any +multiline +noall +answer } # Rector project to php version 8.2 by default. -function rector() +rector() { local php="${1:-82}" docker run -v "$(pwd)":/project rector/rector:latest process \ @@ -191,7 +151,7 @@ function rector() } # Commit everything -function commit() +commit() { commitMessage="$*" @@ -203,7 +163,7 @@ function commit() eval "git commit -a -m '${commitMessage}'" } -function scheduler() +scheduler() { while :; do php artisan schedule:run diff --git a/config/exports b/config/exports index a69e4a6..b13cc9c 100755 --- a/config/exports +++ b/config/exports @@ -2,11 +2,6 @@ # shellcheck shell=bash # vim: filetype=zsh -# if cargo directory exists, add it to path -if [[ -d "$HOME/.local/share/cargo/bin" ]]; then - export PATH="$HOME/.local/share/cargo/bin:$PATH" -fi - # Cache commands using bkt if installed if command -v bkt >&/dev/null; then bkt() @@ -26,7 +21,7 @@ fi # shorthand for checking if the system has the bin in path, # this version does not use caching # usage: have_command php && php -v -function have_command +have_command() { command -v "$1" >&/dev/null } @@ -34,21 +29,21 @@ function have_command # shorthand for checking if the system has the bin in path, # this version uses caching # usage: have php && php -v -function have +have() { bkt -- which "$1" >&/dev/null } -function brew_installed +brew_installed() { bkt -- brew list } # shorthand for checking if brew package is installed # usage: have_brew php && php -v -function have_brew +have_brew() { - ! have brew && return 125 + ! x-have brew && return 125 if bkt -- brew list "$1" &> /dev/null; then return 0 diff --git a/config/exports-apps b/config/exports-apps index 8cb11cb..1352b0e 100755 --- a/config/exports-apps +++ b/config/exports-apps @@ -59,12 +59,6 @@ x-have node && { # op (1Password cli) is present export OP_CACHE="$XDG_STATE_HOME/1password" -x-have op && { - [ "$DOTFILES_CURRENT_SHELL" = "zsh" ] && { - eval "$(op completion zsh)" - compdef _op op - } -} # Python # @@ -87,10 +81,6 @@ export BUNDLE_USER_PLUGIN="$XDG_DATA_HOME"/bundle export RBENV_ROOT="$XDG_STATE_HOME/rbenv" x-dc "$RBENV_ROOT" x-have gem && export PATH="${GEM_HOME}/bin:$PATH" -x-have rbenv && { - [ "$DOTFILES_CURRENT_SHELL" = "zsh" ] && eval "$(rbenv init - zsh)" - [ "$DOTFILES_CURRENT_SHELL" = "bash" ] && eval "$(rbenv init - bash)" -} # Rust / cargo export RUSTUP_HOME="$XDG_DATA_HOME/rustup" diff --git a/config/exports-lakka b/config/exports-lakka index c75cbab..f4e473d 100644 --- a/config/exports-lakka +++ b/config/exports-lakka @@ -1,7 +1,7 @@ # lakka exports ## LUA -have luarocks && $(luarocks path --bin) +x-have luarocks && $(luarocks path --bin) export PATH="$HOME/.local/go/bin:$PATH" @@ -17,4 +17,3 @@ export RVM_PATH="$HOME/.rvm" export PATH="$RVM_PATH/bin:$PATH" # shellcheck source=$HOME/.rvm/scripts/rvm [[ -s "$RVM_PATH/scripts/rvm" ]] && source "$RVM_PATH/scripts/rvm" # Load RVM into a shell session *as a function* - diff --git a/config/exports-shell b/config/exports-shell index f6d7475..d32fb7f 100755 --- a/config/exports-shell +++ b/config/exports-shell @@ -3,21 +3,6 @@ # shellcheck disable=1091,2046 # vim: filetype=bash -export DOTFILES="$HOME/.dotfiles" - -# Explicitly set XDG folders -# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html -export XDG_CONFIG_HOME="$HOME/.config" -export XDG_DATA_HOME="$HOME/.local/share" -export XDG_STATE_HOME="$HOME/.local/state" - -# custom variables -export XDG_BIN_HOME="$HOME/.local/bin" -export XDG_CACHE_HOME="$HOME/.cache" -export XDG_RUNTIME_DIR="$HOME/.local/run" - -export LC_ALL=fi_FI.UTF-8 - # Bash completion file location export BASH_COMPLETION_USER_FILE="${XDG_CONFIG_HOME}/bash-completion/bash_completion" @@ -38,13 +23,13 @@ export HISTORY_IGNORE="(ls|cd|cd -|pwd|exit|date|* --help)" export LESSHISTFILE="$XDG_STATE_HOME"/less/history # Highlight section titles in manual pages -export LESS_TERMCAP_md="$ORANGE" +# export LESS_TERMCAP_md="$ORANGE" # zsh autoloaded terminfo -export TERMINFO="$XDG_DATA_HOME"/terminfo -export TERMINFO_DIRS="$XDG_DATA_HOME"/terminfo:/usr/share/terminfo +export TERMINFO="${XDG_DATA_HOME}/terminfo" +export TERMINFO_DIRS="${XDG_DATA_HOME}/terminfo":/usr/share/terminfo -# Don’t clear the screen after quitting a manual page +# Don't clear the screen after quitting a manual page export MANPAGER="less -X" # Always enable colored `grep` output diff --git a/config/functions b/config/functions index a35326f..794dc2c 100755 --- a/config/functions +++ b/config/functions @@ -1,3 +1,5 @@ #!/usr/bin/env bash # # shell functions + +set -euo pipefail diff --git a/local/bin/x-load-configs b/local/bin/x-load-configs index dbd6aed..247a38f 100755 --- a/local/bin/x-load-configs +++ b/local/bin/x-load-configs @@ -2,35 +2,76 @@ # Load our configuration files # Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved. -# Set verbosity with VERBOSE=1 x-load-configs -: "${VERBOSE:0}" +set -euo pipefail -DOTFILES="$HOME/.dotfiles" +# Set verbosity with VERBOSE=1 x-load-configs +VERBOSE="${VERBOSE:=0}" + +[ "$VERBOSE" = "1" ] && { + set -x +} CONFIG_PATH="$DOTFILES/config" # Load the shell dotfiles, and then some: HOST="$(hostname -s)" [ "$VERBOSE" = "1" ] && { - echo "x-load-config-fn: VERBOSE=1" - echo "x-load-config-fn: HOST: $HOST" + echo "x-load-configs: VERBOSE=1" + echo "x-load-configs: HOST: $HOST" } -for FILE in $CONFIG_PATH/{exports,alias,functions}; do - FILENAME="$FILE" + +configFile() +{ + echo "$CONFIG_PATH/$1" +} + +configMsg() +{ + printf 'x-load-configs: %s %s\n' "$1" "$2" +} + +loadConfigFiles() +{ + CONFIG_FILE=$1 + SECRET_FILE=$CONFIG_FILE-secret + HOST_FILE=$CONFIG_FILE-$HOST + SECRET_HOST=$HOST_FILE-secret + + [ "$VERBOSE" = "1" ] && configMsg "?" "$CONFIG_FILE" + # global (exports|alias|functions) FILENAME for all hosts # shellcheck source=../config/exports - [ -r "$FILENAME" ] && source "$FILENAME" \ - && [ "$VERBOSE" = "1" ] && echo "x-load-config-fn: $FILENAME" + [ -r "$CONFIG_FILE" ] && { + source "$CONFIG_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$CONFIG_FILE" + } + # global secret FILENAME, git ignored # shellcheck source=../config/exports-secret - [ -r "$FILENAME-secret" ] && source "$FILENAME-secret" \ - && [ "$VERBOSE" = "1" ] && echo "x-load-config-fn: $FILENAME-secret" + [ "$VERBOSE" = "1" ] && configMsg "?" "$SECRET_FILE" + [ -r "$SECRET_FILE" ] && { + source "$SECRET_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$SECRET_FILE" + } # host specific (exports|alias|functions) FILENAME # shellcheck source=../config/exports - [ -r "$FILENAME-$HOST" ] && source "$FILENAME-$HOST" \ - && [ "$VERBOSE" = "1" ] && echo "x-load-config-fn: $FILENAME-$HOST" + [ "$VERBOSE" = "1" ] && configMsg "?" "$HOST_FILE" + [ -r "$HOST_FILE" ] && { + source "$HOST_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$HOST_FILE" + } # host specific (exports|alias|functions) FILENAME, git ignored # shellcheck source=../config/exports - [ -r "$FILENAME-$HOST-secret" ] && source "$FILENAME-$HOST-secret" \ - && [ "$VERBOSE" = "1" ] && echo "x-load-config-fn: $FILENAME-$HOST-secret" -done + [ "$VERBOSE" = "1" ] && configMsg "?" "$SECRET_HOST" + [ -r "$SECRET_HOST" ] && { + source "$SECRET_HOST" \ + && [ "$VERBOSE" = "1" ] && configMsg "Found" "$SECRET_HOST" + } +} + +FILE_EXPORTS=$(configFile "exports") +FILE_FUNCTIONS=$(configFile "functions") +FILE_ALIAS=$(configFile "alias") + +loadConfigFiles "$FILE_EXPORTS" +loadConfigFiles "$FILE_FUNCTIONS" +loadConfigFiles "$FILE_ALIAS" + +exit 0 diff --git a/local/bin/x-set-php-aliases b/local/bin/x-set-php-aliases new file mode 100755 index 0000000..01a4949 --- /dev/null +++ b/local/bin/x-set-php-aliases @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Check which php versions are installed with brew, and create aliases for each installation. +# Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved. + +set -euo pipefail + +# Set verbosity with VERBOSE=1 x-load-configs +VERBOSE="${VERBOSE:=0}" + +[ "$VERBOSE" = "2" ] && { + set -x +} + +! x-have brew && { + exit 0 +} + +# Get installed php versions from brew and setup aliases +php_versions=() +while IFS="" read -r line; do php_versions+=("$line"); done < <(bkt -- brew list | grep '^php') + +php_error_reporting='-d error_reporting=22527' + +for version in "${php_versions[@]}"; do + [ "$VERBOSE" = "1" ] && echo "Setting aliases for $version" + # drop the dot from version (8.0 -> 80) + php_abbr="${version//\./}" + # replace "php@" with "p" so "php@80" becomes "p80" + php_alias="${php_abbr//php@/p}" + + # Fetch the exec path once + php_exec="$HOMEBREW_PREFIX/opt/$version/bin/php" + + [ -f "$php_exec" ] && { + [ "$VERBOSE" = "1" ] && echo "-> php_exec $php_exec" + + # Raw PHP without error_reporting flag. + # shellcheck disable=SC2139 + alias "${php_alias}"r="$php_exec" + # PHP with error_reporting flag. + # shellcheck disable=SC2139,SC2140 + alias "$php_alias"="$php_exec $php_error_reporting" + # Local PHP Server. + # shellcheck disable=SC2139,SC2140 + alias "${php_alias}s"="$php_exec -S localhost:9000" + # Use composer with specific PHP and error_reporting flag on. + # shellcheck disable=SC2139,SC2140 + alias "${php_alias}c"="$php_exec $php_error_reporting $(which composer)" + } +done