Skip to content

Instantly share code, notes, and snippets.

@mieko
Last active August 31, 2020 10:06
Show Gist options
  • Save mieko/7ae96382bf17cffb3eb2d862d2128d8e to your computer and use it in GitHub Desktop.
Save mieko/7ae96382bf17cffb3eb2d862d2128d8e to your computer and use it in GitHub Desktop.
Shell functions that always remember to type `./bin/rails` when you type `rails`
# Tries to find a parent directory containing a file: "$1"
find-up() {
local path=$(pwd)
while [[ -n "$path" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
try-binstub() {
local cmd="$1";
shift;
local rails_dir="$(find-up Gemfile)"
if [[ -n "$rails_dir" && -x "$rails_dir/bin/$cmd" ]]; then
"$rails_dir/bin/$cmd" "$@"
else
command "$cmd" "$@"
fi
}
rails() {
try-binstub "rails" "$@"
}
rspec() {
try-binstub "rspec" "$@"
}
rake() {
try-binstub "rake" "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment