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

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

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

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

57
58
## Functions

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

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

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

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

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

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