bundler.plugin.zsh 2.06 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

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

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

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

55
56
## Functions

57
bundle_install() {
58
59
60
61
62
63
  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`
64
    if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
65
      if [[ "$OSTYPE" = (darwin|freebsd)* ]]
66
      then
67
        local cores_num="$(sysctl -n hw.ncpu)"
68
69
70
71
72
73
74
75
76
77
      else
        local cores_num="$(nproc)"
      fi
      bundle install --jobs=$cores_num $@
    else
      bundle install $@
    fi
  fi
}

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

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

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

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

107
108
109
110
111
112
113
## 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
114
    compdef _$cmd bundled_$cmd=$cmd
115
116
  fi
done