mirror of
https://github.com/keanuplayz/dotfiles.git
synced 2024-08-15 02:33:12 +00:00
[zsh] refactor *path variable manipulation functions
This commit is contained in:
parent
093bd9837c
commit
894a81f2b3
2 changed files with 54 additions and 49 deletions
83
zsh/path.zsh
83
zsh/path.zsh
|
@ -2,77 +2,74 @@
|
||||||
|
|
||||||
# tie these env variables to zsh arrays
|
# tie these env variables to zsh arrays
|
||||||
typeset -T PKG_CONFIG_PATH pkg_config_path ':'
|
typeset -T PKG_CONFIG_PATH pkg_config_path ':'
|
||||||
|
export PKG_CONFIG_PATH
|
||||||
# keep only unique values in these arrays
|
|
||||||
typeset -U path fpath manpath pkg_config_path
|
|
||||||
export -U PATH FPATH MANPATH PKG_CONFIG_PATH
|
|
||||||
|
|
||||||
path_append() {
|
|
||||||
local arr_name="$1" value="$2"
|
|
||||||
if eval "if (( \${${arr_name}[(ie)\$value]} > \${#${arr_name}} ))"; then
|
|
||||||
eval "${arr_name}+=(\"\$value\")"
|
|
||||||
eval "${arr_name}=(\"\${${arr_name}[@]}\" \"\$value\")"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
path_prepend() {
|
path_prepend() {
|
||||||
local arr_name="$1" value="$2"
|
if (( $# < 2 )); then
|
||||||
if eval "if (( \${${arr_name}[(ie)\$value]} > \${#${arr_name}} ))"; then
|
print >&2 "usage: $0 <var_name> <value...>"
|
||||||
eval "${arr_name}=(\"\$value\" \"\${${arr_name}[@]}\")"
|
return 1
|
||||||
fi
|
fi
|
||||||
|
local var_name="$1"; shift
|
||||||
|
local value; for value in "$@"; do
|
||||||
|
if eval "(( \${${var_name}[(ie)\$value]} > \${#${var_name}} ))"; then
|
||||||
|
eval "${var_name}=(\"\$value\" \"\${${var_name}[@]}\")"
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_macos; then
|
if is_macos; then
|
||||||
path=(
|
# local Python binaries (for some reason they don't go into ~/.local/bin, but
|
||||||
~/Library/Python/*/bin
|
# instead into the garbage ~/Library directory)
|
||||||
/usr/local/opt/ruby/bin
|
path_prepend path ~/Library/Python/*/bin(OnN)
|
||||||
/usr/local/opt/file-formula/bin # file
|
|
||||||
/usr/local/opt/gnu-tar/libexec/gnubin # GNU tar
|
|
||||||
/usr/local/opt/unzip/bin # GNU unzip
|
|
||||||
/usr/local/opt/openssl/bin # openssl
|
|
||||||
/usr/local/opt/gnu-getopt/bin # getopt
|
|
||||||
/usr/local/opt/findutils/libexec/gnubin # GNU findutils
|
|
||||||
/usr/local/opt/coreutils/libexec/gnubin # GNU coreutils
|
|
||||||
/usr/local/opt/curl/bin # curl
|
|
||||||
"${path[@]}"
|
|
||||||
)
|
|
||||||
|
|
||||||
manpath=(
|
# GNU counterparts of command line utilities
|
||||||
/usr/local/opt/findutils/libexec/gnuman # GNU findutils
|
path_prepend path /usr/local/opt/*/libexec/gnubin(N)
|
||||||
"${manpath[@]}"
|
path_prepend manpath /usr/local/opt/*/libexec/gnuman(N)
|
||||||
)
|
|
||||||
|
|
||||||
for formula in ruby openssl curl; do
|
# add some keg-only Homebrew formulas
|
||||||
|
for formula in curl file-formula openssl ruby; do
|
||||||
formula_path="/usr/local/opt/$formula"
|
formula_path="/usr/local/opt/$formula"
|
||||||
if [[ -d "$formula_path" ]]; then
|
if [[ ! -d "$formula_path" ]]; then
|
||||||
pkg_config_path+=( "$formula_path"/lib/pkgconfig )
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -d "$formula_path/bin" ]]; then
|
||||||
|
path_prepend path "$formula_path/bin"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -d "$formula_path/lib/pkgconfig" ]]; then
|
||||||
|
path_prepend pkg_config_path "$formula_path/lib/pkgconfig"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
unset formula
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Yarn global packages
|
# Yarn global packages
|
||||||
path=(~/.yarn/bin "${path[@]}")
|
path_prepend path ~/.yarn/bin
|
||||||
|
|
||||||
# Go
|
# Go
|
||||||
export GOPATH=~/.go
|
export GOPATH=~/.go
|
||||||
path=("$GOPATH/bin" "${path[@]}")
|
path_prepend path "$GOPATH/bin"
|
||||||
|
|
||||||
# Rust
|
# Rust
|
||||||
path=(~/.cargo/bin "${path[@]}")
|
path_prepend path ~/.cargo/bin
|
||||||
# check if the Rust toolchain was installed via rustup
|
# check if the Rust toolchain was installed via rustup
|
||||||
if rustup_home="$(rustup show home 2> /dev/null)" &&
|
if rustup_home="$(rustup show home 2> /dev/null)" &&
|
||||||
rust_sysroot="$(rustc --print sysroot 2> /dev/null)" &&
|
rust_sysroot="$(rustc --print sysroot 2> /dev/null)" &&
|
||||||
[[ -d "$rustup_home" && -d "$rust_sysroot" && "$rust_sysroot" == "$rustup_home"/* ]]
|
[[ -d "$rustup_home" && -d "$rust_sysroot" && "$rust_sysroot" == "$rustup_home"/* ]]
|
||||||
then
|
then
|
||||||
# add paths of the selected Rust toolchain
|
# add paths of the selected Rust toolchain
|
||||||
fpath=("$rust_sysroot/share/zsh/site-functions" "${fpath[@]}")
|
path_prepend fpath "$rust_sysroot/share/zsh/site-functions"
|
||||||
manpath=("$rust_sysroot/share/man" "${manpath[@]}")
|
path_prepend manpath "$rust_sysroot/share/man"
|
||||||
fi
|
fi
|
||||||
unset rustup_home rust_sysroot
|
unset rustup_home rust_sysroot
|
||||||
|
|
||||||
# add my binaries and completions
|
# add my binaries and completions
|
||||||
path=("${ZSH_DOTFILES:h}/scripts" "${path[@]}")
|
path_prepend path "${ZSH_DOTFILES:h}/scripts"
|
||||||
fpath=("$ZSH_DOTFILES/completions" "${fpath[@]}")
|
path_prepend fpath "$ZSH_DOTFILES/completions"
|
||||||
|
|
||||||
# add user binaries
|
# add user binaries
|
||||||
path=(~/.local/bin "${path[@]}")
|
path_prepend path ~/.local/bin
|
||||||
|
|
||||||
|
unfunction path_prepend
|
||||||
|
|
20
zsh/zplg.zsh
20
zsh/zplg.zsh
|
@ -381,18 +381,26 @@ plugin() {
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local var_name="$1" operator="$2"; shift 2; local values=("$@")
|
local var_name="$1" operator="$2"; shift 2
|
||||||
|
|
||||||
if [[ "$var_name" != *path || "${(Pt)var_name}" != array* ]]; then
|
if [[ "$var_name" != *path || "${(Pt)var_name}" != array* ]]; then
|
||||||
_zplg_error "unknown path variable $var_name"
|
_zplg_error "unknown path variable $var_name"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
case "$operator" in
|
if [[ "$operator" != (prepend|append) ]]; then
|
||||||
prepend) eval "$var_name=(\"\$plugin_dir/\"\${^values} \${$var_name[@]})" ;;
|
_zplg_error "unknown operator $operator"
|
||||||
append) eval "$var_name=(\${$var_name[@]} \"\$plugin_dir/\"\${^values})" ;;
|
return 1
|
||||||
*) _zplg_error "unknown $0 operator $operator"
|
fi
|
||||||
esac
|
|
||||||
|
local value; for value in "$plugin_dir/"${^@}; do
|
||||||
|
if eval "(( \${${var_name}[(ie)\$value]} > \${#${var_name}} ))"; then
|
||||||
|
case "$operator" in
|
||||||
|
prepend) eval "$var_name=(\"\$value\" \${$var_name[@]})" ;;
|
||||||
|
append) eval "$var_name=(\${$var_name[@]} \"\$value\")" ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin-cfg-git-checkout-version() {
|
plugin-cfg-git-checkout-version() {
|
||||||
|
|
Loading…
Reference in a new issue