more xgrep

This commit is contained in:
rkiel
2015-06-03 21:18:36 -04:00
parent cc26c266a0
commit 3fab1dc9f4
6 changed files with 161 additions and 165 deletions

View File

@@ -1,70 +1,10 @@
#!/usr/bin/env ruby
require_relative '../lib/xgrep/commander'
require_relative '../lib/xgrep/rails_env'
commander = Commander.new
commander = Xgrep::Commander.new(ARGV)
if commander.valid?
commander.execute
else
commander.help
end
exit
ands = [ ]
ors = [ ]
nots = [ ]
current = ands
paths = %w{Rakefile app config db lib spec}
debug = false
options = %w{-E -I}
ARGV.each do |x|
case x
when "-d","--debug" then debug = true
when "-i","--ignore" then options << "--ignore-case"
when "-l","--names" then options << "--name-only"
when "--clean" then options << "--no-color"
when "--core" then paths -= ["spec"]
when "--spec" then paths = ["spec"]
when "--model" then paths = ["app/models"]
when "--view" then paths = ["app/views"]
when "--controller" then paths = ["app/controllers"]
when "--lib" then paths = ["lib"]
when "--config" then paths = ["config"]
when "--db" then paths = ["db"]
when /^-+(and|or|not)$/ then current << $1
when "and" then current = ands
when "or" then current = ors
when "not" then current = nots
else current << x
end
end
ands = ands.map { |x| "-e \"#{x}\"" }
ors = ors.map { |x| "-e \"#{x}\"" }
nots = nots.map { |x| "-e \"#{x}\"" }
if ors.size == 1
ands << ors.first
elsif ors.size > 1
ands << "\\( #{ors.join(' --or ')} \\)"
end
ands << "--not \\( #{nots.join(' --or ')} \\)" unless nots.empty?
ands = ands.join(" --and ")
command = "git grep #{options.join(' ')} #{ands} -- #{paths.sort.join(' ')}"
if debug
puts
puts command
end
puts
exec command

View File

@@ -1,11 +1,18 @@
require 'ostruct'
require 'optparse'
require_relative './custom_env'
require_relative './node_env'
require_relative './rails_env'
require_relative './simple_env'
module Xgrep
class Commander
attr_accessor :options
def initialize
def initialize (argv)
@options = OpenStruct.new
options.debug = false
options.git_grep = %w{-E}
@@ -62,8 +69,8 @@ class Commander
end
@option_parser.parse!(ARGV)
options.terms = ARGV # must be after parse!
@option_parser.parse!(argv)
options.terms = argv # must be after parse!
end
def valid?
@@ -76,7 +83,7 @@ class Commander
end
def execute
env = Object.const_get(options.environment).new
env = options.environment
env.update_pathspec(options.pathspec)
ands = []
@@ -102,21 +109,28 @@ class Commander
ands << "--not \\( #{nots.join(' --or ')} \\)" unless nots.empty?
command = "git grep #{options.git_grep.join(' ')} #{ands.join(' --and ')} -- #{env.pathspec.sort.join(' ')}"
puts
if options.debug
puts command
else
system command
end
puts
end
private
def default_environment
if File.exist?('./.xgrep')
"CustomEnv"
elsif File.exist?("Gemfile") and File.exist?("package.json")
"NodeEnv"
Xgrep::CustomEnv.new
elsif File.exist?("Gemfile")
"RailsEnv"
Xgrep::RailsEnv.new
elsif File.exist?("package.json")
"NodeEnv"
Xgrep::NodeEnv.new
else
"RailsEnv"
Xgrep::SimpleEnv.new
end
end
end
end

12
lib/xgrep/custom_env.rb Normal file
View File

@@ -0,0 +1,12 @@
module Xgrep
class CustomEnv
attr_accessor :pathspec
def initialize
@pathspec = { }
end
def update_pathspec ( pathspec )
end
end
end

12
lib/xgrep/node_env.rb Normal file
View File

@@ -0,0 +1,12 @@
module Xgrep
class NodeEnv
attr_accessor :pathspec
def initialize
@pathspec = { }
end
def update_pathspec ( pathspec )
end
end
end

View File

@@ -1,6 +1,12 @@
module Xgrep
class RailsEnv
attr_accessor :pathspec
def initialize
@pathspec = { }
end
def update_pathspec ( pathspec )
end
end
end

12
lib/xgrep/simple_env.rb Normal file
View File

@@ -0,0 +1,12 @@
module Xgrep
class SimpleEnv
attr_accessor :pathspec
def initialize
@pathspec = { }
end
def update_pathspec ( pathspec )
end
end
end