bundler.plugin.zsh 2.0 KB

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