bundler-exec.plugin.zsh 753 Bytes
Newer Older
Bodo Tasche's avatar
Bodo Tasche committed
1
# This plugin is based on https://github.com/gma/bundler-exec
Bodo Tasche's avatar
Bodo Tasche committed
2
# modify the bundled_commands if needed
Bodo Tasche's avatar
Bodo Tasche committed
3

Bodo Tasche's avatar
Bodo Tasche committed
4
bundled_commands=(cucumber heroku rackup rails rake rspec ruby shotgun spec spork)
Bodo Tasche's avatar
Bodo Tasche committed
5
6
7

## Functions

Bodo Tasche's avatar
Bodo Tasche committed
8
_bundler-installed() {
Bodo Tasche's avatar
Bodo Tasche committed
9
10
11
    which bundle > /dev/null 2>&1
}

Bodo Tasche's avatar
Bodo Tasche committed
12
_within-bundled-project() {
13
14
15
    local check_dir=$PWD
    while [ "$(dirname $check_dir)" != "/" ]; do
        [ -f "$check_dir/Gemfile" ] && return
Bodo Tasche's avatar
Bodo Tasche committed
16
        check_dir="$(dirname $check_dir)"
Bodo Tasche's avatar
Bodo Tasche committed
17
18
19
20
    done
    false
}

Bodo Tasche's avatar
Bodo Tasche committed
21
_run-with-bundler() {
Bodo Tasche's avatar
Bodo Tasche committed
22
23
    local command="$1"
    shift
24
    if _bundler-installed && _within-bundled-project; then
Bodo Tasche's avatar
Bodo Tasche committed
25
26
        bundle exec $command "$@"
    else
Bodo Tasche's avatar
Bodo Tasche committed
27
      $command "$@"
Bodo Tasche's avatar
Bodo Tasche committed
28
29
30
31
    fi
}

## Main program
Bodo Tasche's avatar
Bodo Tasche committed
32
for cmd in $bundled_commands; do
33
   alias $cmd="_run-with-bundler $cmd"
Bodo Tasche's avatar
Bodo Tasche committed
34
done