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
17
18
19
20
21
22
bundled_commands=(
  annotate
  cap
  capify
  cucumber
  foodcritic
  guard
  irb
  jekyll
  kitchen
  knife
  middleman
  nanoc
23
  pry
24
25
26
27
28
29
  puma
  rackup
  rainbows
  rake
  rspec
  shotgun
30
  sidekiq
31
32
  spec
  spork
33
  spring
34
35
36
37
38
39
40
41
  strainer
  tailor
  taps
  thin
  thor
  unicorn
  unicorn_rails
)
42

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

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

53
54
## Functions

55
bundle_install() {
56
57
58
  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
59
      if [[ "$OSTYPE" = darwin* ]]
60
      then
61
        local cores_num="$(sysctl -n hw.ncpu)"
62
63
64
65
66
67
68
69
70
71
72
73
      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
}

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

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

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

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

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