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