chruby.plugin.zsh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #
  2. # INSTRUCTIONS
  3. #
  4. # With either a manual or brew installed chruby things should just work.
  5. #
  6. # If you'd prefer to specify an explicit path to load chruby from
  7. # you can set variables like so:
  8. #
  9. # zstyle :omz:plugins:chruby path /local/path/to/chruby.sh
  10. # zstyle :omz:plugins:chruby auto /local/path/to/auto.sh
  11. #
  12. # TODO
  13. # - autodetermine correct source path on non OS X systems
  14. # - completion if ruby-install exists
  15. # rvm and rbenv plugins also provide this alias
  16. alias rubies='chruby'
  17. _homebrew-installed() {
  18. whence brew &> /dev/null
  19. _xit=$?
  20. if [ $_xit -eq 0 ];then
  21. # ok , we have brew installed
  22. # speculatively we check default brew prefix
  23. if [ -h /usr/local/opt/chruby ];then
  24. _brew_prefix="/usr/local/opt/chruby"
  25. else
  26. # ok , it is not default prefix
  27. # this call to brew is expensive ( about 400 ms ), so at least let's make it only once
  28. _brew_prefix=$(brew --prefix chruby)
  29. fi
  30. return 0
  31. else
  32. return $_xit
  33. fi
  34. }
  35. _chruby-from-homebrew-installed() {
  36. [ -r _brew_prefix ] &> /dev/null
  37. }
  38. _ruby-build_installed() {
  39. whence ruby-build &> /dev/null
  40. }
  41. _ruby-install-installed() {
  42. whence ruby-install &> /dev/null
  43. }
  44. # Simple definition completer for ruby-build
  45. if _ruby-build_installed; then
  46. _ruby-build() { compadd $(ruby-build --definitions) }
  47. compdef _ruby-build ruby-build
  48. fi
  49. _source_from_omz_settings() {
  50. local _chruby_path
  51. local _chruby_auto
  52. zstyle -s :omz:plugins:chruby path _chruby_path
  53. zstyle -s :omz:plugins:chruby auto _chruby_auto
  54. if [[ -r ${_chruby_path} ]]; then
  55. source ${_chruby_path}
  56. fi
  57. if [[ -r ${_chruby_auto} ]]; then
  58. source ${_chruby_auto}
  59. fi
  60. }
  61. _chruby_dirs() {
  62. chrubydirs=($HOME/.rubies/ $PREFIX/opt/rubies)
  63. for dir in chrubydirs; do
  64. if [[ -d $dir ]]; then
  65. RUBIES+=$dir
  66. fi
  67. done
  68. }
  69. if _homebrew-installed && _chruby-from-homebrew-installed ; then
  70. source $_brew_prefix/share/chruby/chruby.sh
  71. source $_brew_prefix/share/chruby/auto.sh
  72. _chruby_dirs
  73. elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then
  74. source /usr/local/share/chruby/chruby.sh
  75. source /usr/local/share/chruby/auto.sh
  76. _chruby_dirs
  77. else
  78. _source_from_omz_settings
  79. _chruby_dirs
  80. fi
  81. function ensure_chruby() {
  82. $(whence chruby)
  83. }
  84. function current_ruby() {
  85. local _ruby
  86. _ruby="$(chruby |grep \* |tr -d '* ')"
  87. if [[ $(chruby |grep -c \*) -eq 1 ]]; then
  88. echo ${_ruby}
  89. else
  90. echo "system"
  91. fi
  92. }
  93. function chruby_prompt_info() {
  94. echo "$(current_ruby)"
  95. }
  96. # complete on installed rubies
  97. _chruby() {
  98. compadd $(chruby | tr -d '* ')
  99. local default_path='/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
  100. if PATH=${default_path} type ruby &> /dev/null; then
  101. compadd system
  102. fi
  103. }
  104. compdef _chruby chruby