bundler.plugin.zsh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. alias bi="bundle_install"
  7. alias bcn="bundle clean"
  8. bundled_commands=(
  9. annotate
  10. cap
  11. capify
  12. cucumber
  13. foodcritic
  14. guard
  15. irb
  16. jekyll
  17. kitchen
  18. knife
  19. middleman
  20. nanoc
  21. pry
  22. puma
  23. rackup
  24. rainbows
  25. rake
  26. rspec
  27. shotgun
  28. sidekiq
  29. spec
  30. spork
  31. spring
  32. strainer
  33. tailor
  34. taps
  35. thin
  36. thor
  37. unicorn
  38. unicorn_rails
  39. )
  40. # Remove $UNBUNDLED_COMMANDS from the bundled_commands list
  41. for cmd in $UNBUNDLED_COMMANDS; do
  42. bundled_commands=(${bundled_commands#$cmd});
  43. done
  44. # Add $BUNDLED_COMMANDS to the bundled_commands list
  45. for cmd in $BUNDLED_COMMANDS; do
  46. bundled_commands+=($cmd);
  47. done
  48. ## Functions
  49. bundle_install() {
  50. if _bundler-installed && _within-bundled-project; then
  51. local bundler_version=`bundle version | cut -d' ' -f3`
  52. if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
  53. if [[ "$OSTYPE" = darwin* ]]
  54. then
  55. local cores_num="$(sysctl hw.ncpu | awk '{print $2}')"
  56. else
  57. local cores_num="$(nproc)"
  58. fi
  59. bundle install --jobs=$cores_num $@
  60. else
  61. bundle install $@
  62. fi
  63. else
  64. echo "Can't 'bundle install' outside a bundled project"
  65. fi
  66. }
  67. _bundler-installed() {
  68. which bundle > /dev/null 2>&1
  69. }
  70. _within-bundled-project() {
  71. local check_dir="$PWD"
  72. while [ "$check_dir" != "/" ]; do
  73. [ -f "$check_dir/Gemfile" ] && return
  74. check_dir="$(dirname $check_dir)"
  75. done
  76. false
  77. }
  78. _binstubbed() {
  79. [ -f "./bin/${1}" ]
  80. }
  81. _run-with-bundler() {
  82. if _bundler-installed && _within-bundled-project; then
  83. if _binstubbed $1; then
  84. ./bin/$@
  85. else
  86. bundle exec $@
  87. fi
  88. else
  89. $@
  90. fi
  91. }
  92. ## Main program
  93. for cmd in $bundled_commands; do
  94. eval "function unbundled_$cmd () { $cmd \$@ }"
  95. eval "function bundled_$cmd () { _run-with-bundler $cmd \$@}"
  96. alias $cmd=bundled_$cmd
  97. if which _$cmd > /dev/null 2>&1; then
  98. compdef _$cmd bundled_$cmd=$cmd
  99. fi
  100. done