bundler.plugin.zsh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # Remove $UNBUNDLED_COMMANDS from the bundled_commands list
  9. for cmd in $UNBUNDLED_COMMANDS; do
  10. bundled_commands=(${bundled_commands#$cmd});
  11. done
  12. ## Functions
  13. bi() {
  14. if _bundler-installed && _within-bundled-project; then
  15. local bundler_version=`bundle version | cut -d' ' -f3`
  16. if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
  17. if [[ "$(uname)" == 'Darwin' ]]
  18. then
  19. local cores_num="$(sysctl hw.ncpu | awk '{print $2}')"
  20. else
  21. local cores_num="$(nproc)"
  22. fi
  23. bundle install --jobs=$cores_num $@
  24. else
  25. bundle install $@
  26. fi
  27. else
  28. echo "Can't 'bundle install' outside a bundled project"
  29. fi
  30. }
  31. _bundler-installed() {
  32. which bundle > /dev/null 2>&1
  33. }
  34. _within-bundled-project() {
  35. local check_dir=$PWD
  36. while [ $check_dir != "/" ]; do
  37. [ -f "$check_dir/Gemfile" ] && return
  38. check_dir="$(dirname $check_dir)"
  39. done
  40. false
  41. }
  42. _run-with-bundler() {
  43. if _bundler-installed && _within-bundled-project; then
  44. bundle exec $@
  45. else
  46. $@
  47. fi
  48. }
  49. ## Main program
  50. for cmd in $bundled_commands; do
  51. eval "function unbundled_$cmd () { $cmd \$@ }"
  52. eval "function bundled_$cmd () { _run-with-bundler $cmd \$@}"
  53. alias $cmd=bundled_$cmd
  54. if which _$cmd > /dev/null 2>&1; then
  55. compdef _$cmd bundled_$cmd=$cmd
  56. fi
  57. done