2021-12-13 17:37:44 +01:00
|
|
|
## load chruby from different locations
|
2013-11-17 00:39:01 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
_source-from-omz-settings() {
|
|
|
|
local _chruby_path _chruby_auto
|
2024-03-26 09:55:16 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
zstyle -s :omz:plugins:chruby path _chruby_path || return 1
|
|
|
|
zstyle -s :omz:plugins:chruby auto _chruby_auto || return 1
|
2013-11-17 00:39:01 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
if [[ -r ${_chruby_path} ]]; then
|
|
|
|
source ${_chruby_path}
|
|
|
|
fi
|
2017-11-07 17:03:54 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
if [[ -r ${_chruby_auto} ]]; then
|
|
|
|
source ${_chruby_auto}
|
|
|
|
fi
|
2013-11-17 00:39:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
_source-from-homebrew() {
|
|
|
|
(( $+commands[brew] )) || return 1
|
2013-11-17 00:39:01 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
local _brew_prefix
|
|
|
|
# check default brew prefix
|
|
|
|
if [[ -h /usr/local/opt/chruby ]];then
|
|
|
|
_brew_prefix="/usr/local/opt/chruby"
|
|
|
|
else
|
2024-03-26 09:55:16 +01:00
|
|
|
# ok , it is not default prefix
|
2021-12-13 17:37:44 +01:00
|
|
|
# this call to brew is expensive ( about 400 ms ), so at least let's make it only once
|
|
|
|
_brew_prefix=$(brew --prefix chruby)
|
|
|
|
fi
|
2013-11-17 00:39:01 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
[[ -r "$_brew_prefix" ]] || return 1
|
2013-11-17 00:39:01 +01:00
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
source $_brew_prefix/share/chruby/chruby.sh
|
|
|
|
source $_brew_prefix/share/chruby/auto.sh
|
2013-11-17 00:39:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
_load-chruby-dirs() {
|
|
|
|
local dir
|
|
|
|
for dir in "$HOME/.rubies" "$PREFIX/opt/rubies"; do
|
|
|
|
if [[ -d "$dir" ]]; then
|
|
|
|
RUBIES+=("$dir")
|
|
|
|
fi
|
|
|
|
done
|
2013-11-17 00:39:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
# Load chruby
|
|
|
|
if _source-from-omz-settings; then
|
|
|
|
_load-chruby-dirs
|
2013-11-17 00:39:01 +01:00
|
|
|
elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then
|
2021-12-13 17:37:44 +01:00
|
|
|
source /usr/local/share/chruby/chruby.sh
|
|
|
|
source /usr/local/share/chruby/auto.sh
|
|
|
|
_load-chruby-dirs
|
|
|
|
elif _source-from-homebrew; then
|
|
|
|
_load-chruby-dirs
|
2013-11-17 00:39:01 +01:00
|
|
|
fi
|
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
unfunction _source-from-homebrew _source-from-omz-settings _load-chruby-dirs
|
|
|
|
|
|
|
|
|
|
|
|
## chruby utility functions and aliases
|
|
|
|
|
|
|
|
# rvm and rbenv plugins also provide this alias
|
|
|
|
alias rubies='chruby'
|
2013-11-17 00:39:01 +01:00
|
|
|
|
|
|
|
function current_ruby() {
|
2021-12-13 17:37:44 +01:00
|
|
|
local ruby
|
|
|
|
ruby="$(chruby | grep \* | tr -d '* ')"
|
|
|
|
if [[ $(chruby | grep -c \*) -eq 1 ]]; then
|
|
|
|
echo ${ruby}
|
|
|
|
else
|
|
|
|
echo "system"
|
|
|
|
fi
|
2013-11-17 00:39:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function chruby_prompt_info() {
|
2021-12-13 17:43:32 +01:00
|
|
|
echo "${$(current_ruby):gs/%/%%}"
|
2013-11-17 00:39:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:37:44 +01:00
|
|
|
# Complete chruby command with installed rubies
|
2016-08-03 17:57:48 +02:00
|
|
|
_chruby() {
|
2021-12-13 17:37:44 +01:00
|
|
|
compadd $(chruby | tr -d '* ')
|
|
|
|
if PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" command ruby &>/dev/null; then
|
|
|
|
compadd system
|
|
|
|
fi
|
2016-08-03 17:57:48 +02:00
|
|
|
}
|
2021-12-13 17:37:44 +01:00
|
|
|
|
2013-11-17 00:39:01 +01:00
|
|
|
compdef _chruby chruby
|
2021-12-13 17:37:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Simple definition completer for ruby-build
|
|
|
|
if command ruby-build &> /dev/null; then
|
|
|
|
_ruby-build() { compadd $(ruby-build --definitions) }
|
|
|
|
compdef _ruby-build ruby-build
|
|
|
|
fi
|