bundler.plugin.zsh 2.52 KB
Newer Older
1
2
## Aliases

3
alias ba="bundle add"
4
5
alias bck="bundle check"
alias bcn="bundle clean"
Dennis Hägler's avatar
Dennis Hägler committed
6
alias be="bundle exec"
7
alias bi="bundle_install"
Hakan Ensari's avatar
Hakan Ensari committed
8
alias bl="bundle list"
9
alias bo="bundle open"
Denis's avatar
Denis committed
10
alias bout="bundle outdated"
11
alias bp="bundle package"
Hakan Ensari's avatar
Hakan Ensari committed
12
alias bu="bundle update"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

## Functions

bundle_install() {
  # Bail out if bundler is not installed
  if (( ! $+commands[bundle] )); then
    echo "Bundler is not installed"
    return 1
  fi

  # Bail out if not in a bundled project
  if ! _within-bundled-project; then
    echo "Can't 'bundle install' outside a bundled project"
    return 1
  fi

  # Check the bundler version is at least 1.4.0
  autoload -Uz is-at-least
  local bundler_version=$(bundle version | cut -d' ' -f3)
  if ! is-at-least 1.4.0 "$bundler_version"; then
    bundle install "$@"
    return $?
  fi

  # If bundler is at least 1.4.0, use all the CPU cores to bundle install
  if [[ "$OSTYPE" = (darwin|freebsd)* ]]; then
    local cores_num="$(sysctl -n hw.ncpu)"
  else
    local cores_num="$(nproc)"
  fi
  bundle install --jobs="$cores_num" "$@"
}

## Gem wrapper
47

48
49
50
51
52
53
54
bundled_commands=(
  annotate
  cap
  capify
  cucumber
  foodcritic
  guard
55
  hanami
56
57
58
59
60
61
  irb
  jekyll
  kitchen
  knife
  middleman
  nanoc
62
  pry
63
64
65
66
67
  puma
  rackup
  rainbows
  rake
  rspec
68
  rubocop
69
  shotgun
70
  sidekiq
71
72
  spec
  spork
73
  spring
74
75
76
77
78
79
80
81
  strainer
  tailor
  taps
  thin
  thor
  unicorn
  unicorn_rails
)
82

83
84
85
86
87
# Remove $UNBUNDLED_COMMANDS from the bundled_commands list
for cmd in $UNBUNDLED_COMMANDS; do
  bundled_commands=(${bundled_commands#$cmd});
done

88
89
90
91
92
# Add $BUNDLED_COMMANDS to the bundled_commands list
for cmd in $BUNDLED_COMMANDS; do
  bundled_commands+=($cmd);
done

93
# Check if in the root or a subdirectory of a bundled project
94
_within-bundled-project() {
95
  local check_dir="$PWD"
96
97
98
99
100
  while [[ "$check_dir" != "/" ]]; do
    if [[ -f "$check_dir/Gemfile" || -f "$check_dir/gems.rb" ]]; then
      return 0
    fi
    check_dir="${check_dir:h}"
101
  done
102
  return 1
103
104
}

105
_run-with-bundler() {
106
107
108
109
110
111
112
  if (( ! $+commands[bundle] )) || ! _within-bundled-project; then
    "$@"
    return $?
  fi

  if [[ -f "./bin/${1}" ]]; then
    ./bin/${^^@}
113
  else
114
    bundle exec "$@"
115
116
117
  fi
}

118
for cmd in $bundled_commands; do
119
120
121
122
  # Create wrappers for bundled and unbundled execution
  eval "function unbundled_$cmd () { \"$cmd\" \"\$@\"; }"
  eval "function bundled_$cmd () { _run-with-bundler \"$cmd\" \"\$@\"; }"
  alias "$cmd"="bundled_$cmd"
123

124
125
126
  # Bind completion function to wrapped gem if available
  if (( $+functions[_$cmd] )); then
    compdef "_$cmd" "bundled_$cmd"="$cmd"
127
128
  fi
done
129
130

unset cmd bundled_commands