Class: Milkode::CLI_Grep
- Inherits:
-
Object
- Object
- Milkode::CLI_Grep
- Defined in:
- lib/milkode/grep/cli_grep.rb
Defined Under Namespace
Classes: ArgumentParser, NotFoundPackage
Class Method Summary collapse
Class Method Details
.execute(stdout, arguments = []) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/milkode/grep/cli_grep.rb', line 12 def self.execute(stdout, arguments=[]) option = FindGrep::FindGrep::DEFAULT_OPTION.dup # default option option.dbFile = Dbdir.groonga_path(Dbdir.default_dir) option.isSilent = true # local option my_option = {} my_option[:packages] = [] begin current_dir = package_root_dir(File.(".")) rescue NotFoundPackage => e current_dir = File.(".") end # opt = OptionParser.new "#{File.basename($0)} [option] pattern" opt = OptionParser.new "gmilk [option] pattern\ngmilk is 'milk grep'.\n\nStateful:\n-l, Change state 'line'. (Match line words.)\n-k, Change state 'keyword'. (Match file-content or file-path.)\nFirst state is 'line'.\nExample:\n gmilk line1 line2 -k keyword1 keyword2 -l line3 -k keyword3 ...\n\nGotoline:\n-g, Go to line mode.\nEnter a file name and line number. If you omit the line number jumps to the line:1.\nExample:\n gmilk -g database lib 7\n lib/database.rb:7:xxxxxxxxxxxxxxx\n database_lib.rb:7:yyyyyyyyyyyyyyy\n\n gmilk -g lib/database.rb:7 test/test_database.rb:5\n lib/database.rb:7:xxxxxxxxxxxxxxx\n test/test_database.rb:5:yyyyyyyyy\n\nNormal:\n" opt.on('-a', '--all', 'Search all package.') {|v| my_option[:all] = true } opt.on('-c', '--count', 'Disp count num.') {|v| my_option[:count] = true } opt.on('--cache', 'Search only db.') {|v| option.groongaOnly = true } opt.on('--color', 'Color highlight.') {|v| option.colorHighlight = true} opt.on('--cs', '--case-sensitive', 'Case sensitivity.') {|v| my_option[:case_sensitive] = true } opt.on('-d DIR', '--directory DIR', 'Start directory. (deafult:".")') {|v| current_dir = File.(v); my_option[:find_mode] = true} opt.on('-f FILE_PATH', '--file-path FILE_PATH', 'File path. (Enable multiple call)') {|v| option.filePatterns << v; my_option[:find_mode] = true } opt.on('-i', '--ignore', 'Ignore case.') {|v| option.ignoreCase = true} opt.on('-n NUM', 'Limits the number of match to show.') {|v| option.matchCountLimit = v.to_i } opt.on('--no-snip', 'There being a long line, it does not snip.') {|v| option.noSnip = true } opt.on('-p PACKAGE', '--package PACKAGE', 'Specify search package.') {|v| setup_package(option, my_option, v) } opt.on('-r', '--root', 'Search from package root.') {|v| current_dir = package_root_dir(File.(".")); my_option[:find_mode] = true } opt.on('-s SUFFIX', '--suffix SUFFIX', 'Suffix.') {|v| option.suffixs << v; my_option[:find_mode] = true } opt.on('-u', '--update', 'With update db.') {|v| my_option[:update] = true } opt.on('--verbose', 'Set the verbose level of output.') {|v| option.isSilent = false } begin ap = ArgumentParser.new arguments ap.prev opt.parse!(arguments) ap.after arguments = ap.arguments option.keywords = ap.keywords my_option[:find_mode] = true unless ap.keywords.empty? unless ap.gotowords.empty? my_option[:find_mode] = true my_option[:gotoline_data] = Util::parse_gotoline(ap.gotowords) end # p ap.arguments # p ap.keywords # p ap.gotowords rescue NotFoundPackage => e stdout.puts "fatal: Not found package '#{e}'." return end if option.packages.empty? && !my_option[:all] if (package_dir_in? current_dir) option.filePatterns << current_dir else stdout.puts "fatal: Not package dir '#{current_dir}'." return end end if (arguments.size > 0 || my_option[:find_mode]) # ignore? downcase_all = arguments.all? {|v| Util::downcase? v} option.ignoreCase = true if downcase_all && !my_option[:case_sensitive] # update if my_option[:update] cdstk = Cdstk.new(stdout, Dbdir.select_dbdir) if (my_option[:all]) cdstk.update_all elsif (my_option[:packages].empty?) cdstk.update_for_grep(package_root_dir(File.("."))) else my_option[:packages].each do |v| cdstk.update_for_grep(v) end end stdout.puts end if (my_option[:count]) # count mode option.isSilent = true findGrep = FindGrep::FindGrep.new(arguments, option) records = findGrep.pickupRecords # stdout.puts "#{records.size} records (#{findGrep.time_s})" stdout.puts "#{records.size} records" elsif (my_option[:gotoline_data]) # gotoline mode basePatterns = option.filePatterns my_option[:gotoline_data].each do |v| option.filePatterns = basePatterns + v[0] option.gotoline = v[1] findGrep = FindGrep::FindGrep.new(arguments, option) findGrep.searchAndPrint(stdout) end else # search mode findGrep = FindGrep::FindGrep.new(arguments, option) findGrep.searchAndPrint(stdout) end else stdout.print opt.help end end |