parent
7b12fa9799
commit
5c14474eb2
2 changed files with 71 additions and 97 deletions
@ -1,121 +1,94 @@ |
|||||||
# |
## load chruby from different locations |
||||||
# INSTRUCTIONS |
|
||||||
# |
|
||||||
# With either a manual or brew installed chruby things should just work. |
|
||||||
# |
|
||||||
# If you'd prefer to specify an explicit path to load chruby from |
|
||||||
# you can set variables like so: |
|
||||||
# |
|
||||||
# zstyle :omz:plugins:chruby path /local/path/to/chruby.sh |
|
||||||
# zstyle :omz:plugins:chruby auto /local/path/to/auto.sh |
|
||||||
# |
|
||||||
# TODO |
|
||||||
# - autodetermine correct source path on non OS X systems |
|
||||||
# - completion if ruby-install exists |
|
||||||
|
|
||||||
# rvm and rbenv plugins also provide this alias |
_source-from-omz-settings() { |
||||||
alias rubies='chruby' |
local _chruby_path _chruby_auto |
||||||
|
|
||||||
|
zstyle -s :omz:plugins:chruby path _chruby_path || return 1 |
||||||
|
zstyle -s :omz:plugins:chruby auto _chruby_auto || return 1 |
||||||
|
|
||||||
|
if [[ -r ${_chruby_path} ]]; then |
||||||
|
source ${_chruby_path} |
||||||
|
fi |
||||||
|
|
||||||
_homebrew-installed() { |
if [[ -r ${_chruby_auto} ]]; then |
||||||
whence brew &> /dev/null |
source ${_chruby_auto} |
||||||
_xit=$? |
fi |
||||||
if [ $_xit -eq 0 ];then |
|
||||||
# ok , we have brew installed |
|
||||||
# speculatively we check default brew prefix |
|
||||||
if [ -h /usr/local/opt/chruby ];then |
|
||||||
_brew_prefix="/usr/local/opt/chruby" |
|
||||||
else |
|
||||||
# ok , it is not default prefix |
|
||||||
# this call to brew is expensive ( about 400 ms ), so at least let's make it only once |
|
||||||
_brew_prefix=$(brew --prefix chruby) |
|
||||||
fi |
|
||||||
return 0 |
|
||||||
else |
|
||||||
return $_xit |
|
||||||
fi |
|
||||||
} |
} |
||||||
|
|
||||||
_chruby-from-homebrew-installed() { |
_source-from-homebrew() { |
||||||
[ -r $_brew_prefix ] &> /dev/null |
(( $+commands[brew] )) || return 1 |
||||||
} |
|
||||||
|
|
||||||
_ruby-build_installed() { |
local _brew_prefix |
||||||
whence ruby-build &> /dev/null |
# check default brew prefix |
||||||
} |
if [[ -h /usr/local/opt/chruby ]];then |
||||||
|
_brew_prefix="/usr/local/opt/chruby" |
||||||
|
else |
||||||
|
# ok , it is not default prefix |
||||||
|
# this call to brew is expensive ( about 400 ms ), so at least let's make it only once |
||||||
|
_brew_prefix=$(brew --prefix chruby) |
||||||
|
fi |
||||||
|
|
||||||
_ruby-install-installed() { |
[[ -r "$_brew_prefix" ]] || return 1 |
||||||
whence ruby-install &> /dev/null |
|
||||||
} |
|
||||||
|
|
||||||
# Simple definition completer for ruby-build |
source $_brew_prefix/share/chruby/chruby.sh |
||||||
if _ruby-build_installed; then |
source $_brew_prefix/share/chruby/auto.sh |
||||||
_ruby-build() { compadd $(ruby-build --definitions) } |
|
||||||
compdef _ruby-build ruby-build |
|
||||||
fi |
|
||||||
|
|
||||||
_source_from_omz_settings() { |
|
||||||
local _chruby_path |
|
||||||
local _chruby_auto |
|
||||||
|
|
||||||
zstyle -s :omz:plugins:chruby path _chruby_path |
|
||||||
zstyle -s :omz:plugins:chruby auto _chruby_auto |
|
||||||
|
|
||||||
if [[ -r ${_chruby_path} ]]; then |
|
||||||
source ${_chruby_path} |
|
||||||
fi |
|
||||||
|
|
||||||
if [[ -r ${_chruby_auto} ]]; then |
|
||||||
source ${_chruby_auto} |
|
||||||
fi |
|
||||||
} |
} |
||||||
|
|
||||||
_chruby_dirs() { |
_load-chruby-dirs() { |
||||||
chrubydirs=($HOME/.rubies/ $PREFIX/opt/rubies) |
local dir |
||||||
for dir in chrubydirs; do |
for dir in "$HOME/.rubies" "$PREFIX/opt/rubies"; do |
||||||
if [[ -d $dir ]]; then |
if [[ -d "$dir" ]]; then |
||||||
RUBIES+=$dir |
RUBIES+=("$dir") |
||||||
fi |
fi |
||||||
done |
done |
||||||
} |
} |
||||||
|
|
||||||
if _homebrew-installed && _chruby-from-homebrew-installed ; then |
# Load chruby |
||||||
source $_brew_prefix/share/chruby/chruby.sh |
if _source-from-omz-settings; then |
||||||
source $_brew_prefix/share/chruby/auto.sh |
_load-chruby-dirs |
||||||
_chruby_dirs |
|
||||||
elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then |
elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then |
||||||
source /usr/local/share/chruby/chruby.sh |
source /usr/local/share/chruby/chruby.sh |
||||||
source /usr/local/share/chruby/auto.sh |
source /usr/local/share/chruby/auto.sh |
||||||
_chruby_dirs |
_load-chruby-dirs |
||||||
else |
elif _source-from-homebrew; then |
||||||
_source_from_omz_settings |
_load-chruby-dirs |
||||||
_chruby_dirs |
|
||||||
fi |
fi |
||||||
|
|
||||||
function ensure_chruby() { |
unfunction _source-from-homebrew _source-from-omz-settings _load-chruby-dirs |
||||||
$(whence chruby) |
|
||||||
} |
|
||||||
|
## chruby utility functions and aliases |
||||||
|
|
||||||
|
# rvm and rbenv plugins also provide this alias |
||||||
|
alias rubies='chruby' |
||||||
|
|
||||||
function current_ruby() { |
function current_ruby() { |
||||||
local _ruby |
local ruby |
||||||
_ruby="$(chruby |grep \* |tr -d '* ')" |
ruby="$(chruby | grep \* | tr -d '* ')" |
||||||
if [[ $(chruby |grep -c \*) -eq 1 ]]; then |
if [[ $(chruby | grep -c \*) -eq 1 ]]; then |
||||||
echo ${_ruby} |
echo ${ruby} |
||||||
else |
else |
||||||
echo "system" |
echo "system" |
||||||
fi |
fi |
||||||
} |
} |
||||||
|
|
||||||
function chruby_prompt_info() { |
function chruby_prompt_info() { |
||||||
echo "$(current_ruby)" |
echo "$(current_ruby)" |
||||||
} |
} |
||||||
|
|
||||||
# complete on installed rubies |
# Complete chruby command with installed rubies |
||||||
_chruby() { |
_chruby() { |
||||||
compadd $(chruby | tr -d '* ') |
compadd $(chruby | tr -d '* ') |
||||||
local default_path='/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' |
if PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" command ruby &>/dev/null; then |
||||||
if PATH=${default_path} type ruby &> /dev/null; then |
compadd system |
||||||
compadd system |
fi |
||||||
fi |
|
||||||
} |
} |
||||||
|
|
||||||
compdef _chruby chruby |
compdef _chruby chruby |
||||||
|
|
||||||
|
|
||||||
|
# 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 |
||||||
|
|||||||
Loading…
Reference in new issue