bundler.plugin.zsh 2.1 KB

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