Created
September 10, 2012 09:27
-
-
Save terlar/3689896 to your computer and use it in GitHub Desktop.
Gemfile tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Method for finding a file within a project | |
_file-within-project() { | |
local file=$1 | |
local check_dir=$PWD | |
while [ $check_dir != "/" ]; do | |
if [ -f "$check_dir/$file" ]; then | |
echo "$check_dir/$file" | |
return true | |
fi | |
check_dir="$(dirname $check_dir)" | |
done | |
false | |
} | |
# Method for doing various Gemfile operations: | |
# - Output currently used local Gemfile | |
# - "Unlock" local Gemfile | |
# - Turn off local Gemfile | |
# - Set local Gemfile, both with relative and absolute path | |
bundle_gemfile() { | |
if [ $# -eq 0 ]; then | |
if [ $BUNDLE_GEMFILE ]; then | |
echo "Using Bundle Gemfile ($BUNDLE_GEMFILE)" | |
else | |
echo "Bundle Gemfile not set" | |
fi | |
else | |
case $1 in | |
"unlock") | |
[ $BUNDLE_GEMFILE ] && cp "Gemfile.lock" "$BUNDLE_GEMFILE.lock";; | |
"off") | |
unset BUNDLE_GEMFILE;; | |
/*) | |
[ -f "$1" ] && export BUNDLE_GEMFILE=$1;; | |
*) | |
[ -f "$1" ] && export BUNDLE_GEMFILE="$PWD/$1";; | |
esac | |
fi | |
} | |
# Quicker alias and ignore pattern for better autocompletion | |
alias bgem='bundle_gemfile' | |
zstyle ':completion:*:bundle_gemfile:*' ignored-patterns '(*/)#Gemfile' '(*/)#*.lock' | |
# Unlock local Gemfile and run bundle | |
unlocked_bundle() { | |
bundle_gemfile unlock | |
if [ $# -eq 0 ]; then | |
bundle | |
else | |
bundle $* | |
fi | |
} | |
# Quicker alias and keep bundle autocompletion | |
alias b='unlocked_bundle' | |
compdef _bundle unlocked_bundle=bundle | |
# Toggle local Gemfile when entering and exiting projects with local Gemfile | |
_toggle-local-gemfile() { | |
local gemfile="Gemfile.local" | |
local gemfile_path=$(_file-within-project $gemfile) | |
if [ "$gemfile_path" != "" ]; then | |
[ $BUNDLE_GEMFILE ] && return | |
bundle_gemfile $gemfile_path | |
else | |
bundle_gemfile off | |
fi | |
} | |
chpwd_functions+=(_toggle-local-gemfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment