bundler.plugin.zsh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if [[ "$(uname)" == 'Darwin' ]]
  7. then
  8. local cores_num="$(sysctl hw.ncpu | awk '{print $2}')"
  9. else
  10. local cores_num="$(nproc)"
  11. fi
  12. eval "alias bi='bundle install --jobs=$cores_num'"
  13. # The following is based on https://github.com/gma/bundler-exec
  14. 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)
  15. # Remove $UNBUNDLED_COMMANDS from the bundled_commands list
  16. for cmd in $UNBUNDLED_COMMANDS; do
  17. bundled_commands=(${bundled_commands#$cmd});
  18. done
  19. ## Functions
  20. _bundler-installed() {
  21. which bundle > /dev/null 2>&1
  22. }
  23. _within-bundled-project() {
  24. local check_dir=$PWD
  25. while [ $check_dir != "/" ]; do
  26. [ -f "$check_dir/Gemfile" ] && return
  27. check_dir="$(dirname $check_dir)"
  28. done
  29. false
  30. }
  31. _run-with-bundler() {
  32. if _bundler-installed && _within-bundled-project; then
  33. bundle exec $@
  34. else
  35. $@
  36. fi
  37. }
  38. ## Main program
  39. for cmd in $bundled_commands; do
  40. eval "function unbundled_$cmd () { $cmd \$@ }"
  41. eval "function bundled_$cmd () { _run-with-bundler $cmd \$@}"
  42. alias $cmd=bundled_$cmd
  43. if which _$cmd > /dev/null 2>&1; then
  44. compdef _$cmd bundled_$cmd=$cmd
  45. fi
  46. done