Jelajahi Sumber

Re-written to work with bundler 0.9.x.

Graham Ashton 15 tahun lalu
induk
melakukan
1e28e40aa6
2 mengubah file dengan 35 tambahan dan 124 penghapusan
  1. 24 20
      README.md
  2. 11 104
      bundler-bin.sh

+ 24 - 20
README.md

@@ -1,35 +1,39 @@
 bundler-bin
 ===========
 
-bundler-bin provides a Bash function that updates your PATH environment
-variable to include a project's ./bin directory if the project's Ruby gem
-dependencies are being managed with bundler.
+bundler is a great way to manage the gem dependencies in your Ruby project.
 
-You can find bundler here: http://github.com/wycats/bundler
+One of bundler's nifty features is the `bundle exec` command which allows
+you to run an executable (such as rake) in the context of your bundled gem
+dependencies. In other words, you'll only be able to access the gems that
+you've told bundler that you want to use.
 
-Gems that are managed by bundler will install their scripts in a directory
-called `<project-root>/bin`, and you should run these commands in preference
-to the commands that are installed with your system's gems. If your
-project's bin directory is not in your path it's very easy to forget to
-specify the correct command, which can cause no end of subtle problems.
-bundler-bin fixes this problem by automatically updating your PATH as you
-change directories. It defines a function for updating PATH via the Bash
-PROMPT_COMMAND hook.
+To run a command in this way you need to prefix it with 'bundle exec', like
+so:
 
-bundler-bin requires Bash 3.0 or greater to work.
+    $ bundle exec rake my:task
 
-Usage:
+It's something that you really ought to be doing whenever you run a ruby
+script within a bundled project, but, alas, it can become a chore. Let's be
+honest, it's a right pain in the rear.
+
+bundler-bin works out whether you should have typed "bundle exec" infront of
+common ruby commands and runs them for you.
+
+## Usage
 
  1. Copy bundler-bin.sh to ~/.bundler-bin.sh.
- 2. Set PROJECT_ROOT to the parent directory of your bundled projects
-    (e.g. ~/code). Why is this needed? See update_bundler_path in the code.
- 3. Source it from your ~/.bashrc file.
+ 2. Source it from your ~/.bashrc file.
 
 For example:
 
     $ cp bundler-bin/bundler-bin.sh ~/.bundler-bin.sh
-    $ echo "PROJECT_ROOT=$HOME/code" >> ~/.bashrc
     $ echo "[ -f ~/.bundler-bin.sh ] && source ~/.bundler-bin.sh" >> ~/.bashrc
 
-That's it... cd into a bundled project and try `echo $PATH` to see if
-it works!
+Er, that's it...
+
+You can get bundler by installing the gem:
+
+    $ gem install bundler
+
+See http://github.com/carlhuda/bundler for more on bundler.

+ 11 - 104
bundler-bin.sh

@@ -1,114 +1,21 @@
 #!/bin/bash
-#
-# bundler-bin provides a Bash function that updates your PATH environment
-# variable to include a project's ./bin directory if the project's Ruby gem
-# dependencies are being managed with bundler.
-#
-# You can find bundler here: http://github.com/wycats/bundler
-#
-# Gems that are managed by bundler will install their scripts in a directory
-# called <project-root>/bin, and you should run these commands in preference
-# to the commands that are installed with your system's gems. If your
-# project's bin directory is not in your path it's very easy to forget to
-# specify the correct command, which can cause no end of subtle problems.
-# bundler-bin fixes this problem by automatically updating your PATH as you
-# change directories. It defines a function for updating PATH via the Bash
-# PROMPT_COMMAND hook.
-#
-# Usage:
-#
-# 1. Copy bundler-bin.sh to ~/.bundler-bin.sh.
-# 2. Set PROJECT_ROOT to the parent directory of your bundled projects
-#    (e.g. $HOME/code). Why is this needed? See update_bundler_path below.
-# 3. Source it from your ~/.bashrc file.
-#
-# For example:
-#
-#  $ cp bundler-bin/bundler-bin.sh ~/.bundler-bin.sh
-#  $ echo "PROJECT_ROOT=$HOME/code" >> ~/.bashrc
-#  $ echo "[ -f ~/.bundler-bin.sh ] && source ~/.bundler-bin.sh" >> ~/.bashrc
-#
-# That's it... cd into a bundled project and try `echo $PATH` to see if
-# it works!
-#
-# Copyright (c) 2010 Graham Ashton
-# 
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-# 
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-# 
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
 
-## Variables
+COMMANDS="ruby rake"
 
-PROJECT_ROOT="${PROJECT_ROOT:-$HOME/src}"
-
-
-## Functions
-
-bundled_project_path()
+bundler-installed()
 {
-    local path="$(pwd)"
-    while [ $path != "/" ]; do
-        if [ -e "$path/Gemfile" ]; then
-            echo $path
-            return $(true)
-        fi
-        path="$(readlink -f $(dirname $path))"
-    done
-    return $(false)
+    which bundle > /dev/null
 }
 
-prepend_project_bin()
+define-bundler-aliases()
 {
-    local current_path="$1"
-    local project_path=$(bundled_project_path)
-    if [ -n "$project_path" ]; then
-        echo -n "$project_path/bin:"
+    if bundler-installed; then
+        for command in $COMMANDS; do
+            if [ -e Gemfile ]; then
+                alias $command="bundle exec $command"
+            fi
+        done
     fi
-    echo "$current_path"
-}
-
-within_project_root()
-{
-    local path="$1"
-    [[ $path =~ "^$PROJECT_ROOT" ]]
-}
-
-update_bundler_path()
-{
-    local old_ifs=$IFS
-    IFS=":"
-    local dir
-    local new_path=""
-    
-    # PATH is reconstructed every time the prompt is redrawn. Directories
-    # within PROJECT_ROOT are removed, which prevents your PATH from
-    # collecting lots of unwanted clutter as you cd between projects.
-    for dir in $PATH; do
-        if within_project_root $dir; then
-            continue
-        fi
-        if [ -z "$new_path" ]; then
-            new_path="$dir"
-        else
-            new_path="$new_path:$dir"
-        fi
-    done
-    IFS=$old_ifs
-    export PATH="$(prepend_project_bin $new_path)"
 }
 
-PROMPT_COMMAND=update_bundler_path
+PROMPT_COMMAND="define-bundler-aliases"