bundler.plugin.zsh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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; then
  53. echo "Bundler is not installed"
  54. elif ! _within-bundled-project; then
  55. echo "Can't 'bundle install' outside a bundled project"
  56. else
  57. local bundler_version=`bundle version | cut -d' ' -f3`
  58. if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
  59. if [[ "$OSTYPE" = (darwin|freebsd)* ]]
  60. then
  61. local cores_num="$(sysctl -n hw.ncpu)"
  62. else
  63. local cores_num="$(nproc)"
  64. fi
  65. bundle install --jobs=$cores_num $@
  66. else
  67. bundle install $@
  68. fi
  69. fi
  70. }
  71. _bundler-installed() {
  72. which bundle > /dev/null 2>&1
  73. }
  74. _within-bundled-project() {
  75. local check_dir="$PWD"
  76. while [ "$check_dir" != "/" ]; do
  77. [ -f "$check_dir/Gemfile" ] && return
  78. check_dir="$(dirname $check_dir)"
  79. done
  80. false
  81. }
  82. _binstubbed() {
  83. [ -f "./bin/${1}" ]
  84. }
  85. _run-with-bundler() {
  86. if _bundler-installed && _within-bundled-project; then
  87. if _binstubbed $1; then
  88. ./bin/$@
  89. else
  90. bundle exec $@
  91. fi
  92. else
  93. $@
  94. fi
  95. }
  96. ## Main program
  97. for cmd in $bundled_commands; do
  98. eval "function unbundled_$cmd () { $cmd \$@ }"
  99. eval "function bundled_$cmd () { _run-with-bundler $cmd \$@}"
  100. alias $cmd=bundled_$cmd
  101. if which _$cmd > /dev/null 2>&1; then
  102. compdef _$cmd bundled_$cmd=$cmd
  103. fi
  104. done