bundler.plugin.zsh 1.99 KB
Newer Older
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
  if _bundler-installed && _within-bundled-project; then
    local bundler_version=`bundle version | cut -d' ' -f3`
    if [[ $bundler_version > '1.4.0' || $bundler_version = '1.4.0' ]]; then
60
      if [[ "$OSTYPE" = darwin* ]]
61
      then
62
        local cores_num="$(sysctl -n hw.ncpu)"
63
64
65
66
67
68
69
70
71
72
73
74
      else
        local cores_num="$(nproc)"
      fi
      bundle install --jobs=$cores_num $@
    else
      bundle install $@
    fi
  else
    echo "Can't 'bundle install' outside a bundled project"
  fi
}

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

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

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

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

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