milkode 1.0.0 → 1.0.2
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 +9 -0
- data/HISTORY.rdoc +9 -0
- data/VERSION +1 -1
- data/bin/gmilk +1 -1
- data/bin/milk +1 -1
- data/lib/milkode/cdweb/app.rb +1 -1
- data/lib/milkode/common/util.rb +10 -0
- data/lib/milkode/grep/cli_grep.rb +83 -8
- data/lib/milkode/grep/findgrep.rb +7 -5
- data/milkode.gemspec +2 -2
- data/test/test_cli_grep.rb +7 -0
- metadata +4 -4
data/HISTORY.ja.rdoc
CHANGED
data/HISTORY.rdoc
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
=== 1.0.2 2013/06/08
|
2
|
+
|
3
|
+
* gmilk
|
4
|
+
* Add feature to accelerate search speed with an external tool file search
|
5
|
+
* Add -m(--match-files) option
|
6
|
+
* Add -e (-external-tool) option
|
7
|
+
* Search using "-e grep" when the number of records is large
|
8
|
+
* Add --no-external option
|
9
|
+
|
1
10
|
=== 1.0.0 2013/05/21
|
2
11
|
|
3
12
|
* milk web
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.2
|
data/bin/gmilk
CHANGED
data/bin/milk
CHANGED
data/lib/milkode/cdweb/app.rb
CHANGED
data/lib/milkode/common/util.rb
CHANGED
@@ -4,6 +4,7 @@ require 'rubygems'
|
|
4
4
|
require 'fileutils'
|
5
5
|
require 'pathname'
|
6
6
|
require 'kconv'
|
7
|
+
require 'open3'
|
7
8
|
|
8
9
|
module Milkode
|
9
10
|
module Util
|
@@ -230,6 +231,15 @@ module Milkode
|
|
230
231
|
table.sort(keys, options)
|
231
232
|
end
|
232
233
|
end
|
234
|
+
|
235
|
+
# 指定したコマンドが存在するか?
|
236
|
+
def exist_command?(command)
|
237
|
+
begin
|
238
|
+
Open3.capture3("type #{command}")[2].exited?
|
239
|
+
rescue Errno::ENOENT
|
240
|
+
false
|
241
|
+
end
|
242
|
+
end
|
233
243
|
end
|
234
244
|
end
|
235
245
|
|
@@ -7,9 +7,12 @@ require 'milkode/common/dbdir'
|
|
7
7
|
require 'milkode/common/util'
|
8
8
|
require 'milkode/grep/findgrep_option'
|
9
9
|
require 'optparse'
|
10
|
+
require 'tempfile'
|
10
11
|
|
11
12
|
module Milkode
|
12
13
|
class CLI_Grep
|
14
|
+
AUTO_EXTERNAL_RECORD_NUM = 500
|
15
|
+
|
13
16
|
def self.execute(stdout, arguments=[])
|
14
17
|
# 引数の文字コードをUTF-8に変換
|
15
18
|
if (Util::platform_win?)
|
@@ -63,15 +66,18 @@ Gotoline:
|
|
63
66
|
Normal:
|
64
67
|
EOF
|
65
68
|
opt.on('-a', '--all', 'Search all package.') {|v| my_option[:all] = true }
|
66
|
-
opt.on('-c', '--count', '
|
69
|
+
opt.on('-c', '--count', 'Display count num.') {|v| my_option[:count] = true }
|
67
70
|
opt.on('--cache', 'Search only db.') {|v| option.groongaOnly = true }
|
68
71
|
opt.on('--color', 'Color highlight.') {|v| option.colorHighlight = true}
|
69
72
|
opt.on('--cs', '--case-sensitive', 'Case sensitivity.') {|v| option.caseSensitive = true }
|
70
73
|
opt.on('-d DIR', '--directory DIR', 'Start directory. (deafult:".")') {|v| current_dir = File.expand_path(v); my_option[:find_mode] = true}
|
71
74
|
opt.on('--db DB_DIR', "Specify dbdir. (Use often with '-a')") {|v| option.dbFile = Dbdir.groonga_path(v) }
|
75
|
+
opt.on('-e GREP', '--external-tool GREP', "Use external tool for file search. (e.g. grep, ag)") {|v| my_option[:external_tool] = v}
|
72
76
|
opt.on('-f FILE_PATH', '--file-path FILE_PATH', 'File path. (Enable multiple call)') {|v| option.filePatterns << v; my_option[:find_mode] = true }
|
73
77
|
opt.on('-i', '--ignore', 'Ignore case.') {|v| option.ignoreCase = true}
|
78
|
+
opt.on('-m', '--match-files', 'Display match files.') {|v| my_option[:match_files] = true}
|
74
79
|
opt.on('-n NUM', 'Limits the number of match to show.') {|v| option.matchCountLimit = v.to_i }
|
80
|
+
opt.on('--no-external', 'Disable auto external.') {|v| my_option[:no_external] = true }
|
75
81
|
opt.on('--no-snip', 'There being a long line, it does not snip.') {|v| option.noSnip = true }
|
76
82
|
opt.on('-p PACKAGE', '--package PACKAGE', 'Specify search package.') {|v| setup_package(option, my_option, v) }
|
77
83
|
opt.on('-r', '--root', 'Search from package root.') {|v| current_dir = package_root_dir(File.expand_path(".")); my_option[:find_mode] = true }
|
@@ -150,12 +156,31 @@ EOF
|
|
150
156
|
require 'milkode/grep/findgrep'
|
151
157
|
|
152
158
|
if (my_option[:count])
|
153
|
-
# count mode
|
154
159
|
option.isSilent = true
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
160
|
+
stdout.puts "#{pickup_records(arguments, option).size} records"
|
161
|
+
|
162
|
+
elsif (my_option[:external_tool])
|
163
|
+
option.isSilent = true
|
164
|
+
records = pickup_records(arguments, option)
|
165
|
+
|
166
|
+
case my_option[:external_tool]
|
167
|
+
when 'grep'
|
168
|
+
search_external_tool(arguments, option, records, 'grep -n', 'grep')
|
169
|
+
when 'ag'
|
170
|
+
search_external_tool(arguments, option, records, 'ag', 'ag')
|
171
|
+
else
|
172
|
+
search_external_tool(arguments, option, records, my_option[:external_tool], my_option[:external_tool])
|
173
|
+
end
|
174
|
+
|
175
|
+
elsif (my_option[:match_files])
|
176
|
+
option.isSilent = true
|
177
|
+
records = pickup_records(arguments, option)
|
178
|
+
files = pickup_files(records, '\ ', option.matchCountLimit)
|
179
|
+
|
180
|
+
files.each do |filename|
|
181
|
+
stdout.puts filename
|
182
|
+
end
|
183
|
+
|
159
184
|
elsif my_option[:gotoline_data]
|
160
185
|
# gotoline mode
|
161
186
|
basePatterns = option.filePatterns
|
@@ -176,9 +201,22 @@ EOF
|
|
176
201
|
findGrep.searchAndPrint(stdout)
|
177
202
|
end
|
178
203
|
else
|
179
|
-
# search
|
204
|
+
# normal search
|
180
205
|
findGrep = FindGrep.new(arguments, option)
|
181
|
-
findGrep.
|
206
|
+
records = findGrep.pickupRecords
|
207
|
+
|
208
|
+
if (my_option[:no_external] || records.length < AUTO_EXTERNAL_RECORD_NUM)
|
209
|
+
findGrep.searchAndPrint2(stdout, records)
|
210
|
+
else
|
211
|
+
# レコード数が多い時は"-e grep"で検索
|
212
|
+
if Util::exist_command?('grep') && Util::exist_command?('xargs')
|
213
|
+
$stderr.puts "Number of records is large. Use auto external tool (gmilk -e grep)"
|
214
|
+
search_external_tool(arguments, option, records, 'grep -n', 'grep')
|
215
|
+
else
|
216
|
+
findGrep.searchAndPrint2(stdout, records)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
182
220
|
end
|
183
221
|
end
|
184
222
|
else
|
@@ -188,6 +226,43 @@ EOF
|
|
188
226
|
|
189
227
|
private
|
190
228
|
|
229
|
+
def self.search_external_tool(arguments, option, records, first_command, second_command)
|
230
|
+
files = pickup_files(records, '\\\\ ', option.matchCountLimit)
|
231
|
+
|
232
|
+
unless files.empty?
|
233
|
+
cmd = []
|
234
|
+
|
235
|
+
tmpfile = Tempfile.open("gmilk_external_tool")
|
236
|
+
tmpfile.binmode
|
237
|
+
tmpfile.write(files.join("\n"))
|
238
|
+
tmpfile.close
|
239
|
+
tmpfile.open
|
240
|
+
cmd << "cat #{tmpfile.path}"
|
241
|
+
|
242
|
+
cmd << "xargs #{first_command} #{arguments[0]}"
|
243
|
+
|
244
|
+
(1...arguments.size).each do |index|
|
245
|
+
cmd << "#{second_command} #{arguments[index]}"
|
246
|
+
end
|
247
|
+
|
248
|
+
system(cmd.join(" | "))
|
249
|
+
|
250
|
+
tmpfile.close(true)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def self.pickup_records(arguments, option)
|
255
|
+
FindGrep.new(arguments, option).pickupRecords
|
256
|
+
end
|
257
|
+
|
258
|
+
def self.pickup_files(records, conv_space, length = -1)
|
259
|
+
files = []
|
260
|
+
records.each do |r|
|
261
|
+
files << r.path.gsub(' ', conv_space) if File.exist?(r.path)
|
262
|
+
end
|
263
|
+
(length > 0) ? files[0, length] : files
|
264
|
+
end
|
265
|
+
|
191
266
|
def self.setup_package(option, my_option, keyword)
|
192
267
|
# @memo package指定が簡単になった
|
193
268
|
# dirs = yaml_load.contents.find_all {|p| p.name.include? keyword }.map{|p| p.directory}
|
@@ -109,10 +109,15 @@ module Milkode
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def searchAndPrint(stdout)
|
112
|
+
records = searchDatabase
|
113
|
+
searchAndPrint2(stdout, records)
|
114
|
+
end
|
115
|
+
|
116
|
+
def searchAndPrint2(stdout, records)
|
112
117
|
unless (@option.dbFile)
|
113
118
|
searchFromDir(stdout, @option.directory, 0)
|
114
119
|
else
|
115
|
-
searchFromDB(stdout, @option.directory)
|
120
|
+
searchFromDB(stdout, records, @option.directory)
|
116
121
|
end
|
117
122
|
|
118
123
|
@result.time_stop
|
@@ -153,10 +158,7 @@ module Milkode
|
|
153
158
|
Gren::Util::time_s(@result.time)
|
154
159
|
end
|
155
160
|
|
156
|
-
def searchFromDB(stdout, dir)
|
157
|
-
# データベースを検索
|
158
|
-
records = searchDatabase
|
159
|
-
|
161
|
+
def searchFromDB(stdout, records, dir)
|
160
162
|
# ヒットしたレコード数
|
161
163
|
stdout.puts "Found : #{records.size} records." if (!@option.dispHtml && !@option.isSilent)
|
162
164
|
|
data/milkode.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{milkode}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["ongaeshi"]
|
12
|
-
s.date = %q{2013-
|
12
|
+
s.date = %q{2013-06-08}
|
13
13
|
s.description = %q{Line based local source code search engine & grep-command & web-app.}
|
14
14
|
s.email = %q{ongaeshi0621@gmail.com}
|
15
15
|
s.executables = ["gmilk", "milk"]
|
data/test/test_cli_grep.rb
CHANGED
@@ -26,6 +26,7 @@ class TestCLI_Grep < Test::Unit::TestCase
|
|
26
26
|
t_case_sensitive
|
27
27
|
t_keyword
|
28
28
|
t_db
|
29
|
+
t_match_files
|
29
30
|
end
|
30
31
|
|
31
32
|
def teardown
|
@@ -98,6 +99,12 @@ class TestCLI_Grep < Test::Unit::TestCase
|
|
98
99
|
io = StringIO.new
|
99
100
|
CLI_Grep.execute(io, "--db #{@work.path("db1")} -a db_dir_expand".split)
|
100
101
|
end
|
102
|
+
|
103
|
+
def t_match_files
|
104
|
+
io = StringIO.new
|
105
|
+
CLI_Grep.execute(io, "-m -p a_project default".split)
|
106
|
+
assert_equal 1, io.string.split("\n").count
|
107
|
+
end
|
101
108
|
end
|
102
109
|
|
103
110
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: milkode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- ongaeshi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-06-08 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|