bundler.plugin.zsh 2.0 KB

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