milkode 0.2.4 → 0.2.9
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.
- data/HISTORY.ja.rdoc +16 -0
- data/HISTORY.rdoc +25 -0
- data/Rakefile +3 -2
- data/VERSION +1 -1
- data/bin/gmilk +10 -0
- data/bin/milk +1 -1
- data/lib/milkode/cdstk/cdstk.rb +121 -6
- data/lib/milkode/cdstk/cdstk_command.rb +28 -0
- data/lib/milkode/cdstk/cdstk_yaml.rb +30 -3
- data/lib/milkode/cdstk/cli_cdstk.rb +37 -14
- data/lib/milkode/cdstk/cli_cdstksub.rb +36 -1
- data/lib/milkode/cdweb/app.rb +1 -1
- data/lib/milkode/cdweb/lib/coderay_wrapper.rb +4 -5
- data/lib/milkode/cdweb/lib/database.rb +2 -0
- data/lib/milkode/cdweb/lib/my_nokogiri.rb +34 -0
- data/lib/milkode/common/dbdir.rb +30 -1
- data/lib/milkode/common/util.rb +15 -3
- data/lib/milkode/findgrep/findgrep.rb +86 -18
- data/lib/milkode/findgrep/result.rb +2 -2
- data/lib/milkode/grep/cli_grep.rb +113 -0
- data/milkode.gemspec +32 -9
- data/test/data/a_project/cdstk.rb +634 -0
- data/test/data/a_project/cdstk_yaml.rb +130 -0
- data/test/data/b_project/runner.rb +11 -0
- data/test/data/b_project/test_dir.rb +21 -0
- data/test/milkode_test_work.rb +78 -0
- data/test/test_cdstk.rb +12 -2
- data/test/test_cdstk_command.rb +50 -0
- data/test/test_cdstk_yaml.rb +20 -3
- data/test/test_cli_cdstk.rb +71 -0
- data/test/test_cli_grep.rb +79 -0
- data/test/test_database.rb +1 -1
- data/test/test_dbdir.rb +11 -0
- metadata +43 -8
@@ -33,6 +33,15 @@ EOF
|
|
33
33
|
opt
|
34
34
|
end
|
35
35
|
|
36
|
+
def self.setup_update
|
37
|
+
options = {:force => false}
|
38
|
+
|
39
|
+
opt = OptionParser.new("#{File.basename($0)} update [keyword1 keyword2 ...]")
|
40
|
+
opt.on('--all', 'Update all.') { options[:all] = true }
|
41
|
+
|
42
|
+
return opt, options
|
43
|
+
end
|
44
|
+
|
36
45
|
def self.setup_remove
|
37
46
|
options = {:force => false}
|
38
47
|
|
@@ -45,7 +54,7 @@ EOF
|
|
45
54
|
def self.setup_list
|
46
55
|
options = {:verbose => false}
|
47
56
|
|
48
|
-
opt = OptionParser.new("#{File.basename($0)} list package1 [package2 ...]")
|
57
|
+
opt = OptionParser.new("#{File.basename($0)} list package1 [package2 ...]") # @todo コメント修正
|
49
58
|
opt.on('-v', '--verbose', 'Be verbose.') { options[:verbose] = true }
|
50
59
|
|
51
60
|
return opt, options
|
@@ -98,5 +107,31 @@ EOF
|
|
98
107
|
|
99
108
|
return opts, options
|
100
109
|
end
|
110
|
+
|
111
|
+
def self.setup_dir
|
112
|
+
options = {}
|
113
|
+
|
114
|
+
opt = OptionParser.new("#{File.basename($0)} dir")
|
115
|
+
opt.on('--top', 'XXX') {|v| options[:top] = true }
|
116
|
+
|
117
|
+
return opt, options
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.setup_setdb
|
121
|
+
options = {}
|
122
|
+
|
123
|
+
opt = OptionParser.new("#{File.basename($0)} setdb")
|
124
|
+
opt.on('--reset', 'Reset default db.') {|v| options[:reset] = true }
|
125
|
+
|
126
|
+
return opt, options
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.setup_mcd
|
130
|
+
options = {}
|
131
|
+
|
132
|
+
opt = OptionParser.new("#{File.basename($0)} mcd")
|
133
|
+
|
134
|
+
return opt, options
|
135
|
+
end
|
101
136
|
end
|
102
137
|
end
|
data/lib/milkode/cdweb/app.rb
CHANGED
@@ -8,8 +8,8 @@
|
|
8
8
|
require 'rubygems'
|
9
9
|
require 'coderay'
|
10
10
|
require 'coderay/helpers/file_type'
|
11
|
-
require 'nokogiri'
|
12
11
|
require 'milkode/common/util'
|
12
|
+
require 'milkode/cdweb/lib/my_nokogiri'
|
13
13
|
|
14
14
|
module Milkode
|
15
15
|
class CodeRayWrapper
|
@@ -58,7 +58,7 @@ module Milkode
|
|
58
58
|
)
|
59
59
|
|
60
60
|
if (is_ornament?)
|
61
|
-
html_doc =
|
61
|
+
html_doc = MyNokogiri::HTML(html)
|
62
62
|
add_spanid(html_doc)
|
63
63
|
else
|
64
64
|
html
|
@@ -76,7 +76,7 @@ module Milkode
|
|
76
76
|
)
|
77
77
|
|
78
78
|
if (is_ornament?)
|
79
|
-
html_doc =
|
79
|
+
html_doc = MyNokogiri::HTML(html)
|
80
80
|
anchor = create_anchorlink(html_doc.at_css("table.CodeRay td.code pre").inner_html)
|
81
81
|
body = add_spanid(html_doc)
|
82
82
|
anchor + body
|
@@ -86,9 +86,8 @@ module Milkode
|
|
86
86
|
end
|
87
87
|
|
88
88
|
def add_spanid(html_doc)
|
89
|
-
table = html_doc.at_css("table.CodeRay")
|
90
|
-
|
91
89
|
# preに<span id="行番号"> を付ける
|
90
|
+
table = html_doc.at_css("table.CodeRay")
|
92
91
|
pre = table.at_css("td.code pre")
|
93
92
|
pre.inner_html = add_spanid_in(pre.inner_html)
|
94
93
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# @file
|
4
|
+
# @brief
|
5
|
+
# @author ongaeshi
|
6
|
+
# @date 2011/12/02
|
7
|
+
|
8
|
+
require 'hpricot'
|
9
|
+
require 'nokogiri'
|
10
|
+
require 'milkode/common/util'
|
11
|
+
|
12
|
+
module Hpricot
|
13
|
+
class Elements
|
14
|
+
alias_method :at_css, :search
|
15
|
+
end
|
16
|
+
|
17
|
+
module Traverse
|
18
|
+
alias_method :at_css, :search
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Milkode
|
23
|
+
class MyNokogiri
|
24
|
+
def self.HTML(html)
|
25
|
+
if Util::ruby19?
|
26
|
+
Nokogiri::HTML(html)
|
27
|
+
else
|
28
|
+
Hpricot(html)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
data/lib/milkode/common/dbdir.rb
CHANGED
@@ -11,8 +11,29 @@ module Milkode
|
|
11
11
|
module Dbdir
|
12
12
|
module_function
|
13
13
|
|
14
|
+
@@milkode_db_dir = File.expand_path('~/.milkode_db_dir')
|
15
|
+
|
16
|
+
def milkode_db_dir
|
17
|
+
@@milkode_db_dir
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_milkode_db_dir(dir)
|
21
|
+
@@milkode_db_dir = dir
|
22
|
+
end
|
23
|
+
|
24
|
+
def tmp_milkode_db_dir(path)
|
25
|
+
old_path = Dbdir.milkode_db_dir
|
26
|
+
Dbdir.set_milkode_db_dir path
|
27
|
+
yield
|
28
|
+
Dbdir.set_milkode_db_dir old_path
|
29
|
+
end
|
30
|
+
|
14
31
|
def default_dir
|
15
|
-
|
32
|
+
path = @@milkode_db_dir
|
33
|
+
|
34
|
+
if (File.exist? path)
|
35
|
+
File.read path
|
36
|
+
elsif (ENV['MILKODE_DEFAULT_DIR'])
|
16
37
|
File.expand_path ENV['MILKODE_DEFAULT_DIR']
|
17
38
|
else
|
18
39
|
File.expand_path '~/.milkode'
|
@@ -34,6 +55,14 @@ module Milkode
|
|
34
55
|
def dbdir?(path = '.')
|
35
56
|
FileTest.exist? yaml_path(path)
|
36
57
|
end
|
58
|
+
|
59
|
+
def select_dbdir
|
60
|
+
if (Dbdir.dbdir?('.') || !Dbdir.dbdir?(Dbdir.default_dir))
|
61
|
+
'.'
|
62
|
+
else
|
63
|
+
Dbdir.default_dir
|
64
|
+
end
|
65
|
+
end
|
37
66
|
end
|
38
67
|
end
|
39
68
|
|
data/lib/milkode/common/util.rb
CHANGED
@@ -45,9 +45,13 @@ module Milkode
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def relative_path(path, basedir)
|
48
|
-
path = Pathname.new(path)
|
49
|
-
basedir = Pathname.new(basedir)
|
50
|
-
|
48
|
+
path = Pathname.new(normalize_filename path)
|
49
|
+
basedir = Pathname.new(normalize_filename basedir)
|
50
|
+
begin
|
51
|
+
path.relative_path_from(basedir)
|
52
|
+
rescue ArgumentError
|
53
|
+
path
|
54
|
+
end
|
51
55
|
end
|
52
56
|
|
53
57
|
def ruby19?
|
@@ -87,6 +91,14 @@ module Milkode
|
|
87
91
|
def larger_than_oneline(content)
|
88
92
|
content.count("\n") > 1
|
89
93
|
end
|
94
|
+
|
95
|
+
def normalize_filename(str)
|
96
|
+
if platform_win?
|
97
|
+
str.gsub(/\A([a-z]):/) { "#{$1.upcase}:" }
|
98
|
+
else
|
99
|
+
str
|
100
|
+
end
|
101
|
+
end
|
90
102
|
end
|
91
103
|
end
|
92
104
|
|
@@ -11,6 +11,7 @@ require 'groonga'
|
|
11
11
|
require 'milkode/common/util'
|
12
12
|
include Gren
|
13
13
|
require 'cgi'
|
14
|
+
require 'pathname'
|
14
15
|
|
15
16
|
module FindGrep
|
16
17
|
class FindGrep
|
@@ -22,6 +23,7 @@ module FindGrep
|
|
22
23
|
:colorHighlight,
|
23
24
|
:isSilent,
|
24
25
|
:debugMode,
|
26
|
+
:packages,
|
25
27
|
:filePatterns,
|
26
28
|
:suffixs,
|
27
29
|
:ignoreFiles,
|
@@ -31,7 +33,8 @@ module FindGrep
|
|
31
33
|
:dbFile,
|
32
34
|
:groongaOnly,
|
33
35
|
:isMatchFile,
|
34
|
-
:dispHtml
|
36
|
+
:dispHtml,
|
37
|
+
:matchCountLimit)
|
35
38
|
|
36
39
|
DEFAULT_OPTION = Option.new([],
|
37
40
|
[],
|
@@ -45,13 +48,17 @@ module FindGrep
|
|
45
48
|
[],
|
46
49
|
[],
|
47
50
|
[],
|
51
|
+
[],
|
48
52
|
Platform.get_shell_kcode,
|
49
53
|
false,
|
50
54
|
nil,
|
51
55
|
false,
|
52
56
|
false,
|
53
|
-
false
|
57
|
+
false,
|
58
|
+
-1)
|
54
59
|
|
60
|
+
class MatchCountOverError < RuntimeError ; end
|
61
|
+
|
55
62
|
attr_reader :documents
|
56
63
|
|
57
64
|
def initialize(patterns, option)
|
@@ -73,7 +80,7 @@ module FindGrep
|
|
73
80
|
|
74
81
|
if dbfile.exist?
|
75
82
|
Groonga::Database.open(dbfile.to_s)
|
76
|
-
puts "open : #{dbfile} open."
|
83
|
+
puts "open : #{dbfile} open." unless @option.isSilent
|
77
84
|
else
|
78
85
|
raise "error : #{dbfile.to_s} not found!!"
|
79
86
|
end
|
@@ -143,6 +150,16 @@ module FindGrep
|
|
143
150
|
end
|
144
151
|
end
|
145
152
|
|
153
|
+
# パッケージ(OR)
|
154
|
+
pe = package_expression(record, @option.packages)
|
155
|
+
if (pe)
|
156
|
+
if expression.nil?
|
157
|
+
expression = pe
|
158
|
+
else
|
159
|
+
expression &= pe
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
146
163
|
# パス
|
147
164
|
@option.filePatterns.each do |word|
|
148
165
|
sub_expression = record.path =~ word
|
@@ -155,26 +172,49 @@ module FindGrep
|
|
155
172
|
|
156
173
|
# 拡張子(OR)
|
157
174
|
se = suffix_expression(record)
|
158
|
-
|
175
|
+
if (se)
|
176
|
+
if expression.nil?
|
177
|
+
expression = se
|
178
|
+
else
|
179
|
+
expression &= se
|
180
|
+
end
|
181
|
+
end
|
159
182
|
|
160
183
|
# 検索式
|
161
184
|
expression
|
162
185
|
end
|
163
186
|
|
187
|
+
# @todo オプションで出来るようにする?
|
164
188
|
# タイムスタンプでソート
|
165
|
-
records = table.sort([{:key => "_score", :order => "descending"},
|
166
|
-
|
189
|
+
# records = table.sort([{:key => "_score", :order => "descending"},
|
190
|
+
# {:key => "timestamp", :order => "descending"}])
|
191
|
+
|
192
|
+
# ファイル名でソート
|
193
|
+
records = table.sort([{:key => "shortpath", :order => "ascending"}])
|
167
194
|
|
168
195
|
# データベースにヒット
|
169
|
-
stdout.puts "Found : #{records.size} records."
|
196
|
+
stdout.puts "Found : #{records.size} records." if (!@option.dispHtml && !@option.isSilent)
|
170
197
|
|
171
198
|
# 検索にヒットしたファイルを実際に検索
|
172
|
-
|
173
|
-
if (@
|
174
|
-
|
199
|
+
begin
|
200
|
+
if (@patterns.size > 0)
|
201
|
+
records.each do |record|
|
202
|
+
if (@option.groongaOnly)
|
203
|
+
searchGroongaOnly(stdout, record)
|
204
|
+
else
|
205
|
+
searchFile(stdout, record.path, record.path) if FileTest.exist?(record.path)
|
206
|
+
end
|
207
|
+
end
|
175
208
|
else
|
176
|
-
|
209
|
+
records.each do |record|
|
210
|
+
path = record.path
|
211
|
+
relative_path = Milkode::Util::relative_path(path, Dir.pwd).to_s
|
212
|
+
stdout.puts relative_path
|
213
|
+
@result.match_file_count += 1
|
214
|
+
raise MatchCountOverError if (0 < @option.matchCountLimit && @option.matchCountLimit <= @result.match_file_count)
|
215
|
+
end
|
177
216
|
end
|
217
|
+
rescue MatchCountOverError
|
178
218
|
end
|
179
219
|
end
|
180
220
|
|
@@ -193,6 +233,24 @@ module FindGrep
|
|
193
233
|
sub
|
194
234
|
end
|
195
235
|
|
236
|
+
def package_expression(record, packages)
|
237
|
+
sub = nil
|
238
|
+
|
239
|
+
# @todo 専用カラム package が欲しいところ
|
240
|
+
# でも今でもpackageはORとして機能してるからいいっちゃいい
|
241
|
+
packages.each do |word|
|
242
|
+
e = record.path =~ word
|
243
|
+
if sub.nil?
|
244
|
+
sub = e
|
245
|
+
else
|
246
|
+
sub |= e
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
sub
|
251
|
+
end
|
252
|
+
private :package_expression
|
253
|
+
|
196
254
|
def suffix_expression(record)
|
197
255
|
sub = nil
|
198
256
|
|
@@ -239,7 +297,7 @@ module FindGrep
|
|
239
297
|
searchFromDir(stdout, fpath, depth + 1)
|
240
298
|
when "file"
|
241
299
|
searchFile(stdout, fpath, fpath_disp)
|
242
|
-
end
|
300
|
+
end
|
243
301
|
end
|
244
302
|
end
|
245
303
|
private :searchFromDir
|
@@ -314,7 +372,7 @@ module FindGrep
|
|
314
372
|
|
315
373
|
@result.search_files << record.path if (@option.debugMode)
|
316
374
|
|
317
|
-
searchData(stdout, record.content, record.path)
|
375
|
+
searchData(stdout, record.content.split("\n"), record.path)
|
318
376
|
end
|
319
377
|
private :searchGroongaOnly
|
320
378
|
|
@@ -326,7 +384,10 @@ module FindGrep
|
|
326
384
|
|
327
385
|
if ( result )
|
328
386
|
unless (@option.dispHtml)
|
329
|
-
header = "#{path}:#{index + 1}:"
|
387
|
+
# header = "#{path}:#{index + 1}:"
|
388
|
+
rpath = Milkode::Util::relative_path(path, Dir.pwd).to_s
|
389
|
+
header = "#{rpath}:#{index + 1}:"
|
390
|
+
|
330
391
|
line = GrenSnip::snip(line, match_datas) unless (@option.noSnip)
|
331
392
|
|
332
393
|
unless (@option.colorHighlight)
|
@@ -354,24 +415,31 @@ EOF
|
|
354
415
|
end
|
355
416
|
|
356
417
|
@result.match_count += 1
|
418
|
+
if (0 < @option.matchCountLimit && @option.matchCountLimit <= @result.match_count)
|
419
|
+
raise MatchCountOverError
|
420
|
+
end
|
357
421
|
end
|
358
422
|
}
|
359
423
|
end
|
360
424
|
private :searchData
|
361
425
|
|
362
426
|
def file2data(file)
|
363
|
-
|
364
|
-
|
427
|
+
data = file.read
|
428
|
+
|
429
|
+
unless Milkode::Util::ruby19?
|
365
430
|
if (@option.kcode != Kconv::NOCONV)
|
366
431
|
file_kcode = Kconv::guess(data)
|
367
432
|
|
368
433
|
if (file_kcode != @option.kcode)
|
369
|
-
# puts "encode!! #{fpath} : #{@option.kcode} <- #{file_kcode}"
|
434
|
+
# puts "encode!! #{fpath} : #{@option.kcode} <- #{file_kcode}"
|
370
435
|
data = data.kconv(@option.kcode, file_kcode)
|
371
436
|
end
|
372
437
|
end
|
438
|
+
else
|
439
|
+
data = data.kconv(@option.kcode)
|
440
|
+
end
|
373
441
|
|
374
|
-
|
442
|
+
data = data.split("\n");
|
375
443
|
end
|
376
444
|
private :file2data
|
377
445
|
|
@@ -34,8 +34,8 @@ module FindGrep
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def print(stdout)
|
37
|
-
stdout.puts "dir : #{@start_dir} (#{Util::time_s(time)})"
|
38
|
-
stdout.puts "files : #{@search_count} in #{@count} (#{Util::size_s(@search_size)} in #{Util::size_s(@size)})"
|
37
|
+
stdout.puts "dir : #{@start_dir} (#{Gren::Util::time_s(time)})"
|
38
|
+
stdout.puts "files : #{@search_count} in #{@count} (#{Gren::Util::size_s(@search_size)} in #{Gren::Util::size_s(@size)})"
|
39
39
|
stdout.puts "match : #{@match_file_count} files, #{match_count} hit"
|
40
40
|
end
|
41
41
|
|