grep-fu 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/README.md CHANGED
@@ -132,6 +132,17 @@ to
132
132
 
133
133
  And migrations will no longer be searched. Note that targeted searches and specified directories always override the PRUNE_PATHS option.
134
134
 
135
+ Version Information
136
+ -------------------
137
+
138
+ Passing the '--version' flag to grep-fu will return the current version.
139
+
140
+ grep-fu --version
141
+
142
+ returns
143
+
144
+ grep-fu 0.5.0
145
+
135
146
  Technical mumbo-jumbo
136
147
  ---------------------
137
148
 
@@ -147,7 +158,7 @@ Grep-Fu speeds up the searching process by only searching the files you care abo
147
158
 
148
159
  Note that all these directories are still searchable with targeted searches or a directory specification; they're simply not searched by default
149
160
 
150
- Grep-fu also ignores files larger than 100K, which tend to be unsearchable binaries.
161
+ Grep-fu also ignores files larger than 100K, which tend to be unsearchable binaries or SQL dumps.
151
162
 
152
163
  The pruning options for find are some of the ugliest in the CLI world. Using Ruby allows us to construct a giant, hideous command that does exactly what we need.
153
164
 
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'spec/rake/spectask'
3
4
 
4
5
  begin
5
6
  require 'jeweler'
@@ -36,7 +37,12 @@ rescue LoadError
36
37
  end
37
38
  end
38
39
 
39
- task :test => :check_dependencies
40
+ desc "Run all specs"
41
+ Spec::Rake::SpecTask.new('run_specs') do |t|
42
+ t.spec_files = FileList['spec/**/*.rb']
43
+ end
44
+
45
+ task :test => [:check_dependencies, :run_specs]
40
46
 
41
47
  task :default => :test
42
48
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
data/bin/grep-fu CHANGED
@@ -1,61 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
-
3
- PATH_REPLACEMENTS = {
4
- 'a' => 'app',
5
- 'c' => 'app/controllers',
6
- 'h' => 'app/helpers',
7
- 'm' => 'app/models',
8
- 'v' => 'app/views',
9
- 'l' => 'lib',
10
- 'p' => 'public',
11
- 'css' => 'public/stylesheets',
12
- 'js' => 'public/javascripts',
13
- 't' => 'test',
14
- 's' => 'spec',
15
- 'vp' => 'vendor/plugins',
16
- 'mig' => 'db/migrate'
17
- }
18
-
19
- PRUNE_PATHS = ['/.svn', '/.git', '/vendor', '/log', '/public', '/tmp', '/coverage']
20
- COLOR_ON_BY_DEFAULT = false
21
- options = '-ril'
22
-
23
- unless ARGV.size > 0
24
- puts "\nUsage: #{__FILE__} [findpath] search_string [--verbose|--single-line]\n
25
- Where findpath is one of the following:
26
- any literal subdirectory
27
- #{PATH_REPLACEMENTS.map { |abbr, txt| " #{abbr} - #{txt}" }.join("\n")}\n\n"
28
- exit
29
- end
30
-
31
- clargs = ARGV
32
-
33
- verbose = (clargs -= ['--verbose'] if clargs.include?('--verbose'))
34
- single_line = (clargs -= ['--single-line'] if clargs.include?('--single-line'))
35
- color = (clargs -= ['--color'] if (clargs.include?('--color')) || COLOR_ON_BY_DEFAULT)
36
-
37
- if clargs.include?('--no-color')
38
- clargs -= ['--no-color']
39
- color = false
40
- end
41
-
42
- options = '-rin' if verbose
43
- options << ' --color=always' if color
44
-
45
- search_criteria = clargs.last
46
- file_path = clargs.size == 2 ? PATH_REPLACEMENTS[clargs.first] || clargs.first : './'
47
- delicious_prunes = PRUNE_PATHS ? "-path '*#{PRUNE_PATHS.join("' -prune -o -path *'")}' -prune -o" : ''
48
-
49
- find_command = "find #{file_path} #{delicious_prunes} \\( -size -100000c -type f \\) -print0 | xargs -0 grep #{options} \"#{search_criteria}\""
50
-
51
- if verbose
52
- `#{find_command}`.each_line do |found|
53
- file_and_line = found.slice!(/^.*?:.*?:/)
54
- puts "#{file_and_line}\n\t#{found.strip}"
55
- end
56
- elsif single_line
57
- puts `#{find_command}`.map { |found| found.chomp }.join(' ')
58
- else
59
- puts `#{find_command}`
60
- end
61
-
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require 'grep-fu'
4
+
5
+ GrepFu::run!(ARGV)
data/grep-fu.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{grep-fu}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Eric Budd"]
12
- s.date = %q{2010-05-27}
12
+ s.date = %q{2010-06-16}
13
13
  s.default_executable = %q{grep-fu}
