bundler.plugin.zsh 2.5 KB

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