test/test_extras_exec_cgi: drop Rack::ContentLength
[yahns.git] / Rakefile
blob769356ef5ab1117bb23222ac5d28ff6321d16305
1 # Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
2 # License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
3 require 'tempfile'
4 include Rake::DSL
6 apidoc = {
7   'doc/Yahns.html' => 'lib/yahns.rb',
8   'doc/Yahns/ProxyPass.html' => 'lib/yahns/proxy_pass.rb'
11 task apidoc.keys[0] => apidoc.values do
12   rdoc = ENV['rdoc'] || 'rdoc'
13   system("git", "set-file-times", *(apidoc.values))
14   sh "#{rdoc} -f dark216" # dark216 requires olddoc 1.7+
16   apidoc.each do |dst, src|
17     src = File.stat(src)
18     File.utime(src.atime, src.mtime, dst)
19   end
20 end
22 gendocs = %W(NEWS NEWS.atom.xml #{apidoc.keys[0]})
23 task html: apidoc.keys[0]
24 task rsync_docs: gendocs do
25   dest = ENV["RSYNC_DEST"] || "yhbt.net:/srv/yhbt/yahns/"
26   top = %w(INSTALL HACKING README COPYING)
28   # git-set-file-times is distributed with rsync,
29   # Also available at: https://yhbt.net/git-set-file-times
30   # on Debian systems: /usr/share/doc/rsync/scripts/git-set-file-times.gz
31   sh("git", "set-file-times", "Documentation", "examples", *top)
32   make = ENV['MAKE'] || 'make'
33   sh(%Q(#{make} -C Documentation))
35   do_gzip = lambda do |txt|
36     gz = "#{txt}.gz"
37     tmp = "#{gz}.#$$"
38     sh("gzip --rsyncable -9 < #{txt} > #{tmp}")
39     st = File.stat(txt)
40     File.utime(st.atime, st.mtime, tmp) # make nginx gzip_static happy
41     File.rename(tmp, gz)
42     gz
43   end
45   files = `git ls-files Documentation/*.txt`.split(/\n/)
46   files.concat(top)
47   files.concat(gendocs)
48   files.concat(%w(doc/Yahns.html))
49   files.concat(%w(yahns yahns-rackup yahns_config).map! { |x|
50     "Documentation/#{x}.txt"
51   })
52   gzfiles = files.map { |txt| do_gzip.call(txt) }
53   files.concat(gzfiles)
55   sh("rsync --chmod=Fugo=r -av #{files.join(' ')} #{dest}")
57   examples = `git ls-files examples`.split(/\n/)
58   gzex = examples.map { |txt| do_gzip.call(txt) }
59   examples.concat(gzex)
61   sh("rsync --chmod=Fugo=r -av #{examples.join(' ')} #{dest}/examples/")
63   rdoc = apidoc.keys.grep(%r{\Adoc/Yahns/})
64   gzex = rdoc.map { |txt| do_gzip.call(txt) }
65   examples.concat(gzex)
66   sh("rsync --chmod=Fugo=r -av #{rdoc.join(' ')} #{dest}/Yahns/")
67 end
69 def tags
70   timefmt = '%Y-%m-%dT%H:%M:%SZ'
71   @tags ||= `git tag -l`.split(/\n/).map do |tag|
72     if %r{\Av[\d\.]+} =~ tag
73       header, subject, body = `git cat-file tag #{tag}`.split(/\n\n/, 3)
74       header = header.split(/\n/)
75       tagger = header.grep(/\Atagger /).first
76       body ||= "initial"
77       time = Time.at(tagger.split(/ /)[-2].to_i).utc
78       {
79         time_obj: time,
80         time: time.strftime(timefmt),
81         tagger_name: %r{^tagger ([^<]+)}.match(tagger)[1].strip,
82         tagger_email: %r{<([^>]+)>}.match(tagger)[1].strip,
83         id: `git rev-parse refs/tags/#{tag}`.chomp!,
84         tag: tag,
85         subject: subject,
86         body: body,
87       }
88     end
89   end.compact.sort { |a,b| b[:time] <=> a[:time] }
90 end
92 def xml(dst, tag, text = nil, attrs = nil)
93   if Hash === text
94     attrs = text
95     text = nil
96   end
97   if attrs
98     attrs = attrs.map { |k,v| "#{k}=#{v.encode(xml: :attr)}" }
99     attrs = "\n#{attrs.join("\n")}"
100   end
101   case text
102   when nil
103     if block_given?
104       dst << "<#{tag}#{attrs}>"
105       yield
106       dst << "</#{tag}>"
107     else
108       dst << "<#{tag}#{attrs}/>"
109     end
110   else
111     dst << "<#{tag}#{attrs}>#{text.encode(xml: :text)}</#{tag}>"
112   end
115 desc 'prints news as an Atom feed'
116 task "NEWS.atom.xml" do
117   require 'uri'
118   cgit_uri = URI('https://yhbt.net/yahns.git')
119   uri = URI('https://yhbt.net/yahns/')
120   new_tags = tags[0,10]
121   time = nil
122   project_name = 'yahns'
123   short_desc = File.readlines('README')[0].split(' - ')[0]
124   new_tags = tags[0,10]
125   atom_uri = uri.dup
126   atom_uri.path += 'NEWS.atom.xml'
127   news_uri = uri.dup
128   news_uri.path += 'NEWS.html'
129   dst = ''
130   xml(dst, 'feed', xmlns: 'http://www.w3.org/2005/Atom') do
131     xml(dst, 'id', atom_uri.to_s)
132     xml(dst, 'title', "#{project_name} news")
133     xml(dst, 'subtitle', short_desc)
134     xml(dst, 'link', rel: 'alternate', type: 'text/html', href: news_uri.to_s)
135     xml(dst, 'updated', new_tags.empty? ? '1970-01-01:00:00:00Z'
136                                       : new_tags[0][:time])
137     new_tags.each do |tag|
138       xml(dst, 'entry') do
139         xml(dst, 'title', tag[:subject])
140         xml(dst, 'updated', tag[:time])
141         xml(dst, 'published', tag[:time])
142         xml(dst, 'author') do
143           xml(dst, 'name', tag[:tagger_name])
144           xml(dst, 'email', tag[:tagger_email])
145         end
146         uri = cgit_uri.dup
147         uri.path += '/tag/'
148         uri.query = "id=#{tag[:tag]}"
149         uri = uri.to_s
150         xml(dst, 'link', rel: 'alternate', type: 'text/html', href: uri)
151         xml(dst, 'id', uri)
152         xml(dst, 'content', type: 'xhtml') do
153           xml(dst, 'div', xmlns: 'http://www.w3.org/1999/xhtml') do
154             xml(dst, 'pre', tag[:body])
155           end # div
156         end # content
157       end # entry
158     end # new_tags.each
159   end # feed
161   fp = Tempfile.new('NEWS.atom.xml', '.')
162   fp.sync = true
163   fp.write(dst)
164   fp.chmod 0644
165   File.utime(time, time, fp.path) if time
166   File.rename(fp.path, 'NEWS.atom.xml')
167   fp.close!
170 desc 'prints news as a text file'
171 task 'NEWS' do
172   fp = Tempfile.new('NEWS', '.')
173   fp.sync = true
174   time = nil
175   tags.each do |tag|
176     time ||= tag[:time_obj]
177     line = tag[:subject] + ' / ' + tag[:time].sub(/T.*/, '')
178     fp.puts line
179     fp.puts('-' * line.length)
180     fp.puts
181     fp.puts tag[:body]
182     fp.puts
183   end
184   fp.write("Unreleased\n\n") unless fp.size > 0
185   fp.puts "COPYRIGHT"
186   fp.puts "---------"
187   fp.puts "Copyright (C) 2013-2017 all contributors <yahns-public@yhbt.net>"
188   fp.puts "License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>"
189   fp.chmod 0644
190   File.utime(time, time, fp.path) if time
191   File.rename(fp.path, 'NEWS')
192   fp.close!