bundler-exec.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. # Automatically run Ruby scripts with "bundle exec" (but only when appropriate).
  3. # http://effectif.com/ruby/automating-bundle-exec
  4. # Github: https://github.com/gma/bundler-exec
  5. ## Functions
  6. rbenv-installed()
  7. {
  8. which rbenv > /dev/null 2>&1
  9. }
  10. bundler-installed()
  11. {
  12. if rbenv-installed; then
  13. rbenv which bundle > /dev/null 2>&1
  14. else
  15. which bundle > /dev/null 2>&1
  16. fi
  17. }
  18. within-bundled-project()
  19. {
  20. local dir="$(pwd)"
  21. while [ "$(dirname $dir)" != "/" ]; do
  22. [ -f "$dir/Gemfile" ] && return
  23. dir="$(dirname $dir)"
  24. done
  25. false
  26. }
  27. run-with-bundler()
  28. {
  29. if bundler-installed && within-bundled-project; then
  30. bundle exec "$@"
  31. else
  32. "$@"
  33. fi
  34. }
  35. ## Main program
  36. if rbenv-installed; then
  37. BUNDLED_COMMANDS=$(/bin/ls ~/.rbenv/shims)
  38. else
  39. BUNDLED_COMMANDS="${BUNDLED_COMMANDS:-
  40. cap
  41. capify
  42. cucumber
  43. foreman
  44. guard
  45. haml
  46. heroku
  47. html2haml
  48. rackup
  49. rails
  50. rake
  51. rake2thor
  52. rspec
  53. ruby
  54. sass
  55. sass-convert
  56. serve
  57. shotgun
  58. spec
  59. spork
  60. thin
  61. thor
  62. tilt
  63. tt
  64. turn
  65. unicorn
  66. unicorn_rails
  67. }"
  68. fi
  69. for CMD in $BUNDLED_COMMANDS; do
  70. if [[ $CMD != "bundle" && $CMD != "gem" ]]; then
  71. alias $CMD="run-with-bundler $CMD"
  72. fi
  73. done