bundler.plugin.zsh 2.1 KB

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