bundler.plugin.zsh 2.05 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
30
  puma
  rackup
  rainbows
  rake
  rspec
  shotgun
31
  sidekiq
32
33
  spec
  spork
34
  spring
35
36
37
38
39
40
41
42
  strainer
  tailor
  taps
  thin
  thor
  unicorn
  unicorn_rails
)
43

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

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

54
55
## Functions

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

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

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

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

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

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