-
Notifications
You must be signed in to change notification settings - Fork 7
/
rakefile
177 lines (156 loc) · 3.87 KB
/
rakefile
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
require "rake"
Dir.chdir __dir__
status_file = "ext/inc/status_codes.h"
makefile = "ext/Makefile"
extconf = "ext/extconf.rb"
desc "generate #{status_file}"
file status_file => __FILE__ do
puts "generating: #{status_file}"
require "nokogiri"
require "open-uri"
f = File.open status_file, 'w'
f.puts "#define HTTP_STATUS_CODES(XX)\\"
Nokogiri::XML(open("http://www.iana.org/assignments/http-status-codes/http-status-codes.xml")).css("record").each do |r|
value = r.css('value').text
next if value.index '-'
description = r.css('description').text
f.puts %Q| XX(#{value}, "#{description}");\\|
end
f.puts "// end define"
f.close
end
desc "generate makefile"
file makefile => [extconf, 'nyara.gemspec', __FILE__] do
Dir.chdir 'ext' do
sh 'make clean' if File.exist?('Makefile')
sh 'ruby extconf.rb'
end
end
desc "build ext"
task :build => makefile do
Dir.chdir 'ext' do
sh 'make'
end
end
desc "test"
task :test => :build do
sh 'rspec', '-c'
end
desc "build and test"
task :default => :test
desc "build and install gem"
task :gem do
Dir.glob('*.gem') do |f|
sh 'rm', f
end
# we need to run gem command without the mess of bundler
ENV['RUBYOPT'] &&= ENV['RUBYOPT'].gsub /\S*bundler\S*/, ''
sh 'gem', 'build', 'nyara.gemspec'
gem_package = Dir.glob('*.gem').first
sh 'gem', 'install', '-l', '--rdoc', '--ri', gem_package
end
desc "clean"
task :clean do
sh 'rm', '-f', '*.gem'
Dir.chdir 'ext' do
sh 'make', 'clean'
sh 'rm', '-f', 'Makefile'
end
end
desc "generate doc"
task "doc" do
sh 'yardoc'
sh 'open', '.doc/index.html'
end
# -- utils --
desc "collect line stat"
task :lines do
rb_c = 0
Dir.glob('**/*.rb') do |f|
rb_c += (File.read(f).count "\n")
end
c_c = 0
Dir.glob('ext/*.{c,cc,h}') do |f|
c_c += (File.read(f).count "\n")
end
spec_c = 0
Dir.glob('spec/**/*.rb') do |f|
spec_c += (File.read(f).count "\n")
end
puts "c: #{c_c} lines"
puts "rb: #{rb_c - spec_c} lines"
puts "spec: #{spec_c} lines"
end
desc "list Nyara::Ext methods"
task :list_ext do
require_relative "lib/nyara/nyara"
puts_methods = lambda{|methods|
methods.each do |m|
puts "#{m} /#{Nyara::Ext.method(m).arity}"
end
}
methods = (Nyara::Ext.methods - Module.methods).sort
[/queue/, /route/, /parse/, /request/].each do |r|
group = methods.grep r
puts_methods[group]
puts
methods -= group
end
puts_methods[methods]
end
def term_color n
print "\e[38;5;#{n}m"
end
def reset_color
print "\e[00m"
end
desc "audit arity of rb_define_method/rb_define_singleton_method"
task :audit_arity do
Dir.glob 'ext/*.{c,cc}' do |f|
puts "validating #{f}"
arities = {}
data = File.read f
data.scan /^(?:static )?VALUE (\w+)\((.+)\)/ do |func, params|
arities[func] = params.count(',')
puts " scan: #{func}/#{arities[func]}"
end
data.scan /rb_define(?:_singleton)?_method\(.*?(\w+)\s*\,\s*(\d+)\)/ do |func, arity|
print " check: #{func}/#{arity} "
if arities[func].nil?
term_color 5
print "UNSCANNED"
reset_color
puts
elsif arities[func] != arity.to_i
term_color 9
print "MISMATCH #{arities[func]}"
reset_color
puts
else
puts "OK"
end
end
end
end
desc "audit http_parser / multipart_parser struct size, they should be dividable by 8 so that Request fields are aligned"
task :audit_sizeof_parsers do
require "mkmf"
$CFLAGS << " -Iext/http-parser -Iext/multipart-parser-c"
{
'http_parser' => 'http_parser.h',
'multipart_parser*' => 'multipart_parser.h' # multipart_parser is opaque -_-
}.each do |struct, header|
have_header header
res = check_sizeof(struct, header)
if res and res % 8 == 0
puts "OK"
else
term_color 9
print "Need padding"
reset_color
puts
end
puts
end
sh 'rm', 'mkmf.log'
end