more xgrep
This commit is contained in:
62
bin/xgrep
62
bin/xgrep
@@ -1,70 +1,10 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
require_relative '../lib/xgrep/commander'
|
require_relative '../lib/xgrep/commander'
|
||||||
require_relative '../lib/xgrep/rails_env'
|
|
||||||
|
|
||||||
|
commander = Xgrep::Commander.new(ARGV)
|
||||||
commander = Commander.new
|
|
||||||
if commander.valid?
|
if commander.valid?
|
||||||
commander.execute
|
commander.execute
|
||||||
else
|
else
|
||||||
commander.help
|
commander.help
|
||||||
end
|
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
|
|
||||||
|
|||||||
@@ -1,122 +1,136 @@
|
|||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
|
||||||
class Commander
|
require_relative './custom_env'
|
||||||
|
require_relative './node_env'
|
||||||
|
require_relative './rails_env'
|
||||||
|
require_relative './simple_env'
|
||||||
|
|
||||||
attr_accessor :options
|
module Xgrep
|
||||||
|
|
||||||
def initialize
|
class Commander
|
||||||
@options = OpenStruct.new
|
|
||||||
options.debug = false
|
|
||||||
options.git_grep = %w{-E}
|
|
||||||
options.pathspec = []
|
|
||||||
options.environment = default_environment
|
|
||||||
|
|
||||||
@option_parser = OptionParser.new do |op|
|
attr_accessor :options
|
||||||
op.banner = "Usage: xgrep options term(s)"
|
|
||||||
|
def initialize (argv)
|
||||||
|
@options = OpenStruct.new
|
||||||
|
options.debug = false
|
||||||
|
options.git_grep = %w{-E}
|
||||||
|
options.pathspec = []
|
||||||
|
options.environment = default_environment
|
||||||
|
|
||||||
|
@option_parser = OptionParser.new do |op|
|
||||||
|
op.banner = "Usage: xgrep options term(s)"
|
||||||
|
|
||||||
|
op.on('-a','--asset') do |argument|
|
||||||
|
options.pathspec << :asset
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-b','--db') do |argument|
|
||||||
|
options.pathspec << :db
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-c','--controller') do |argument|
|
||||||
|
options.pathspec << :controller
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-d','--[no-]debug') do |argument|
|
||||||
|
options.debug = argument
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-f','--file') do |argument|
|
||||||
|
options.git_grep << "-L"
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-g','--config') do |argument|
|
||||||
|
options.pathspec << :config
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-i','--invert') do |argument|
|
||||||
|
options.git_grep << '-v'
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-l','--lib') do |argument|
|
||||||
|
options.pathspec << :lib
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-m','--model') do |argument|
|
||||||
|
options.pathspec << :model
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on('-o','--core') do |argument|
|
||||||
|
options.pathspec << :core
|
||||||
|
end
|
||||||
|
|
||||||
|
op.on_tail('-h','--help') do |argument|
|
||||||
|
puts op
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
op.on('-a','--asset') do |argument|
|
|
||||||
options.pathspec << :asset
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-b','--db') do |argument|
|
|
||||||
options.pathspec << :db
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-c','--controller') do |argument|
|
|
||||||
options.pathspec << :controller
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-d','--[no-]debug') do |argument|
|
|
||||||
options.debug = argument
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-f','--file') do |argument|
|
|
||||||
options.git_grep << "-L"
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-g','--config') do |argument|
|
|
||||||
options.pathspec << :config
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-i','--invert') do |argument|
|
|
||||||
options.git_grep << '-v'
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-l','--lib') do |argument|
|
|
||||||
options.pathspec << :lib
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-m','--model') do |argument|
|
|
||||||
options.pathspec << :model
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on('-o','--core') do |argument|
|
|
||||||
options.pathspec << :core
|
|
||||||
end
|
|
||||||
|
|
||||||
op.on_tail('-h','--help') do |argument|
|
|
||||||
puts op
|
|
||||||
exit
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@option_parser.parse!(argv)
|
||||||
|
options.terms = argv # must be after parse!
|
||||||
end
|
end
|
||||||
|
|
||||||
@option_parser.parse!(ARGV)
|
def valid?
|
||||||
options.terms = ARGV # must be after parse!
|
options.terms.size > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid?
|
def help
|
||||||
options.terms.size > 0
|
puts @option_parser
|
||||||
end
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
def help
|
def execute
|
||||||
puts @option_parser
|
env = options.environment
|
||||||
exit
|
env.update_pathspec(options.pathspec)
|
||||||
end
|
|
||||||
|
|
||||||
def execute
|
ands = []
|
||||||
env = Object.const_get(options.environment).new
|
ors = []
|
||||||
env.update_pathspec(options.pathspec)
|
nots = []
|
||||||
|
current_op = ands
|
||||||
|
|
||||||
ands = []
|
options.terms.each do |x|
|
||||||
ors = []
|
case x
|
||||||
nots = []
|
when 'and' then current_op = ands
|
||||||
current_op = ands
|
when 'or' then current_op = ors
|
||||||
|
when 'not' then current_op = nots
|
||||||
|
when /^-+(and|or|not)$/i then current_op << $1
|
||||||
|
else current_op << x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
options.terms.each do |x|
|
ands = ands.map { |x| "-e \"#{x}\"" }
|
||||||
case x
|
ors = ors.map { |x| "-e \"#{x}\"" }
|
||||||
when 'and' then current_op = ands
|
nots = nots.map { |x| "-e \"#{x}\"" }
|
||||||
when 'or' then current_op = ors
|
|
||||||
when 'not' then current_op = nots
|
ands << "\\( #{ors.join(' --or ')} \\)" unless ors.empty?
|
||||||
when /^-+(and|or|not)$/i then current_op << $1
|
ands << "--not \\( #{nots.join(' --or ')} \\)" unless nots.empty?
|
||||||
else current_op << x
|
|
||||||
|
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')
|
||||||
|
Xgrep::CustomEnv.new
|
||||||
|
elsif File.exist?("Gemfile")
|
||||||
|
Xgrep::RailsEnv.new
|
||||||
|
elsif File.exist?("package.json")
|
||||||
|
Xgrep::NodeEnv.new
|
||||||
|
else
|
||||||
|
Xgrep::SimpleEnv.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ands = ands.map { |x| "-e \"#{x}\"" }
|
|
||||||
ors = ors.map { |x| "-e \"#{x}\"" }
|
|
||||||
nots = nots.map { |x| "-e \"#{x}\"" }
|
|
||||||
|
|
||||||
ands << "\\( #{ors.join(' --or ')} \\)" unless ors.empty?
|
|
||||||
ands << "--not \\( #{nots.join(' --or ')} \\)" unless nots.empty?
|
|
||||||
|
|
||||||
command = "git grep #{options.git_grep.join(' ')} #{ands.join(' --and ')} -- #{env.pathspec.sort.join(' ')}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def default_environment
|
|
||||||
if File.exist?('./.xgrep')
|
|
||||||
"CustomEnv"
|
|
||||||
elsif File.exist?("Gemfile") and File.exist?("package.json")
|
|
||||||
"NodeEnv"
|
|
||||||
elsif File.exist?("Gemfile")
|
|
||||||
"RailsEnv"
|
|
||||||
elsif File.exist?("package.json")
|
|
||||||
"NodeEnv"
|
|
||||||
else
|
|
||||||
"RailsEnv"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
12
lib/xgrep/custom_env.rb
Normal file
12
lib/xgrep/custom_env.rb
Normal 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
12
lib/xgrep/node_env.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module Xgrep
|
||||||
|
class NodeEnv
|
||||||
|
attr_accessor :pathspec
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@pathspec = { }
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_pathspec ( pathspec )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
class RailsEnv
|
module Xgrep
|
||||||
|
class RailsEnv
|
||||||
|
attr_accessor :pathspec
|
||||||
|
|
||||||
def update_pathspec ( pathspec )
|
def initialize
|
||||||
|
@pathspec = { }
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_pathspec ( pathspec )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
12
lib/xgrep/simple_env.rb
Normal file
12
lib/xgrep/simple_env.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module Xgrep
|
||||||
|
class SimpleEnv
|
||||||
|
attr_accessor :pathspec
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@pathspec = { }
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_pathspec ( pathspec )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user