-
Notifications
You must be signed in to change notification settings - Fork 4
/
Guardfile
71 lines (59 loc) · 2.26 KB
/
Guardfile
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
require 'bitclust'
require 'open3'
require 'shellwords'
rurema_root = 'doctree'
directories %W(#{rurema_root}/refm/api/src public)
def generate_rdoc(rd_path)
o, s = Open3.capture2e('git diff -U0 ' + rd_path.shellescape)
unless s.success?
puts o
return
end
return unless m = o.match(/@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/)
changed_line = m[1].to_i + 1 # @sinceなどで変更の1行目がコメントアウトされているとうまくメソッド名を探せないので+1しておく
prev = File.foreach(rd_path).take(changed_line)
method = prev.reverse_each.detect { |line| line.start_with?('---') }
method = BitClust::MethodSignature.parse(method).name if method
klass = prev.reverse_each.detect do |line|
next unless line.start_with?('= ')
m = line.match(/^= (?:class|module) ([^ \n]+) ?/)
break m && m[1]
end
target = prev.reverse_each.detect do |line|
next unless line.start_with?('== ')
if line.include?('Singleton Methods') || line.include?('Class Methods')
break "#{klass}.#{method}"
elsif line.include?('Module Functions')
break "#{klass}.##{method}"
elsif line.include?('Instance Methods') || line.include?('== Methods')
break "#{klass}##{method}"
elsif line.include?('Constants')
break "#{klass}::#{method}"
else
break
end
end
template = File.join(__dir__, 'template').shellescape
if klass && method && target
o, s = Open3.capture2e('bitclust htmlfile --baseurl=http://localhost:9292 --templatedir=' + template + ' --target=' + target.shellescape + ' --ruby=2.4.0 ' + rd_path.shellescape)
raise "Please report the issue: target=#{target}\n#{o}" unless s.success?
else
o, s = Open3.capture2e('bitclust htmlfile --baseurl=http://localhost:9292 --templatedir=' + template + ' --ruby=2.4.0 ' + rd_path.shellescape)
end
File.write(File.join(__dir__, 'public', 'index.html'), o)
end
watch(%r{^#{rurema_root}/(?<rd_path>.*)}) do |m|
Dir.chdir(rurema_root) do
generate_rdoc(m[:rd_path]) unless File.basename(m[:rd_path]).start_with?('.')
end
end
guard(:livereload) do
watch('public/index.html')
end
guard(:rack)
File.write(File.join('public', 'index.html'), <<HTML)
<html>
<head><title>るりま書く</title></head>
<body>るりま書く</body>
</html>
HTML