bundler.plugin.zsh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_JOBS="$cores_num" bundle install "$@"
  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. bundled_commands=(${bundled_commands:|UNBUNDLED_COMMANDS})
  76. unset UNBUNDLED_COMMANDS
  77. # Add $BUNDLED_COMMANDS to the bundled_commands list
  78. bundled_commands+=($BUNDLED_COMMANDS)
  79. unset BUNDLED_COMMANDS
  80. # Check if in the root or a subdirectory of a bundled project
  81. _within-bundled-project() {
  82. local check_dir="$PWD"
  83. while [[ "$check_dir" != "/" ]]; do
  84. if [[ -f "$check_dir/Gemfile" || -f "$check_dir/gems.rb" ]]; then
  85. return 0
  86. fi
  87. check_dir="${check_dir:h}"
  88. done
  89. return 1
  90. }
  91. _run-with-bundler() {
  92. if (( ! $+commands[bundle] )) || ! _within-bundled-project; then
  93. "$@"
  94. return $?
  95. fi
  96. if [[ -f "./bin/${1}" ]]; then
  97. ./bin/${^^@}
  98. else
  99. bundle exec "$@"
  100. fi
  101. }
  102. for cmd in $bundled_commands; do
  103. # Create wrappers for bundled and unbundled execution
  104. eval "function unbundled_$cmd () { \"$cmd\" \"\$@\"; }"
  105. eval "function bundled_$cmd () { _run-with-bundler \"$cmd\" \"\$@\"; }"
  106. alias "$cmd"="bundled_$cmd"
  107. # Bind completion function to wrapped gem if available
  108. if (( $+functions[_$cmd] )); then
  109. compdef "_$cmd" "bundled_$cmd"="$cmd"
  110. fi
  111. done
  112. unset cmd bundled_commands