14
14
  s.description = %q{Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.}
15
15
  s.email = %q{calamitous@calamitylane.com}
@@ -19,18 +19,32 @@ Gem::Specification.new do |s|
19
19
  "README.md"
20
20
  ]
21
21
  s.files = [
22
- "LICENSE",
22
+ ".gitignore",
23
+ "LICENSE",
23
24
  "README.md",
24
25
  "Rakefile",
25
26
  "VERSION",
26
27
  "bin/grep-fu",
27
- "grep-fu.gemspec"
28
+ "grep-fu.gemspec",
29
+ "lib/grep-fu.rb",
30
+ "lib/grep-fu/find_builder.rb",
31
+ "lib/grep-fu/options.rb",
32
+ "spec/find_builder_spec.rb",
33
+ "spec/grep_fu_spec.rb",
34
+ "spec/options_spec.rb",
35
+ "spec/spec_helper.rb"
28
36
  ]
29
37
  s.homepage = %q{http://github.com/Calamitous/grep-fu}
30
38
  s.rdoc_options = ["--charset=UTF-8"]
31
39
  s.require_paths = ["lib"]
32
40
  s.rubygems_version = %q{1.3.6}
33
41
  s.summary = %q{Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.}
42
+ s.test_files = [
43
+ "spec/find_builder_spec.rb",
44
+ "spec/options_spec.rb",
45
+ "spec/spec_helper.rb",
46
+ "spec/grep_fu_spec.rb"
47
+ ]
34
48
 
35
49
  if s.respond_to? :specification_version then
36
50
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -0,0 +1,20 @@
1
+ module GrepFu
2
+ class FindBuilder
3
+ PRUNE_PATHS = ['/.svn', '/.git', '/vendor', '/log', '/public', '/tmp', '/coverage']
4
+
5
+ def self.delicious_prunes
6
+ "-path '*#{PRUNE_PATHS.join("' -prune -o -path '*")}' -prune -o"
7
+ end
8
+
9
+ def self.find_command(options)
10
+ [
11
+ 'find',
12
+ options.file_path,
13
+ delicious_prunes,
14
+ "\\( -size -100000c -type f \\) -print0 | xargs -0 grep",
15
+ options.to_s,
16
+ "\"#{options.search_criteria}\""
17
+ ].join(' ')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,61 @@
1
+ module GrepFu
2
+ class Options
3
+ attr_reader :verbose, :single_line, :color, :search_criteria, :file_path
4
+
5
+ PATH_REPLACEMENTS = {
6
+ 'a' => 'app',
7
+ 'c' => 'app/controllers',
8
+ 'h' => 'app/helpers',
9
+ 'm' => 'app/models',
10
+ 'v' => 'app/views',
11
+ 'l' => 'lib',
12
+ 'p' => 'public',
13
+ 'css' => 'public/stylesheets',
14
+ 'js' => 'public/javascripts',
15
+ 't' => 'test',
16
+ 's' => 'spec',
17
+ 'vp' => 'vendor/plugins',
18
+ 'mig' => 'db/migrate'
19
+ }
20
+
21
+ COLOR_ON_BY_DEFAULT = false
22
+
23
+ def self.usage(file)
24
+ lines = ['',
25
+ "Usage: #{file} [findpath] search_string [--verbose|--single-line]\n",
26
+ " Where findpath is one of the following:",
27
+ " any literal subdirectory",
28
+ Options::PATH_REPLACEMENTS.map { |abbr, txt| " #{abbr} - #{txt}" }.join("\n"),
29
+ "\n\n"
30
+ ]
31
+
32
+ lines.join("\n")
33
+ end
34
+
35
+ def initialize(args)
36
+ if args.include?('--version')
37
+ puts "grep-fu #{File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))}"
38
+ exit(0)
39
+ end
40
+
41
+ @verbose = (args -= ['--verbose'] if args.include?('--verbose'))
42
+ @single_line = (args -= ['--single-line'] if args.include?('--single-line'))
43
+
44
+ @color = (args -= ['--color'] if (args.include?('--color')) || COLOR_ON_BY_DEFAULT)
45
+ if args.include?('--no-color')
46
+ args -= ['--no-color']
47
+ @color = false
48
+ end
49
+
50
+ @search_criteria = args.last
51
+ @file_path = args.size == 2 ? PATH_REPLACEMENTS[args.first] || args.first : './'
52
+ end
53
+
54
+ def to_s
55
+ options = @verbose ? '-rin' : '-ril'
56
+ options << ' --color=always' if @color
57
+ options
58
+ end
59
+
60
+ end
61
+ end
data/lib/grep-fu.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'grep-fu/options'
2
+ require 'grep-fu/find_builder'
3
+
4
+ module GrepFu
5
+ def self.run!(args = [])
6
+ unless args.size > 0
7
+ puts Options.usage(__FILE__)
8
+ return
9
+ end
10
+
11
+ options = Options.new(args)
12
+
13
+ find_command = FindBuilder.find_command(options)
14
+
15
+ if options.verbose
16
+ `#{find_command}`.each_line do |found|
17
+ file_and_line = found.slice!(/^.*?:.*?:/)
18
+ puts "#{file_and_line}\n\t#{found.strip}"
19
+ end
20
+ elsif options.single_line
21
+ puts `#{find_command}`.map { |found| found.chomp }.join(' ')
22
+ else
23
+ puts `#{find_command}`
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'grep-fu/find_builder'
2
+ require 'spec/spec_helper'
3
+
4
+ describe GrepFu::FindBuilder do
5
+ describe "#delicious_prunes" do
6
+ it "should create command-line options for pruning path" do
7
+ silence_warnings { GrepFu::FindBuilder::PRUNE_PATHS = ['/pruney'] }
8
+ GrepFu::FindBuilder.delicious_prunes.should == "-path '*/pruney' -prune -o"
9
+ end
10
+
11
+ it "should create command-line options for multiple pruning paths" do
12
+ silence_warnings { GrepFu::FindBuilder::PRUNE_PATHS = ['/pruney', '/apply'] }
13
+ GrepFu::FindBuilder.delicious_prunes.should == "-path '*/pruney' -prune -o -path '*/apply' -prune -o"
14
+ end
15
+ end
16
+
17
+ describe "#find_command" do
18
+ before(:each) do
19
+ GrepFu::FindBuilder.stub!(:delicious_prunes).and_return('prune_list')
20
+ @options = mock(Object,
21
+ :file_path => 'path',
22
+ :to_s => 'str_opts',
23
+ :search_criteria => 'crits')
24
+ @find_command = GrepFu::FindBuilder.find_command(@options)
25
+ end
26
+
27
+ it "should start with the find command" do
28
+ @find_command.should =~ /^find\s/
29
+ end
30
+
31
+ it "should contain the file path" do
32
+ @find_command.should =~ /\spath\s/
33
+ end
34
+
35
+ it "should contain all the pruned directories" do
36
+ @find_command.should =~ /\sprune_list\s/
37
+ end
38
+
39
+ it "should contain a blob of bash muck" do
40
+ @find_command.should =~ /\s.. -size -100000c -type f .. -print0 | xargs -0 grep\s/
41
+ end
42
+
43
+ it "should contain the options" do
44
+ @find_command.should =~ /\sstr_opts\s/
45
+ end
46
+
47
+ it "should end with the quoted search criteria" do
48
+ @find_command.should =~ /\s"crits"$/
49
+ end
50
+
51
+ it "should stitch together a long, long find command" do
52
+ @find_command.should =~ /find path prune_list .. -size -100000c -type f .. -print0 | xargs -0 grep str_opts \"crits\"/
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
@@ -0,0 +1,76 @@
1
+ require 'grep-fu'
2
+ require 'spec/spec_helper'
3
+
4
+ describe GrepFu do
5
+ describe "#run!" do
6
+ before(:each) do
7
+ GrepFu.stub!(:puts)
8
+ end
9
+
10
+ describe "with no arguments provided" do
11
+ it "should print the usage and exit if no arguments are provided" do
12
+ GrepFu::Options.should_receive(:usage).and_return('Usage: etc.')
13
+ GrepFu.should_receive(:puts).with('Usage: etc.')
14
+ GrepFu.run!
15
+ end
16
+
17
+ it "should exit early if no arguments are provided" do
18
+ GrepFu::Options.should_not_receive(:new)
19
+ GrepFu.run!
20
+ end
21
+ end
22
+
23
+ describe "with valid arguments" do
24
+ before(:each) do
25
+ @options = mock(GrepFu::Options, :verbose => false, :single_line => false)
26
+ @find_command = "find me"
27
+ GrepFu::Options.stub!(:new).and_return(@options)
28
+ GrepFu::FindBuilder.stub!(:find_command).and_return(@find_command)
29
+ GrepFu.stub!(:'`')
30
+ end
31
+
32
+ it "should create options from provided arguments" do
33
+ GrepFu::Options.should_receive(:new).with('arguments!')
34
+ GrepFu.run!('arguments!')
35
+ end
36
+
37
+ it "should build a find command from options" do
38
+ GrepFu::FindBuilder.should_receive(:find_command).with(@options)
39
+ GrepFu.run!('arguments!')
40
+ end
41
+
42
+ it "should display the results of the find" do
43
+ GrepFu.should_receive(:'`').with(@find_command)
44
+ GrepFu.run!(['stuff_to_find'])
45
+ end
46
+
47
+ it "should appropriately format results for single-line requests" do
48
+ @options.stub!(:single_line).and_return(true)
49
+ GrepFu.stub!(:'`').and_return("line one\nline two\nlast line\n")
50
+ GrepFu.should_receive(:puts).with('line one line two last line')
51
+ GrepFu.run!(['--single-line'])
52
+ end
53
+
54
+ it "should appropriately format results for verbose requests" do
55
+ @options.stub!(:verbose).and_return(true)
56
+ verbose_returns = [
57
+ "app/views/mailer/new_notification.text.plain.erb:1:A new notification has arrived!",
58
+ "README:45: although it's not clear who might have",
59
+ "app/models/funge.rb:22: Hugenot.new"
60
+ ].join("\n")
61
+
62
+ formatted_returns = [
63
+ "app/views/mailer/new_notification.text.plain.erb:1:\n\tA new notification has arrived!",
64
+ "README:45:\n\talthough it's not clear who might have",
65
+ "app/models/funge.rb:22:\n\tHugenot.new"
66
+ ]
67
+
68
+ GrepFu.stub!(:'`').and_return(verbose_returns)
69
+ GrepFu.should_receive(:puts).with(formatted_returns[0])
70
+ GrepFu.should_receive(:puts).with(formatted_returns[1])
71
+ GrepFu.should_receive(:puts).with(formatted_returns[2])
72
+ GrepFu.run!(['not', '--verbose'])
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,55 @@
1
+ require 'grep-fu/options'
2
+ require 'spec/spec_helper'
3
+
4
+ describe GrepFu::Options do
5
+ it "should instantiate correctly" do
6
+ GrepFu::Options.new([])
7
+ end
8
+
9
+ it "should set the verbose option to true, if provided" do
10
+ GrepFu::Options.new(['--verbose']).verbose.should be_true
11
+ end
12
+
13
+ it "should set the single-line option to true, if provided" do
14
+ GrepFu::Options.new(['--single-line']).single_line.should be_true
15
+ end
16
+
17
+ it "should set the color option to true, if provided" do
18
+ GrepFu::Options.new(['--color']).color.should be_true
19
+ end
20
+
21
+ it "should default to no color" do
22
+ GrepFu::Options.new([]).color.should be_false
23
+ end
24
+
25
+ it "should allow color to be disabled from the command line" do
26
+ GrepFu::Options.new(['--no_color']).color.should be_false
27
+ end
28
+
29
+ it "should change color default based on constant" do
30
+ silence_warnings { GrepFu::Options::COLOR_ON_BY_DEFAULT = true }
31
+ GrepFu::Options.new([]).color.should be_true
32
+ end
33
+
34
+ it "should allow color to be disabled from the command line when default is color=true" do
35
+ silence_warnings { GrepFu::Options::COLOR_ON_BY_DEFAULT = true }
36
+ GrepFu::Options.new(['--no-color']).color.should be_false
37
+ end
38
+
39
+ it "should set the search criteria to the last non-option flag in the list" do
40
+ GrepFu::Options.new(%w{offer}).search_criteria.should == 'offer'
41
+ GrepFu::Options.new(%w{a help --color}).search_criteria.should == 'help'
42
+ GrepFu::Options.new(%w{lib/tasks unrake --verbose --color}).search_criteria.should == 'unrake'
43
+ end
44
+
45
+ it "should expand the file_path to one of the replacement variables, if found" do
46
+ GrepFu::Options.new(%w{a value}).file_path.should == 'app'
47
+ GrepFu::Options.new(%w{s hoozits}).file_path.should == 'spec'
48
+ GrepFu::Options.new(%w{mig down --color}).file_path.should == 'db/migrate'
49
+ end
50
+
51
+ it "should accept an explicit find path, if provided" do
52
+ GrepFu::Options.new(%w{lib/tasks value}).file_path.should == 'lib/tasks'
53
+ end
54
+
55
+ end
@@ -0,0 +1,6 @@
1
+ def silence_warnings
2
+ old_verbose, $VERBOSE = $VERBOSE, nil
3
+ yield
4
+ ensure
5
+ $VERBOSE = old_verbose
6
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
7
+ - 5
8
8
  - 0
9
- version: 0.4.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Eric Budd
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-27 00:00:00 -05:00
17
+ date: 2010-06-16 00:00:00 -05:00
18
18
  default_executable: grep-fu
19
19
  dependencies: []
20
20
 
@@ -28,12 +28,20 @@ extra_rdoc_files:
28
28
  - LICENSE
29
29
  - README.md
30
30
  files:
31
+ - .gitignore
31
32
  - LICENSE
32
33
  - README.md
33
34
  - Rakefile
34
35
  - VERSION
35
36
  - bin/grep-fu
36
37
  - grep-fu.gemspec
38
+ - lib/grep-fu.rb
39
+ - lib/grep-fu/find_builder.rb
40
+ - lib/grep-fu/options.rb
41
+ - spec/find_builder_spec.rb
42
+ - spec/grep_fu_spec.rb
43
+ - spec/options_spec.rb
44
+ - spec/spec_helper.rb
37
45
  has_rdoc: true
38
46
  homepage: http://github.com/Calamitous/grep-fu
39
47
  licenses: []
@@ -64,5 +72,8 @@ rubygems_version: 1.3.6
64
72
  signing_key:
65
73
  specification_version: 3
66
74
  summary: Grep-Fu is a very fast, Rails-oriented command-line helper script for grep.
67
- test_files: []
68
-
75
+ test_files:
76
+ - spec/find_builder_spec.rb
77
+ - spec/options_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/grep_fu_spec.rb