bundler.plugin.zsh 2.0 KB

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