Go back to the main page

Search your Rails code

 

Installation:

Copy the contents of the below fart.sh to your /bin/fart.sh and do a chmod +x /bin/fart.sh

Usage:

  • cd /var/www/project_code# Important: Only searches current directory so you must switch into it
  • fart.sh "def screenshot"
  • fart.sh "def screenshot" -d "./vendor ./app" # -d switch: just look into directories vendor and app. Need quotes for multiple directories
  • fart.sh list_tag_history -f "html.erb"# -f switch: just search .html.erb files
  • fart.sh "function open_window" -f js -d ./public# this will only search javascript files in the public directory
  • fart.sh "function open_window" "js|erb"# this will only search javascript files and .erb files
  • fart.sh "func.*open_"# pass in a regex
# contents of fart.sh
OPTIND=2
while getopts "d:f:" OPTION
do
  case $OPTION in
    d) dirs=$OPTARG;;
    f) files=$OPTARG;;
  esac
done
[ -e ${files+isset} ] && files="rb|rhtml|html|erb|css|js|rjs|rake"
[ -e ${dirs+isset} ] && dirs="app/ public/ lib/ script/ config/"
file_regex="^.*\.($files)$"
cmd=$( cat <<EOT
find $dirs \
-regextype 'posix-egrep' -regex '$file_regex' \
-not -name "firebug-lite-compressed.js" -not -name "AJS.*" \
-exec grep -Hn "$1" {} \;
EOT
)

eval $cmd

The above code is a simple and fast find script for posix systems. It is fast because you determine which file types or not to search through. Just using a find . -name "." exec grep type of search is quite slow in my experience. You can hack this code easily to search PHP projects for example, (or Python, Perl, whatever ) by editing the files="rb|rhtml|html|erb|css|js|rjs|rake" and dirs="app/ public/ lib/ script/ config/" lines to fit your project needs.

You will have to change the "-not -name" line to fit your personal project needs. This line is for excluding searching certain files. For example in the above code I don't want to search files that start with "AJS" or Firebug.

This code is lovingly named after the fart utility.

  • Pushed on 10/21/2010 by Christian