From 4572c42eeadbb5dd65087b276eedded2169062cc Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 1 Nov 2023 14:45:03 +0200 Subject: [PATCH] feat: x-path-* as scripts, run other changes --- base/bashrc | 8 ++++-- base/zshenv | 1 + base/zshrc | 11 ++++---- config/alias | 8 +++--- config/exports | 52 +++++++++++++++++++++++++++++++++- config/exports-apps | 24 ++++++++-------- config/exports-shell | 29 ------------------- local/bin/x-have | 4 +-- local/bin/x-path-append | 8 ++++++ local/bin/x-path-prepend | 7 +++++ local/bin/x-path-remove | 7 +++++ scripts/shared.sh | 60 ++++------------------------------------ 12 files changed, 109 insertions(+), 110 deletions(-) create mode 100755 local/bin/x-path-append create mode 100755 local/bin/x-path-prepend create mode 100755 local/bin/x-path-remove diff --git a/base/bashrc b/base/bashrc index 55c7be5..aca944c 100644 --- a/base/bashrc +++ b/base/bashrc @@ -18,6 +18,12 @@ export XDG_BIN_HOME="$HOME/.local/bin" export XDG_CACHE_HOME="$HOME/.cache" export XDG_RUNTIME_DIR="$HOME/.local/run" +source "$DOTFILES/config/exports" + +# shellcheck source=../config/fzf/fzf.bash +[ -f "${DOTFILES}/config/fzf/fzf.bash" ] && + source "${DOTFILES}/config/fzf/fzf.bash" + # Import ssh keys in keychain ssh-add -A 2>/dev/null @@ -96,5 +102,3 @@ export OSH="$HOME/.local/share/oh-my-bash" # export EDITOR='mvim' # fi } - - diff --git a/base/zshenv b/base/zshenv index 9968b05..a0eaccd 100644 --- a/base/zshenv +++ b/base/zshenv @@ -16,3 +16,4 @@ export XDG_BIN_HOME="$HOME/.local/bin" export XDG_CACHE_HOME="$HOME/.cache" export XDG_RUNTIME_DIR="$HOME/.local/run" +source "$DOTFILES/config/exports" diff --git a/base/zshrc b/base/zshrc index 0bac8b7..90fe9dd 100755 --- a/base/zshrc +++ b/base/zshrc @@ -1,6 +1,12 @@ # this is my zsh config. there are many like it, but this one is mine. # shellcheck shell=bash +export PATH="$HOME/.local/bin:$HOME/.dotfiles/local/bin:$HOME/.local/go/bin:$PATH" + +source "$DOTFILES/config/exports" +source "$DOTFILES/config/alias" +source "$DOTFILES/config/functions" + source "$DOTFILES/scripts/shared.sh" export COMPLETION_WAITING_DOTS=true @@ -17,11 +23,6 @@ path_prepend "$XDG_BIN_HOME" path_prepend "$HOME/.local/go/bin" path_prepend "$XDG_DATA_HOME/bob/nvim-bin" -source "$DOTFILES/config/exports-shell" -source "$DOTFILES/config/exports-apps" -source "$DOTFILES/config/alias" -source "$DOTFILES/config/functions" - export ZSH_CUSTOM_COMPLETION_PATH="$XDG_CONFIG_HOME/zsh/completion" x-dc "$ZSH_CUSTOM_COMPLETION_PATH" diff --git a/config/alias b/config/alias index afe7eec..da03a87 100755 --- a/config/alias +++ b/config/alias @@ -6,9 +6,9 @@ DOTFILES="$HOME/.dotfiles" # Get installed php versions from brew and setup aliases function x-set-php-aliases { - have brew && { + x-have brew && { local php_versions=() - while IFS="" read -r line; do php_versions+=("$line"); done < <(brew_installed | grep '^php') + while IFS="" read -r line; do php_versions+=("$line"); done < <(bkt -- brew list | grep '^php') php_error_reporting='-d error_reporting=22527' @@ -61,8 +61,8 @@ alias please="sudo " # Color the grep output alias grep='grep --color' -! have eza && alias ls='ls --color=auto' -have eza && { +! x-have eza && alias ls='ls --color=auto' +x-have eza && { alias ls='eza -h -s=type --git --icons --group-directories-first' } diff --git a/config/exports b/config/exports index db46ff6..fc4c207 100755 --- a/config/exports +++ b/config/exports @@ -2,7 +2,57 @@ # shellcheck shell=bash # vim: filetype=zsh +# Cache commands using bkt if installed +if command -v bkt >&/dev/null; then + bkt() + { + command bkt --cache-dir="$XDG_CACHE_HOME/bkt" "$@" + } +else + # If bkt isn't installed skip its arguments and just execute directly. + # Optionally write a msg to stderr suggesting users install bkt. + bkt() + { + while [[ "$1" == --* ]]; do shift; done + "$@" + } +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 +{ + command -v "$1" >&/dev/null +} + +# shorthand for checking if the system has the bin in path, +# this version uses caching +# usage: have php && php -v +function have +{ + bkt -- which "$1" >&/dev/null +} + +function brew_installed +{ + bkt -- brew list +} + +# shorthand for checking if brew package is installed +# usage: have_brew php && php -v +function have_brew +{ + ! have brew && return 125 + + if bkt -- brew list "$1" &> /dev/null; then + return 0 + else + return 1 + fi +} + source "$DOTFILES/config/exports-shell" source "$DOTFILES/config/exports-apps" -have nvim && export EDITOR="nvim" +x-have nvim && export EDITOR="nvim" diff --git a/config/exports-apps b/config/exports-apps index 687c58a..36f852e 100755 --- a/config/exports-apps +++ b/config/exports-apps @@ -21,15 +21,15 @@ x-dc "$ANSIBLE_GALAXY_CACHE_DIR" export ANDROID_HOME="$XDG_DATA_HOME/android" # bob manages nvim versions -path_prepend "$XDG_DATA_HOME/bob/nvim-bin" -have nvim && export EDITOR="nvim" +x-path-prepend "$XDG_DATA_HOME/bob/nvim-bin" +x-have nvim && export EDITOR="nvim" export HOMEBREW_NO_ENV_HINTS=true # composer, https://getcomposer.org/ export COMPOSER_HOME="$XDG_STATE_HOME/composer" export COMPOSER_BIN="$COMPOSER_HOME/vendor/bin" -path_append "$COMPOSER_BIN" +x-path-append "$COMPOSER_BIN" # docker, https://docs.docker.com/engine/reference/commandline/cli/ export DOCKER_CONFIG="$XDG_CONFIG_HOME/docker" @@ -39,7 +39,7 @@ export DOCKER_SCAN_SUGGEST=false # ffmpeg export FFMPEG_DATADIR="$XDG_CONFIG_HOME/ffmpeg" -have ffmpeg && x-dc "$FFMPEG_DATADIR" +x-have ffmpeg && x-dc "$FFMPEG_DATADIR" # GnuPG export GNUPGHOME="$XDG_DATA_HOME/gnupg" @@ -53,14 +53,14 @@ export NBRC_PATH="$XDG_CONFIG_HOME/nbrc" export NB_DIR="$XDG_STATE_HOME/nb" # NPM: Add npm packages to path -have node && { +x-have node && { NVM_NODE_BIN_DIR="$(dirname "$(which node)")" - path_append "$NVM_NODE_BIN_DIR" + x-path-append "$NVM_NODE_BIN_DIR" } # op (1Password cli) is present export OP_CACHE="$XDG_STATE_HOME/1password" -have op && { +x-have op && { [ "$DOTFILES_CURRENT_SHELL" = "zsh" ] && { eval "$(op completion zsh)" compdef _op op @@ -72,8 +72,8 @@ have op && { # pyenv, python environments export WORKON_HOME="$XDG_DATA_HOME/virtualenvs" export PYENV_ROOT="$XDG_STATE_HOME/pyenv" -have pyenv && { - path_append "$PYENV_ROOT/shims" +x-have pyenv && { + x-path-append "$PYENV_ROOT/shims" eval "$(pyenv init -)" } @@ -87,8 +87,8 @@ export BUNDLE_USER_CACHE="$XDG_CACHE_HOME"/bundle export BUNDLE_USER_PLUGIN="$XDG_DATA_HOME"/bundle export RBENV_ROOT="$XDG_STATE_HOME/rbenv" x-dc "$RBENV_ROOT" -have gem && path_append "${GEM_HOME}/bin" -have rbenv && { +x-have gem && x-path-append "${GEM_HOME}/bin" +x-have rbenv && { [ "$DOTFILES_CURRENT_SHELL" = "zsh" ] && eval "$(rbenv init - zsh)" [ "$DOTFILES_CURRENT_SHELL" = "bash" ] && eval "$(rbenv init - bash)" } @@ -96,7 +96,7 @@ have rbenv && { # Rust / cargo export RUSTUP_HOME="$XDG_DATA_HOME/rustup" export CARGO_HOME="$XDG_DATA_HOME/cargo" -path_append "$CARGO_HOME/bin" +x-path-append "$CARGO_HOME/bin" # screen export SCREENRC="$XDG_CONFIG_HOME/misc/screenrc" diff --git a/config/exports-shell b/config/exports-shell index 7977d8a..b1232e7 100755 --- a/config/exports-shell +++ b/config/exports-shell @@ -54,32 +54,3 @@ export GREP_OPTIONS="--color=auto" # update the values of LINES and COLUMNS. hash shopt 2> /dev/null && shopt -s checkwinsize -# Set dircolors based on the file, if it exists -have dircolors && eval $(dircolors "$XDG_CONFIG_HOME/dircolors") - -# If we are using zsh, color our dir lists and such -if [ "$DOTFILES_CURRENT_SHELL" = "-zsh" ]; then - [[ $ZSH_VERSION != "" ]] && { - autoload -U colors zsh/terminfo compinit - colors - compinit -d "$XDG_CACHE_HOME"/zsh/zcompdump-"$ZSH_VERSION" - - x-dc "$XDG_CACHE_HOME/zsh" - x-dc "$XDG_STATE_HOME/zsh" - export HISTFILE="$XDG_STATE_HOME/zsh/history" - zstyle ':completion:*' cache-path "$XDG_CACHE_HOME/zsh/zcompcache" - zstyle ':completion:*' list-colors "$LS_COLORS" - - # Load iterm2 shell integration - # https://iterm2.com/documentation-shell-integration.html - [[ -f "$XDG_BIN_HOME/iterm2_shell_integration.zsh" ]] \ - && source "$XDG_BIN_HOME/iterm2_shell_integration.zsh" - } -fi - -# If we are using bash -if [ "$DOTFILES_CURRENT_SHELL" = "bash" ]; then - # shellcheck source=../config/fzf/fzf.bash - [ -f "${DOTFILES}/config/fzf/fzf.bash" ] && - source "${DOTFILES}/config/fzf/fzf.bash" -fi diff --git a/local/bin/x-have b/local/bin/x-have index 8226d28..fcbc0ed 100755 --- a/local/bin/x-have +++ b/local/bin/x-have @@ -3,7 +3,7 @@ which "$1" >&/dev/null if [ $? -eq 0 ]; then - echo 0 + exit 0 else - echo 1 + exit 1 fi diff --git a/local/bin/x-path-append b/local/bin/x-path-append new file mode 100755 index 0000000..5ef768e --- /dev/null +++ b/local/bin/x-path-append @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# +# Add a directory to the beginning of the PATH if it's not already there. +# Usage: x-path-append + +x-path-remove "$1" +export PATH="${PATH:+"$PATH:"}$1" + diff --git a/local/bin/x-path-prepend b/local/bin/x-path-prepend new file mode 100755 index 0000000..4af3541 --- /dev/null +++ b/local/bin/x-path-prepend @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# +# Add a directory to the front of the PATH if it exists and is not already there +# Usage: x-path-prepend + +export PATH="$1${PATH:+":$PATH"}" + diff --git a/local/bin/x-path-remove b/local/bin/x-path-remove new file mode 100755 index 0000000..279b50f --- /dev/null +++ b/local/bin/x-path-remove @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# +# Remove a directory from the PATH +# Usage: x-path-remove + +export PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//') + diff --git a/scripts/shared.sh b/scripts/shared.sh index edb9563..2377380 100755 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -31,23 +31,23 @@ export XDG_RUNTIME_DIR="$HOME/.local/run" # usage: path_remove ~/.local/bin function path_remove { - PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//') + x-path-remove "$1" } # Append directory to the PATH # usage: path_append ~/.local/bin function path_append { - path_remove "$1" - PATH="${PATH:+"$PATH:"}$1" + x-path-remove "$1" + x-path-prepend "$1" } # Prepend directory to the PATH # usage: path_prepend ~/.local/bin function path_prepend { - path_remove "$1" - PATH="$1${PATH:+":$PATH"}" + x-path-remove "$1" + x-path-prepend "$1" } # Create directory if it doesn't exist already @@ -84,56 +84,6 @@ nonascii() LC_ALL=C grep -n '[^[:print:][:space:]]' "${@}" } -# Cache commands using bkt if installed -if command -v bkt >&/dev/null; then - bkt() - { - command bkt --cache-dir="$XDG_CACHE_HOME/bkt" "$@" - } -else - # If bkt isn't installed skip its arguments and just execute directly. - # Optionally write a msg to stderr suggesting users install bkt. - bkt() - { - while [[ "$1" == --* ]]; do shift; done - "$@" - } -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 -{ - command -v "$1" >&/dev/null -} - -# shorthand for checking if the system has the bin in path, -# this version uses caching -# usage: have php && php -v -function have -{ - bkt -- which "$1" >&/dev/null -} - -function brew_installed -{ - bkt -- brew list -} - -# shorthand for checking if brew package is installed -# usage: have_brew php && php -v -function have_brew -{ - ! have brew && return 125 - - if bkt -- brew list "$1" &> /dev/null; then - return 0 - else - return 1 - fi -} - CONFIG_PATH="$DOTFILES/config" # Load the shell dotfiles, and then some: