proxy_pass: document as a public API
[yahns.git] / Rakefile
blob65862f2abdbb07ae756666a44fe91aea3ab646db
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/")
62 end
64 def tags
65   timefmt = '%Y-%m-%dT%H:%M:%SZ'
66   @tags ||= `git tag -l`.split(/\n/).map do |tag|
67     if %r{\Av[\d\.]+} =~ tag
68       header, subject, body = `git cat-file tag #{tag}`.split(/\n\n/, 3)
69       header = header.split(/\n/)
70       tagger = header.grep(/\Atagger /).first
71       body ||= "initial"
72       time = Time.at(tagger.split(/ /)[-2].to_i).utc
73       {
74         time_obj: time,
75         time: time.strftime(timefmt),
76         tagger_name: %r{^tagger ([^<]+)}.match(tagger)[1].strip,
77         tagger_email: %r{<([^>]+)>}.match(tagger)[1].strip,
78         id: `git rev-parse refs/tags/#{tag}`.chomp!,
79         tag: tag,
80         subject: subject,
81         body: body,
82       }
83     end
84   end.compact.sort { |a,b| b[:time] <=> a[:time] }
85 end
87 def xml(dst, tag, text = nil, attrs = nil)
88   if Hash === text
89     attrs = text
90     text = nil
91   end
92   if attrs
93     attrs = attrs.map { |k,v| "#{k}=#{v.encode(xml: :attr)}" }
94     attrs = "\n#{attrs.join("\n")}"
95   end
96   case text
97   when nil
98     if block_given?
99       dst << "<#{tag}#{attrs}>"
100       yield
101       dst << "</#{tag}>"
102     else
103       dst << "<#{tag}#{attrs}/>"
104     end
105   else
106     dst << "<#{tag}#{attrs}>#{text.encode(xml: :text)}</#{tag}>"
107   end
110 desc 'prints news as an Atom feed'
111 task "NEWS.atom.xml" do
112   require 'uri'
113   cgit_uri = URI('https://yhbt.net/yahns.git')
114   uri = URI('https://yhbt.net/yahns/')
115   new_tags = tags[0,10]
116   time = nil
117   project_name = 'yahns'
118   short_desc = File.readlines('README')[0].split(' - ')[0]
119   new_tags = tags[0,10]
120   atom_uri = uri.dup
121   atom_uri.path += 'NEWS.atom.xml'
122   news_uri = uri.dup
123   news_uri.path += 'NEWS.html'
124   dst = ''
125   xml(dst, 'feed', xmlns: 'http://www.w3.org/2005/Atom') do
126     xml(dst, 'id', atom_uri.to_s)
127     xml(dst, 'title', "#{project_name} news")
128     xml(dst, 'subtitle', short_desc)
129     xml(dst, 'link', rel: 'alternate', type: 'text/html', href: news_uri.to_s)
130     xml(dst, 'updated', new_tags.empty? ? '1970-01-01:00:00:00Z'
131                                       : new_tags[0][:time])
132     new_tags.each do |tag|
133       xml(dst, 'entry') do
134         xml(dst, 'title', tag[:subject])
135         xml(dst, 'updated', tag[:time])
136         xml(dst, 'published', tag[:time])
137         xml(dst, 'author') do
138           xml(dst, 'name', tag[:tagger_name])
139           xml(dst, 'email', tag[:tagger_email])
140         end
141         uri = cgit_uri.dup
142         uri.path += '/tag/'
143         uri.query = "id=#{tag[:tag]}"
144         uri = uri.to_s
145         xml(dst, 'link', rel: 'alternate', type: 'text/html', href: uri)
146         xml(dst, 'id', uri)
147         xml(dst, 'content', type: 'xhtml') do
148           xml(dst, 'div', xmlns: 'http://www.w3.org/1999/xhtml') do
149             xml(dst, 'pre', tag[:body])
150           end # div
151         end # content
152       end # entry
153     end # new_tags.each
154   end # feed
156   fp = Tempfile.new('NEWS.atom.xml', '.')
157   fp.sync = true
158   fp.write(dst)
159   fp.chmod 0644
160   File.utime(time, time, fp.path) if time
161   File.rename(fp.path, 'NEWS.atom.xml')
162   fp.close!
165 desc 'prints news as a text file'
166 task 'NEWS' do
167   fp = Tempfile.new('NEWS', '.')
168   fp.sync = true
169   time = nil
170   tags.each do |tag|
171     time ||= tag[:time_obj]
172     line = tag[:subject] + ' / ' + tag[:time].sub(/T.*/, '')
173     fp.puts line
174     fp.puts('-' * line.length)
175     fp.puts
176     fp.puts tag[:body]
177     fp.puts
178   end
179   fp.write("Unreleased\n\n") unless fp.size > 0
180   fp.puts "COPYRIGHT"
181   fp.puts "---------"
182   fp.puts "Copyright (C) 2013-2017 all contributors <yahns-public@yhbt.net>"
183   fp.puts "License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>"
184   fp.chmod 0644
185   File.utime(time, time, fp.path) if time
186   File.rename(fp.path, 'NEWS')
187   fp.close!