bundler.plugin.zsh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. alias be="bundle exec"
  2. alias bl="bundle list"
  3. alias bp="bundle package"
  4. alias bo="bundle open"
  5. alias bu="bundle update"
  6. # The following is based on https://github.com/gma/bundler-exec
  7. bundled_commands=(annotate berks cap capify cucumber foodcritic foreman guard jekyll kitchen knife middleman nanoc rackup rainbows rake rspec ruby shotgun spec spin spork strainer tailor taps thin thor unicorn unicorn_rails puma)
  8. ## Functions
  9. bi() {
  10. if _bundler-installed && _within-bundled-project; then
  11. local bundler_version=`bundle version | cut -d' ' -f3`
  12. if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
  13. if [[ "$(uname)" == 'Darwin' ]]
  14. then
  15. local cores_num="$(sysctl hw.ncpu | awk '{print $2}')"
  16. else
  17. local cores_num="$(nproc)"
  18. fi
  19. bundle install --jobs=$cores_num $@
  20. else
  21. bundle install $@
  22. fi
  23. else
  24. echo "Can't 'bundle install' outside a bundled project"
  25. fi
  26. }
  27. _bundler-installed() {
  28. which bundle > /dev/null 2>&1
  29. }
  30. _within-bundled-project() {
  31. local check_dir=$PWD
  32. while [ $check_dir != "/" ]; do
  33. [ -f "$check_dir/Gemfile" ] && return
  34. check_dir="$(dirname $check_dir)"
  35. done
  36. false
  37. }
  38. _run-with-bundler() {
  39. if _bundler-installed && _within-bundled-project; then
  40. bundle exec $@
  41. else
  42. $@
  43. fi
  44. }
  45. ## Main program
  46. for cmd in $bundled_commands; do
  47. eval "function unbundled_$cmd () { $cmd \$@ }"
  48. eval "function bundled_$cmd () { _run-with-bundler $cmd \$@}"
  49. alias $cmd=bundled_$cmd
  50. if which _$cmd > /dev/null 2>&1; then
  51. compdef _$cmd bundled_$cmd=$cmd
  52. fi
  53. done