This repository has been archived by the owner on Jul 27, 2021. It is now read-only.
/
myrurema.rb
235 lines (196 loc) · 5.17 KB
/
myrurema.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
require 'optparse'
require 'pathname'
require 'shellwords'
require 'tmpdir'
require 'launchy'
require 'myrurema/options'
require 'myrurema/version'
# Extend Pathname so that you can write
# foo / "bar" #=> Pathname("foo/bar")
# instead of
# foo + "bar"
class Pathname; alias / +; end
class MyRurema
SVN_URL = "http://jp.rubyist.net/svn/rurema"
def initialize(opt=Options.new(ARGV))
@opt = opt
end
def run
if @opt.command
send(@opt.command)
else
query = @opt.rest_args
num = if !query.empty? and query.last =~ /\A\d+\z/
query.pop.to_i
else
nil
end
case
when query.empty?
if num
search(num, @opt.rubyver)
else
@opt.usage
end
when query && num
search_num(query, num, @opt.rubyver)
else
search(query, @opt.rubyver)
end
end
end
def init
sh "svn co -rHEAD #{SVN_URL}/doctree/trunk #{doctree_path}"
sh "svn co -rHEAD #{SVN_URL}/bitclust/trunk #{bitclust_path}"
init_db(@opt.rubyver)
end
def update
sh "svn up #{doctree_path}"
refresh_db(@opt.rubyver)
end
# - query: Array or String
# - ver: String
def search(query, ver)
should_have_db(ver)
args = Array(query).map{|s| Shellwords.escape s}.join(" ")
cmd = "#{bitclust_path/'bin/refe'}" +
" #{args} -d #{db_path(ver)}"
sh cmd, :silent => true do |txt|
lines = txt.lines.to_a
if lines.count < 10 and
lines.first(2).join =~ /#{query}.*#{query}/m and
lines[1] !~ /^--- / and # ad hoc :-( see Issue #2
!@opt.no_ask
words = {}
k = 0
puts lines.map{|line|
line.gsub(/(\S+)/){|str|
k+=1
words[k] = str
"(#{k})#{str}"
}
}
print "which one? > "
line = $stdin.gets or (puts; exit)
n = line.to_i
puts "searching #{words[n]}"
puts
search(words[n].sub(/\.#/, "."), ver)
else
puts txt
end
end
end
def search_num(query, num, ver)
should_have_db(ver)
result = `#{bitclust_path/'bin/refe'} #{query} -d #{db_path(ver)}`
word = result.split[num-1]
if word
word.gsub!(/\.#/, ".") # avoid multi-hit for a module function
puts "searching #{word}"
search(word, ver)
else
error "less than #{num} entries found"
end
end
def list
should_have_db(@opt.rubyver)
sh "#{bitclust_path/'bin/refe'}" +
" -l -d #{db_path(@opt.rubyver)}", :silent => true
end
def server
port = @opt.port || default_port(@opt.rubyver)
th = Thread.new{
sh "ruby #{bitclust_path/'standalone.rb'}" +
" --srcdir=#{bitclust_path}" +
" --baseurl=http://localhost:#{port}" +
" --port=#{port}" +
" --database=#{db_path(@opt.rubyver)}" +
" --debug" # needed to avoid the server running as daemon :-(
}
url = "http://localhost:#{port}/view/"
puts "Starting BitClust server .."
puts "Open #{url} in your browser."
puts
if @opt.open_browser
sleep 1 # wait for the server to start
Launchy.open(url)
end
th.join
end
TMP_FILE = Pathname(Dir.tmpdir)/'rurema_preview.html'
def preview
file, target = *@opt.rest_args
if file
error "file not found: #{file}" unless File.exist?(file)
result = sh "#{bitclust_path/'tools/bc-tohtml.rb'}" +
" #{file}" +
(target ? " --target=#{target}" : "") +
" --ruby=#{@opt.rubyver}" +
" > #{TMP_FILE}"
if result && @opt.open_browser
cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open"
sh "#{cmd} #{TMP_FILE}"
end
else
sh "cd #{doctree_path/'refm/api/src'}"
end
end
private
def default_port(ver)
"10" + ver.scan(/\d/).join
end
def init_db(ver)
sh "ruby -I #{bitclust_path/'lib'}" +
" #{bitclust_path/'bin/bitclust'}" +
" -d #{db_path(ver)} init version=#{ver} encoding=euc-jp"
refresh_db(ver)
end
def refresh_db(ver)
puts "Updating Rurema database:"
puts "This will take a few minutes. Please be patient."
sh "ruby -I #{bitclust_path/'lib'}" +
" #{bitclust_path/'bin/bitclust'}" +
" -d #{db_path(ver)}" +
" update --stdlibtree=#{doctree_path/'refm/api/src'}"
end
def bitclust_path
@opt.ruremadir / "bitclust"
end
def doctree_path
@opt.ruremadir / "doctree"
end
def should_have_db(ver)
unless has_db?(ver)
puts "You don't have a database for ruby #{ver}."
puts "Make it now? [y/n]"
if $stdin.gets.chomp.downcase == "y"
init_db(ver)
else
exit
end
end
end
def has_db?(ver)
db_path(ver).directory?
end
def db_path(ver)
@opt.ruremadir / "db" / ver
end
def sh(cmd, opt={}, &block)
puts cmd unless opt[:silent]
return if @opt.dry_run
if block
block.call(`#{cmd}`)
else
system cmd
end
end
def error(msg)
$stderr.puts msg unless $MYRUREMA_TEST
exit
end
def puts(str="")
Kernel.puts(str) unless $MYRUREMA_TEST
end
end