bundler.plugin.zsh 2.12 KB
Newer Older
Dennis Hägler's avatar
Dennis Hägler committed
1
alias be="bundle exec"
Hakan Ensari's avatar
Hakan Ensari committed
2
alias bl="bundle list"
3
alias bp="bundle package"
4
alias bo="bundle open"
Denis's avatar
Denis committed
5
alias bout="bundle outdated"
Hakan Ensari's avatar
Hakan Ensari committed
6
alias bu="bundle update"
7
alias bi="bundle_install"
Satoshi Ohmori's avatar
Satoshi Ohmori committed
8
alias bcn="bundle clean"
9
alias bck="bundle check"
10

11
12
13
14
15
16
17
bundled_commands=(
  annotate
  cap
  capify
  cucumber
  foodcritic
  guard
18
  hanami
19
20
21
22
23
24
  irb
  jekyll
  kitchen
  knife
  middleman
  nanoc
25
  pry
26
27
28
29
30
  puma
  rackup
  rainbows
  rake
  rspec
31
  rubocop
32
  shotgun
33
  sidekiq
34
35
  spec
  spork
36
  spring
37
38
39
40
41
42
43
44
  strainer
  tailor
  taps
  thin
  thor
  unicorn
  unicorn_rails
)
45

46
47
48
49
50
# Remove $UNBUNDLED_COMMANDS from the bundled_commands list
for cmd in $UNBUNDLED_COMMANDS; do
  bundled_commands=(${bundled_commands#$cmd});
done

51
52
53
54
55
# Add $BUNDLED_COMMANDS to the bundled_commands list
for cmd in $BUNDLED_COMMANDS; do
  bundled_commands+=($cmd);
done

56
57
## Functions

58
bundle_install() {
59
60
61
62
63
64
  if ! _bundler-installed; then
    echo "Bundler is not installed"
  elif ! _within-bundled-project; then
    echo "Can't 'bundle install' outside a bundled project"
  else
    local bundler_version=`bundle version | cut -d' ' -f3`
65
    if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
66
      if [[ "$OSTYPE" = (darwin|freebsd)* ]]
67
      then
68
        local cores_num="$(sysctl -n hw.ncpu)"
69
70
71
72
73
74
75
76
77
78
      else
        local cores_num="$(nproc)"
      fi
      bundle install --jobs=$cores_num $@
    else
      bundle install $@
    fi
  fi
}

79
80
81
82
83
_bundler-installed() {
  which bundle > /dev/null 2>&1
}

_within-bundled-project() {
84
85
  local check_dir="$PWD"
  while [ "$check_dir" != "/" ]; do
86
    [ -f "$check_dir/Gemfile" -o -f "$check_dir/gems.rb" ] && return
87
88
89
90
91
    check_dir="$(dirname $check_dir)"
  done
  false
}

92
93
94
95
_binstubbed() {
  [ -f "./bin/${1}" ]
}

96
97
_run-with-bundler() {
  if _bundler-installed && _within-bundled-project; then
98
    if _binstubbed $1; then
99
      ./bin/${^^@}
100
101
102
    else
      bundle exec $@
    fi
103
  else
Hakan Ensari's avatar
Hakan Ensari committed
104
    $@
105
106
107
  fi
}

108
109
110
111
112
113
114
## Main program
for cmd in $bundled_commands; do
  eval "function unbundled_$cmd () { $cmd \$@ }"
  eval "function bundled_$cmd () { _run-with-bundler $cmd \$@}"
  alias $cmd=bundled_$cmd

  if which _$cmd > /dev/null 2>&1; then
115
    compdef _$cmd bundled_$cmd=$cmd
116
117
  fi
done