Skip to content
Browse files

CliGrepをYamlFileWrapperに置き換え。

CdstkYamlを削除完了。
  • Loading branch information...
1 parent 4ce53b8 commit 615e3442a9d096873842a6ced4374103f8640ec7 @ongaeshi committed
Showing with 13 additions and 392 deletions.
  1. +3 −3 lib/milkode/cdstk/cdstk.rb
  2. +0 −177 lib/milkode/cdstk/cdstk_yaml.rb
  3. +10 −9 lib/milkode/grep/cli_grep.rb
  4. +0 −203 test/test_cdstk_yaml.rb
View
6 lib/milkode/cdstk/cdstk.rb
@@ -6,7 +6,6 @@
require 'groonga'
require 'fileutils'
require 'pathname'
-require 'milkode/cdstk/cdstk_yaml'
require 'milkode/common/grenfiletest'
require 'milkode/common/util'
require 'milkode/common/dir'
@@ -20,7 +19,8 @@
require 'milkode/cdweb/lib/database'
require 'open-uri'
-require 'milkode/cdstk/cdstk_command' # @todo 削除予定
+require 'milkode/cdstk/cdstk_command'
+
require 'milkode/cdstk/yaml_file_wrapper'
require 'milkode/cdstk/package'
@@ -515,7 +515,7 @@ def custom_db?
end
def yaml_file
- CdstkYaml.yaml_file @db_dir
+ YamlFileWrapper.yaml_file @db_dir
end
def update_dir_in(dir)
View
177 lib/milkode/cdstk/cdstk_yaml.rb
@@ -1,177 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# @file
-# @brief
-# @author ongaeshi
-# @date 2011/02/20
-
-require 'yaml'
-require 'pathname'
-require 'milkode/common/dbdir'
-require 'milkode/common/util.rb'
-
-module Milkode
- class CdstkYaml
- MILKODE_YAML_VERSION = '0.2'
-
- class YAMLAlreadyExist < RuntimeError
- end
-
- class YAMLNotExist < RuntimeError
- end
-
- def self.create(path = ".")
- yf = yaml_file(path)
- raise YAMLAlreadyExist.new if FileTest.exist? yf
- obj = CdstkYaml.new(yf, {'contents' => [], 'version' => MILKODE_YAML_VERSION})
- obj.save
- return obj
- end
-
- def self.load(path = ".")
- yf = yaml_file(path)
- raise YAMLNotExist.new unless FileTest.exist? yf
- open(yf) do |f|
- return CdstkYaml.new(yf, YAML.load(f.read()))
- end
- end
-
- def initialize(yaml_file, data)
- @yaml_file = yaml_file
- @data = data
- normalize
- migrate
- end
-
- def normalize
- if (Util::platform_win?)
- contents.each do |v|
- v['directory'] = Util::normalize_filename v['directory']
- end
- end
- end
-
- def add(dirs)
- contents.concat(dirs.map{|v|{'directory' => v, 'ignore' => []}})
- contents.uniq!
- end
-
- def remove(query)
- r = query.select_any?(contents)
- r.each {|v| contents.delete v}
- end
-
- def remove_dir(dir)
- contents.delete_if do |v|
- dir == v['directory']
- end
- end
-
- def save
- open(@yaml_file, "w") { |f| YAML.dump(@data, f) }
- end
-
- def contents
- @data['contents']
- end
-
- def find_content(dir)
- contents.find do |v|
- dir == v['directory']
- end
- end
-
- def package_num
- @data['contents'].size
- end
-
- def directorys
- contents.map{|v|v['directory']}
- end
-
- def version
- @data['version']
- end
-
- def list(query = nil)
- query ? query.select_all?(contents) : contents
- end
-
- def exist?(shortname)
- @data['contents'].find {|v| File.basename(v['directory']) == shortname }
- end
-
- def cant_add_directory?(dir)
- @data['contents'].find {|v|
- v['directory'] != File.expand_path(dir) &&
- File.basename(v['directory']) == File.basename(dir)
- }
- end
-
- def cleanup
- contents.delete_if do |v|
- if (!File.exist? v['directory'])
- yield v if block_given?
- true
- else
- false
- end
- end
- end
-
- def package_root(dir)
- nd = Util::normalize_filename dir
- @data['contents'].find do |v|
- v if nd =~ /^#{v['directory']}/
- end
- end
-
- def package_root_dir(dir)
- package = package_root(dir)
- (package) ? package['directory'] : nil
- end
-
- def self.yaml_file(path)
- Dbdir.yaml_path(path)
- end
-
- class Query
- def initialize(keywords)
- @keywords = keywords
- end
-
- def select_any?(contents)
- contents.find_all do |v|
- @keywords.any? {|s| File.basename(v['directory']).include? s }
- end
- end
-
- def select_all?(contents)
- contents.find_all do |v|
- @keywords.all? {|s| File.basename(v['directory']).include? s }
- end
- end
- end
-
- def migrate
- if (version != MILKODE_YAML_VERSION)
- puts "milkode.yaml is old '#{version}'. Convert to '#{MILKODE_YAML_VERSION}'."
-
- # バージョン更新
- @data['version'] = MILKODE_YAML_VERSION
-
- # データ内容更新
- contents.each do |v|
- v['ignore'] = [] unless v['ignore']
- end
-
- # セーブ
- save
- end
- end
-
- def ignore(dir)
- find_content(dir)['ignore']
- end
- end
-end
View
19 lib/milkode/grep/cli_grep.rb
@@ -3,8 +3,9 @@
require 'optparse'
require 'milkode/findgrep/findgrep'
require 'milkode/common/dbdir'
-require 'milkode/cdstk/cdstk_yaml'
require 'milkode/cdstk/cdstk'
+require 'milkode/cdstk/yaml_file_wrapper'
+require 'milkode/cdstk/package'
module Milkode
class CLI_Grep
@@ -142,28 +143,28 @@ def self.execute(stdout, arguments=[])
private
def self.setup_package(option, my_option, keyword)
- packages = yaml_load.list( CdstkYaml::Query.new([keyword]) ).map{|v| v['directory']}
- raise NotFoundPackage.new keyword if (packages.empty?)
- option.packages += packages
- my_option[:packages] += packages
+ 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(dir)
+ yaml_load.package_root(dir)
end
def self.package_root_dir(dir)
- package_root = yaml_load.package_root_dir(dir)
+ package_root = yaml_load.package_root(dir)
if (package_root)
- package_root
+ package_root.directory
else
raise NotFoundPackage.new dir
end
end
def self.yaml_load
- CdstkYaml.load(Dbdir.select_dbdir)
+ YamlFileWrapper.load(Dbdir.select_dbdir)
end
class ArgumentParser
View
203 test/test_cdstk_yaml.rb
@@ -1,203 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# @file
-# @brief
-# @author ongaeshi
-# @date 2011/02/20
-
-require 'test_helper'
-require 'milkode/cdstk/cdstk_yaml.rb'
-require 'fileutils'
-
-class TestCdstkYaml < Test::Unit::TestCase
- include Milkode
-
- def setup
- @prev_dir = Dir.pwd
- @tmp_dir = Pathname(File.dirname(__FILE__)) + "tmp"
- FileUtils.rm_rf(@tmp_dir.to_s)
- FileUtils.mkdir_p(@tmp_dir.to_s)
- FileUtils.cd(@tmp_dir.to_s)
- end
-
- def test_000
- # create
- yaml = CdstkYaml.create
- assert_equal yaml.contents, []
- assert_equal yaml.version, '0.2'
- assert_raise(CdstkYaml::YAMLAlreadyExist) { CdstkYaml.create }
-
- # load
- yaml = CdstkYaml.load
- assert_equal yaml.contents, []
- assert_equal yaml.version, '0.2'
-
- # load fail
- FileUtils.mkdir 'loadtest'
- FileUtils.cd 'loadtest' do
- assert_raise(CdstkYaml::YAMLNotExist) { CdstkYaml.load }
- end
-
- # add
- yaml.add(['dir1'])
- yaml.add(['dir2', 'dir3'])
- assert_equal ['dir1', 'dir2', 'dir3'], yaml.directorys
-
- # remove
- yaml.add(['dir2', 'dir4', 'dir5'])
- yaml.remove(CdstkYaml::Query.new ['dir5'])
- yaml.remove(CdstkYaml::Query.new ['dir2', 'dir3'])
- assert_equal ['dir1', 'dir4'], yaml.directorys
-
- # save
- yaml.save
- r = YAML.load(open('milkode.yaml').read)
- assert_equal '0.2', r['version']
- assert_equal([{"directory"=>"dir1", "ignore"=>[]}, {"directory"=>"dir4", "ignore"=>[]}], r['contents'])
- end
-
- def test_001
- FileUtils.mkdir 'otherpath'
- yaml = CdstkYaml.create('otherpath')
- yaml.save
-
- # save
- r = YAML.load(open('otherpath/milkode.yaml').read)
- assert_equal '0.2', r['version']
- assert_equal([], r['contents'])
- end
-
- def test_query
- d = 'directory'
-
- contents = [{d => 'key'}, {d => 'keyword'}, {d => 'not'}]
-
- query = CdstkYaml::Query.new(['key'])
- assert_equal [{d => 'key'}, {d => 'keyword'}], query.select_any?(contents)
-
- query = CdstkYaml::Query.new(['word'])
- assert_equal [{d => 'keyword'}], query.select_any?(contents)
-
- contents = [{d => 'a/dir'}, {d => 'b/dia'}]
- query = CdstkYaml::Query.new(['a'])
- assert_equal [{d => 'b/dia'}], query.select_any?(contents) # ディレクトリ名は含めない
- end
-
- def test_list
- src = <<EOF
-version: 0.1
-contents:
-- directory: /a/dir1
-- directory: /b/dir4
-EOF
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src)) # 自動で0.2にアップグレードされる
- assert_equal [{"directory"=>"/a/dir1", "ignore"=>[]}, {"directory"=>"/b/dir4", "ignore"=>[]}], yaml.list
- assert_equal [{"directory"=>"/b/dir4", "ignore"=>[]}], yaml.list(CdstkYaml::Query.new(['4']))
- assert_equal [], yaml.list(CdstkYaml::Query.new(['a']))
- assert_equal [{"directory"=>"/a/dir1", "ignore"=>[]}, {"directory"=>"/b/dir4", "ignore"=>[]}], yaml.list(nil)
- end
-
- def test_remove
- src = <<EOF
-version: 0.1
-contents:
-- directory: /a/dir1
-- directory: /b/dir4
-EOF
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
-
- yaml.remove(CdstkYaml::Query.new(['dir4']))
- assert_equal [{"directory"=>"/a/dir1", "ignore"=>[]}], yaml.list
-
- yaml.remove(CdstkYaml::Query.new(['dir1']))
- assert_equal [], yaml.list
-
- # ---
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
-
- yaml.remove(CdstkYaml::Query.new(['dir1']))
- assert_equal [{"directory"=>"/b/dir4", "ignore"=>[]}], yaml.list
-
- yaml.remove(CdstkYaml::Query.new([]))
- assert_equal [{"directory"=>"/b/dir4", "ignore"=>[]}], yaml.list
-
- end
-
- def test_exist
- src = <<EOF
-version: 0.1
-contents:
-- directory: /a/dir1
-- directory: /b/dir12
-- directory: /b/dir4
-EOF
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
-
- assert_not_nil yaml.exist?('dir1')
- assert_not_nil yaml.exist?('dir12')
- assert_nil yaml.exist?('dir123')
- assert_nil yaml.exist?('dir')
- end
-
- def test_package_root
- src = <<EOF
-version: 0.1
-contents:
-- directory: /a/dir1
-- directory: /path/to/dir
-- directory: /a/b/c
-EOF
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
-
- assert_equal nil , yaml.package_root_dir('/not_dir')
- assert_equal "/a/dir1" , yaml.package_root_dir('/a/dir1/dir3')
- assert_equal nil , yaml.package_root_dir('/hoge/a/dir1/dir3')
- assert_equal '/path/to/dir', yaml.package_root_dir('/path/to/dir')
- end
-
- def test_find_content
- src = <<EOF
-version: '0.2'
-contents:
-- directory: /a/dir1
- ignore: []
-- directory: /path/to/dir
- ignore: []
-- directory: /a/b/c
- ignore: []
-EOF
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
- assert_equal '/a/dir1' , yaml.find_content('/a/dir1')['directory']
- assert_equal nil , yaml.find_content('/a/dir2')
- assert_equal '/path/to/dir' , yaml.find_content('/path/to/dir')['directory']
- assert_equal '/a/b/c' , yaml.find_content('/a/b/c')['directory']
- end
-
- def test_ignore
- src = <<EOF
-version: '0.2'
-contents:
-- directory: /a/dir1
- ignore: []
-- directory: /path/to/dir
- ignore: ['*.bak', '/rdoc']
-- directory: /a/b/c
- ignore: []
-EOF
-
- yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
- assert_equal [], yaml.ignore('/a/dir1')
- assert_equal ['*.bak', '/rdoc'], yaml.ignore('/path/to/dir')
- end
-
- def teardown
- FileUtils.cd(@prev_dir)
- FileUtils.rm_rf(@tmp_dir.to_s)
- end
-end

0 comments on commit 615e344

Please sign in to comment.
Something went wrong with that request. Please try again.