# -*- coding: utf-8 -*- require 'optparse' require 'milkode/findgrep/findgrep' require 'milkode/common/dbdir' require 'milkode/cdstk/cdstk' require 'milkode/cdstk/yaml_file_wrapper' require 'milkode/cdstk/package' module Milkode class CLI_Grep 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.expand_path(".")) rescue NotFoundPackage => e current_dir = File.expand_path(".") end # opt = OptionParser.new "#{File.basename($0)} [option] pattern" opt = OptionParser.new < 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.expand_path("."))) 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 private def self.setup_package(option, my_option, keyword) dirs = yaml_load.contents.find_all {|p| p.name.include? keyword }.map{|p| p.directory} raise NotFoundPackage.new keyword if (dirs.empty?) option.packages += dirs my_option[:packages] += dirs end def self.package_dir_in?(dir) yaml_load.package_root(dir) end def self.package_root_dir(dir) package_root = yaml_load.package_root(dir) if (package_root) package_root.directory else raise NotFoundPackage.new dir end end def self.yaml_load YamlFileWrapper.load(Dbdir.select_dbdir) end class ArgumentParser attr_reader :arguments attr_reader :keywords attr_reader :gotowords def initialize(arguments) @arguments = arguments @state = :line @keywords = [] @gotowords = [] end def prev @arguments.map! do |v| v.gsub("-l", ":l"). gsub("-k", ":k"). gsub("-g", ":g") end end def after result = [] @arguments.each do |v| case v when ":l" @state = :line next when ":k" @state = :keyword next when ":g" @state = :gotoline @gotowords += result result = [] next end case @state when :line result << v when :keyword @keywords << v when :gotoline @gotowords << v end end @arguments = result end end # --- error --- class NotFoundPackage < RuntimeError ; end end end