-
-
Save bertomartin/7377841 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
class Grepr | |
def initialize(options, pattern) | |
@paths = [] | |
@pattern = pattern | |
@noregex = options.include?('n') | |
@pattern = "'#{@pattern}'" if pattern.include?(' ') | |
options.each_char do |opt| | |
case opt | |
when 'a' | |
@paths = ['app/*', 'lib/*'] | |
break | |
when 'm' | |
@paths << 'app/models/*' | |
when 'v' | |
@paths << 'app/views/*' | |
when 'c' | |
@paths << 'app/controllers/*' | |
when 'r' | |
@paths << 'app/reports/*' | |
when 'h' | |
@paths << 'app/helpers/*' | |
when 'l' | |
@paths << 'lib/*' | |
when 't' | |
@paths << 'test/*' | |
end | |
end | |
end | |
def command | |
@paths = ['app/*', 'lib/*'] if @paths.empty? | |
"grep -inr#{'F' if @noregex} --colour #{@pattern} #{@paths.join(' ')}" | |
end | |
end | |
def usage | |
puts "Grep a rails project using common options and dirs. | |
Usage: grepr [OPTIONS] PATTERN | |
Look through your rails project source for PATTERN using grep. | |
- matches case-insensitive (-i). | |
- shows line numbers in result (-n). | |
- checks recursively through dirs (-r). | |
- highlights PATTERN in result (--colour). | |
Command must be issued at the root of a rails project. | |
OPTIONS must be concatenated: grepr -mv user | |
-a will be assumed if OPTIONS are omitted. | |
PATTERN must be in quotes if it contains spaces. | |
OPTIONS | |
-n No regex. | |
Use this so special chars aren't treated as regex (eg '.'). | |
-a All folders except test (app/* and lib/*) | |
(any other options will be ignored) | |
-m app/models/* | |
-v app/views/* | |
-c app/controllers/* | |
-r app/reports/* | |
-h app/helpers/* | |
-l lib/* | |
-t test/* | |
" | |
exit | |
end | |
if ARGV[0] | |
if ARGV[0].start_with? '-' | |
opts = ARGV.shift.delete('-') | |
else | |
opts = 'a' | |
end | |
parms = ARGV.shift | |
return usage if parms.nil? | |
grepr = Grepr.new(opts, parms) | |
#puts grepr.command | |
exec grepr.command | |
else | |
return usage | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment