milkode 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/VERSION +1 -1
  2. data/bin/gmilk +1 -1
  3. data/bin/milk +1 -1
  4. data/lib/milkode/cdstk/cdstk.rb +337 -117
  5. data/lib/milkode/cdstk/cli_cdstk.rb +27 -9
  6. data/lib/milkode/cdstk/cli_cdstksub.rb +44 -6
  7. data/lib/milkode/cdstk/milkode_yaml.rb +105 -0
  8. data/lib/milkode/cdstk/package.rb +83 -0
  9. data/lib/milkode/cdstk/yaml_file_wrapper.rb +94 -0
  10. data/lib/milkode/cdweb/app.rb +6 -28
  11. data/lib/milkode/cdweb/lib/database.rb +6 -0
  12. data/lib/milkode/cdweb/lib/grep.rb +28 -0
  13. data/lib/milkode/cdweb/lib/mkurl.rb +2 -0
  14. data/lib/milkode/cdweb/lib/search_contents.rb +49 -10
  15. data/lib/milkode/cdweb/views/filelist.haml +2 -6
  16. data/lib/milkode/cdweb/views/header_menu.haml +5 -0
  17. data/lib/milkode/cdweb/views/search.haml +4 -8
  18. data/lib/milkode/cdweb/views/search_form.haml +22 -0
  19. data/lib/milkode/cdweb/views/view.haml +2 -6
  20. data/lib/milkode/common/ignore_checker.rb +34 -0
  21. data/lib/milkode/common/ignore_setting.rb +62 -0
  22. data/lib/milkode/grep/cli_grep.rb +10 -9
  23. data/milkode.gemspec +18 -5
  24. data/test/data/.gitignore +42 -0
  25. data/test/data/no_auto_ignore/.gitignore +1 -0
  26. data/test/data/no_auto_ignore/a.txt +3 -0
  27. data/test/milkode_test_work.rb +2 -2
  28. data/test/test_cdstk.rb +17 -12
  29. data/test/test_database.rb +4 -4
  30. data/test/test_ignore_checker.rb +26 -0
  31. data/test/test_ignore_setting.rb +113 -0
  32. data/test/test_milkode_yaml.rb +149 -0
  33. data/test/test_package.rb +73 -0
  34. data/test/test_yaml_file_wrapper.rb +97 -0
  35. metadata +19 -6
  36. data/lib/milkode/cdstk/cdstk_yaml.rb +0 -140
  37. data/test/test_cdstk_yaml.rb +0 -167
@@ -19,6 +19,7 @@ The most commonly used #{File.basename($0)} are:
19
19
  dir Disp package dir.
20
20
  dump Dump records.
21
21
  grep Print lines matching a pattern
22
+ ignore Ignore setting.
22
23
  info Disp information.
23
24
  init Init db.
24
25
  list List packages.
@@ -49,35 +50,40 @@ EOF
49
50
  suboptions = Hash.new
50
51
 
51
52
  subopt['init'], suboptions['init'] = CLI_Cdstksub.setup_init
52
- subopt['add'] = CLI_Cdstksub.setup_add
53
+ subopt['add'], suboptions['add'] = CLI_Cdstksub.setup_add
53
54
  subopt['update'], suboptions['update'] = CLI_Cdstksub.setup_update
54
55
  subopt['remove'], suboptions['remove'] = CLI_Cdstksub.setup_remove
55
56
  subopt['list'], suboptions['list'] = CLI_Cdstksub.setup_list
56
57
  subopt['pwd'], suboptions['pwd'] = CLI_Cdstksub.setup_pwd
57
58
  subopt['cleanup'], suboptions['cleanup'] = CLI_Cdstksub.setup_cleanup
58
- subopt['rebuild'] = OptionParser.new("#{File.basename($0)} rebuild")
59
+ subopt['rebuild'], suboptions['rebuild'] = CLI_Cdstksub.setup_rebuild
59
60
  subopt['dump'] = OptionParser.new("#{File.basename($0)} dump")
60
61
  subopt['web'], suboptions['web'] = CLI_Cdstksub.setup_web
61
62
  subopt['dir'], suboptions['dir'] = CLI_Cdstksub.setup_dir
62
63
  subopt['setdb'], suboptions['setdb'] = CLI_Cdstksub.setup_setdb
63
64
  subopt['mcd'], suboptions['mcd'] = CLI_Cdstksub.setup_mcd
64
65
  subopt['info'], suboptions['info'] = CLI_Cdstksub.setup_info
66
+ subopt['ignore'], suboptions['ignore'] = CLI_Cdstksub.setup_ignore
65
67
 
66
68
  if (subopt[subcommand])
67
69
  subopt[subcommand].parse!(arguments) unless arguments.empty?
68
70
  init_default = suboptions['init'][:init_default]
69
71
 
70
- db_dir = select_dbdir(subcommand, init_default)
72
+ db_dir = select_dbdir(subcommand, init_default, arguments)
71
73
  obj = Cdstk.new(stdout, db_dir)
72
74
 
73
75
  case subcommand
74
76
  when 'init'
75
- FileUtils.mkdir_p db_dir if (init_default)
76
- obj.init
77
+ if (arguments.size <= 1)
78
+ FileUtils.mkdir_p db_dir if (init_default || init_specify_dbddir?(arguments))
79
+ obj.init(suboptions[subcommand])
80
+ else
81
+ $stderr.puts "milk init [db_dir] (Cannot specify two 'db_dir')"
82
+ end
77
83
  when 'update'
78
84
  obj.update(arguments, suboptions[subcommand])
79
85
  when 'add'
80
- obj.add(arguments)
86
+ obj.add(arguments, suboptions[subcommand])
81
87
  when 'remove'
82
88
  obj.remove(arguments, suboptions[subcommand])
83
89
  when 'list'
@@ -87,7 +93,7 @@ EOF
87
93
  when 'cleanup'
88
94
  obj.cleanup(suboptions[subcommand])
89
95
  when 'rebuild'
90
- obj.rebuild
96
+ obj.rebuild(arguments, suboptions[subcommand])
91
97
  when 'dump'
92
98
  obj.dump
93
99
  when 'web'
@@ -101,6 +107,12 @@ EOF
101
107
  obj.mcd(arguments, suboptions[subcommand])
102
108
  when 'info'
103
109
  obj.info(arguments, suboptions[subcommand])
110
+ when 'ignore'
111
+ begin
112
+ obj.ignore(arguments, suboptions[subcommand])
113
+ rescue IgnoreError => e
114
+ stdout.puts e.message
115
+ end
104
116
  end
105
117
  else
106
118
  if subcommand
@@ -111,9 +123,11 @@ EOF
111
123
  end
112
124
  end
113
125
 
114
- def self.select_dbdir(subcommand, init_default)
126
+ def self.select_dbdir(subcommand, init_default, arguments)
115
127
  if (subcommand == 'init')
116
- if (init_default)
128
+ if (init_specify_dbddir?(arguments))
129
+ arguments[0]
130
+ elsif (init_default)
117
131
  Dbdir.default_dir
118
132
  else
119
133
  '.'
@@ -126,6 +140,10 @@ EOF
126
140
  end
127
141
  end
128
142
  end
143
+
144
+ def self.init_specify_dbddir?(arguments)
145
+ arguments.size == 1
146
+ end
129
147
 
130
148
  end
131
149
  end
@@ -10,8 +10,9 @@ module Milkode
10
10
  def self.setup_init
11
11
  options = {:init_default => false}
12
12
 
13
- opt = OptionParser.new("#{File.basename($0)} init")
13
+ opt = OptionParser.new("#{File.basename($0)} init [db_dir]")
14
14
  opt.on('--default', 'Init default db, ENV[\'MILKODE_DEFAULT_DIR\'] or ~/.milkode.') { options[:init_default] = true }
15
+ opt.on('-s', '--setdb', 'With setdb.') { options[:setdb] = true }
15
16
 
16
17
  return opt, options
17
18
  end
@@ -19,8 +20,10 @@ module Milkode
19
20
  def self.setup_add
20
21
  bin = File.basename($0)
21
22
 
23
+ options = {:ignore => []}
24
+
22
25
  opt = OptionParser.new(<<EOF)
23
- #{bin} add package1 [package2 ...]
26
+ #{bin} add dir1 [dir2 ...]
24
27
  usage:
25
28
  #{bin} add /path/to/dir1
26
29
  #{bin} add /path/to/dir2 /path/to/dir3
@@ -28,9 +31,15 @@ usage:
28
31
  #{bin} add /path/to/zipfile.zip
29
32
  #{bin} add /path/to/addon.xpi
30
33
  #{bin} add http://example.com/urlfile.zip
34
+
35
+ option:
31
36
  EOF
37
+ # opt.on('-n NAME', '--name NAME', 'Specify name (default: File.basename(dir))') {|v| options[:name] = v }
38
+ opt.on('-i PATH', '--ignore PATH', 'Ignore path.') {|v| options[:ignore] << v }
39
+ opt.on('--no-auto-ignore', 'Disable auto ignore (.gitignore)') { options[:no_auto_ignore] = true }
40
+ opt.on('-v', '--verbose', 'Be verbose.') { options[:verbose] = true }
32
41
 
33
- opt
42
+ return opt, options
34
43
  end
35
44
 
36
45
  def self.setup_update
@@ -38,6 +47,7 @@ EOF
38
47
 
39
48
  opt = OptionParser.new("#{File.basename($0)} update [keyword1 keyword2 ...]")
40
49
  opt.on('--all', 'Update all.') { options[:all] = true }
50
+ opt.on('-v', '--verbose', 'Be verbose.') { options[:verbose] = true }
41
51
 
42
52
  return opt, options
43
53
  end
@@ -45,8 +55,10 @@ EOF
45
55
  def self.setup_remove
46
56
  options = {:force => false}
47
57
 
48
- opt = OptionParser.new("#{File.basename($0)} remove package1 [package2 ...]")
49
- opt.on('-f', '--force', 'Force remove.') { options[:force] = true }
58
+ opt = OptionParser.new("#{File.basename($0)} remove keyword1 [keyword2 ...]")
59
+ opt.on('--all', 'Update all.') { options[:all] = true }
60
+ opt.on('-f', '--force', 'Force remove.') { options[:force] = true }
61
+ opt.on('-v', '--verbose', 'Be verbose.') { options[:verbose] = true }
50
62
 
51
63
  return opt, options
52
64
  end
@@ -78,6 +90,16 @@ EOF
78
90
  return opt, options
79
91
  end
80
92
 
93
+ def self.setup_rebuild
94
+ options = {}
95
+
96
+ opt = OptionParser.new("#{File.basename($0)} keyword1 [keyword2 ...]")
97
+ opt.on('--all', 'Rebuild all.') { options[:all] = true}
98
+ opt.on('-v', '--verbose', 'Be verbose.') { options[:verbose] = true }
99
+
100
+ return opt, options
101
+ end
102
+
81
103
  def self.setup_web
82
104
  options = {
83
105
  :environment => ENV['RACK_ENV'] || "development",
@@ -121,7 +143,7 @@ EOF
121
143
  options = {}
122
144
 
123
145
  opt = OptionParser.new("#{File.basename($0)} setdb")
124
- opt.on('--reset', 'Reset default db.') {|v| options[:reset] = true }
146
+ opt.on('--default', '--reset', 'Reset default db.') {|v| options[:reset] = true }
125
147
 
126
148
  return opt, options
127
149
  end
@@ -141,5 +163,21 @@ EOF
141
163
 
142
164
  return opt, options
143
165
  end
166
+
167
+ def self.setup_ignore
168
+ bin = File.basename($0)
169
+
170
+ options = {}
171
+
172
+ opt = OptionParser.new("#{File.basename($0)} ignore [path ...]")
173
+ opt.on('-p PACKAGE', '--package PACKAGE', 'Specify ignore package.') {|v| options[:package] = v }
174
+ opt.on('-d', '--delete', 'Delete ignore') { options[:delete] = true }
175
+ opt.on('--delete-all', 'Delete all') { options[:delete_all] = true }
176
+ opt.on('--test', 'Ignore test, Display complete list') { options[:test] = true }
177
+
178
+ return opt, options
179
+ end
180
+
181
+
144
182
  end
145
183
  end
@@ -0,0 +1,105 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2012/02/21
7
+
8
+ require 'yaml'
9
+ require 'milkode/cdstk/package'
10
+ require 'milkode/common/util'
11
+
12
+ module Milkode
13
+ class MilkodeYaml
14
+ MILKODE_YAML_VERSION = '0.2'
15
+
16
+ EMPTY_YAML = <<EOF
17
+ ---
18
+ version: '#{MILKODE_YAML_VERSION}'
19
+ contents: []
20
+ EOF
21
+
22
+ attr_reader :contents
23
+
24
+ def initialize(str = nil)
25
+ @data = YAML.load(str || EMPTY_YAML)
26
+ @contents = parse_contents
27
+ end
28
+
29
+ def dump
30
+ YAML.dump(@data)
31
+ end
32
+
33
+ def version
34
+ @data['version']
35
+ end
36
+
37
+ # パッケージを追加
38
+ def add(package)
39
+ @contents.push package
40
+ update_contents
41
+ end
42
+
43
+ # 同名パッケージの内容を置き換え
44
+ def update(package)
45
+ i = @contents.find_index {|v| v.same_name?(package.name) }
46
+ raise unless i
47
+ @contents[i] = package
48
+ update_contents
49
+ end
50
+
51
+ # パッケージを削除
52
+ def remove(package)
53
+ @contents.delete(package)
54
+ update_contents
55
+ end
56
+
57
+ # 名前が同じパッケージを検索
58
+ def find_name(name)
59
+ @contents.find {|v| v.same_name?(name)}
60
+ end
61
+
62
+ # ディレクトリ名が同じパッケージを検索
63
+ def find_dir(directory)
64
+ @contents.find {|v| v.directory == directory}
65
+ end
66
+
67
+ # 指定ディレクトリの所属するパッケージのルートディレクトリを得る。
68
+ # 見つからない場合はnilを返す。
69
+ def package_root(dir)
70
+ nd = Util::normalize_filename dir
71
+ @contents.find do |v|
72
+ v if nd =~ /^#{v.directory}/
73
+ end
74
+ end
75
+
76
+ # マイグレーション
77
+ def migrate
78
+ if (version != MILKODE_YAML_VERSION)
79
+ # バージョン番号
80
+ @data['version'] = MILKODE_YAML_VERSION
81
+
82
+ # パッケージ
83
+ contents.each{|v| v.migrate}
84
+
85
+ # migrateが起きた
86
+ true
87
+ else
88
+ false
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def parse_contents
95
+ @data['contents'].map do |v|
96
+ Package.new(v)
97
+ end
98
+ end
99
+
100
+ def update_contents
101
+ @data['contents'] = @contents.map{|v| v.hash }
102
+ end
103
+ end
104
+ end
105
+
@@ -0,0 +1,83 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2012/02/21
7
+
8
+ require 'milkode/common/util'
9
+
10
+ module Milkode
11
+ class Package
12
+ def self.create(dir, ignore=nil)
13
+ if ignore && ignore.size > 0
14
+ Package.new({"directory" => dir, "ignore" => ignore})
15
+ else
16
+ Package.new({"directory" => dir})
17
+ end
18
+ end
19
+
20
+ def initialize(hash)
21
+ @hash = hash
22
+ normalize
23
+ end
24
+
25
+ def name
26
+ if options[:name]
27
+ options[:name]
28
+ else
29
+ File.basename(directory)
30
+ end
31
+ end
32
+
33
+ def directory
34
+ @hash['directory']
35
+ end
36
+
37
+ def ignore
38
+ @hash['ignore'] || []
39
+ end
40
+
41
+ def set_ignore(ignore)
42
+ @hash['ignore'] = ignore
43
+ end
44
+
45
+ def options
46
+ @hash['options'] || {}
47
+ end
48
+
49
+ def set_options(options)
50
+ @hash['options'] = options
51
+ end
52
+
53
+ def hash
54
+ @hash
55
+ end
56
+
57
+ def migrate
58
+ # 色々あって、ignore値はデフォルトで設定しないようにした
59
+ # @hash['ignore'] = [] unless ignore
60
+ end
61
+
62
+ # 同名パッケージか?
63
+ def same_name?(a_name)
64
+ name == a_name
65
+ end
66
+
67
+ # 同値検査
68
+ def ==(rhs)
69
+ name == rhs.name && directory == rhs.directory && ignore == rhs.ignore
70
+ end
71
+
72
+ private
73
+
74
+ def normalize
75
+ if (Util::platform_win?)
76
+ @hash['directory'] = Util::normalize_filename(directory)
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+
@@ -0,0 +1,94 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/02/20
7
+
8
+ require 'yaml'
9
+ require 'milkode/common/dbdir'
10
+ require 'milkode/cdstk/milkode_yaml'
11
+
12
+ module Milkode
13
+ class YamlFileWrapper
14
+ class YAMLAlreadyExist < RuntimeError ; end
15
+ class YAMLNotExist < RuntimeError ; end
16
+
17
+ def self.yaml_file(path)
18
+ Dbdir.yaml_path(path)
19
+ end
20
+
21
+ def self.create(path = ".")
22
+ yf = yaml_file(path)
23
+ raise YAMLAlreadyExist.new if FileTest.exist? yf
24
+ obj = YamlFileWrapper.new(yf, MilkodeYaml.new)
25
+ obj.save
26
+ return obj
27
+ end
28
+
29
+ def self.load(path = ".")
30
+ yf = yaml_file(path)
31
+ raise YAMLNotExist.new unless FileTest.exist? yf
32
+ open(yf) do |f|
33
+ return YamlFileWrapper.new(yf, MilkodeYaml.new(f.read()))
34
+ end
35
+ end
36
+
37
+ def self.load_if(path = ".")
38
+ begin
39
+ load(path)
40
+ rescue YAMLNotExist
41
+ nil
42
+ end
43
+ end
44
+
45
+ def initialize(yaml_file, data)
46
+ @yaml_file = yaml_file
47
+ @data = data
48
+ migrate
49
+ end
50
+
51
+ def contents
52
+ @data.contents
53
+ end
54
+
55
+ def find_name(name)
56
+ @data.find_name(name)
57
+ end
58
+
59
+ def find_dir(dir)
60
+ @data.find_dir(dir)
61
+ end
62
+
63
+ def add(package)
64
+ @data.add package
65
+ end
66
+
67
+ def update(package)
68
+ @data.update package
69
+ end
70
+
71
+ def remove(package)
72
+ @data.remove package
73
+ end
74
+
75
+ def package_root(dir)
76
+ @data.package_root(dir)
77
+ end
78
+
79
+ def save
80
+ open(@yaml_file, "w") { |f| f.write(@data.dump) }
81
+ end
82
+
83
+ def version
84
+ @data.version
85
+ end
86
+
87
+ def migrate
88
+ if (@data.migrate)
89
+ puts "milkode.yaml is old '#{version}'. Convert to '#{MilkodeYaml::MILKODE_YAML_VERSION}'."
90
+ save
91
+ end
92
+ end
93
+ end
94
+ end