summary refs log tree commit
path: root/Rakefile
blob: af3a811ccdfcb0631a75aefa1968ee2212b15b8d (plain)
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
# Copyright (C) 2013-2019 all contributors <dtas-all@nongnu.org>.
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true
require 'tempfile'
include Rake::DSL

def tags
  timefmt = '%Y-%m-%dT%H:%M:%SZ'
  @tags ||= `git tag -l --sort=-v:refname`.split(/\n/).map do |tag|
    if %r{\Av[\d\.]+} =~ tag
      header, subject, body = `git cat-file tag #{tag}`.split(/\n\n/, 3)
      header = header.split(/\n/)
      tagger = header.grep(/\Atagger /).first
      {
        time: Time.at(tagger.split(' ')[-2].to_i).utc.strftime(timefmt),
        tagger_name: %r{^tagger ([^<]+)}.match(tagger)[1].strip,
        tagger_email: %r{<([^>]+)>}.match(tagger)[1].strip,
        id: `git rev-parse refs/tags/#{tag}`.chomp!,
        tag: tag,
        subject: subject,
        body: body,
      }
    end
  end.compact.sort { |a,b| b[:time] <=> a[:time] }
end

task "NEWS" do
  latest = nil
  fp = Tempfile.new("NEWS", ".")
  fp.sync = true
  tags.each do |tag|
    version = tag[:tag].delete 'v'
    fp.puts "# #{version} / #{tag[:time].split('T')[0]}"
    fp.puts
    fp.puts tag[:subject]
    body = tag[:body]
    if body && body.strip.size > 0
      fp.puts "\n\n#{body}"
    end
    fp.puts
  end
  fp.puts "Unreleased" unless fp.size > 0
  fp.puts "# COPYRIGHT"
  fp.puts "Copyright (C) 2013-2015 all contributors <dtas-all@nongnu.org>"
  fp.puts "License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>"
  fp.rewind
  assert_equal fp.read, File.read("NEWS") rescue nil
  fp.chmod 0644
  File.rename(fp.path, "NEWS")
end

desc 'prints news as an Atom feed'
task 'NEWS.atom' do
  require 'builder' # gem install builder
  url_base = 'https://80x24.org/dtas/'
  cgit_url = 'https://80x24.org/dtas.git/'
  new_tags = tags[0,10]
  x = Builder::XmlMarkup.new
  x.instruct! :xml, encoding: 'UTF-8', version: '1.0'
  x.feed(xmlns: 'http://www.w3.org/2005/Atom') do
    x.id "#{url_base}/NEWS.atom"
    x.title "dtas news"
    x.subtitle 'duct tape audio suite for *nix'
    x.link rel: 'alternate', type: 'text/plain', href: "#{url_base}/NEWS"
    x.updated(new_tags.empty? ? "1970-01-01T00:00:00Z" : new_tags.first[:time])
    new_tags.each do |tag|
      x.entry do
        x.title tag[:subject]
        x.updated tag[:time]
        x.published tag[:time]
        x.author {
          x.name tag[:tagger_name]
          x.email tag[:tagger_email]
        }
        url = "#{cgit_url}/tag/?id=#{tag[:tag]}"
        x.link rel: 'alternate', type: 'text/html', href: url
        x.id url
        x.content(type: :xhtml) do
          x.div(xmlns: 'http://www.w3.org/1999/xhtml') do
            x.pre tag[:body]
          end
        end
      end
    end
  end

  fp = Tempfile.new(%w(NEWS .atom), ".")
  fp.sync = true
  fp.puts x.target!
  fp.chmod 0644
  File.rename fp.path, 'NEWS.atom'
  fp.close!
end

task rsync_docs: %w(NEWS NEWS.atom) do
  dest = ENV["RSYNC_DEST"] || "80x24.org:/srv/80x24/dtas/"
  top = %w(INSTALL NEWS README COPYING NEWS.atom)
  files = []

  # git-set-file-times is distributed with rsync,
  # Also available at: https://yhbt.net/git-set-file-times
  # on Debian systems: /usr/share/doc/rsync/scripts/git-set-file-times.gz
  sh("git", "set-file-times", "Documentation", "examples", *top)
  make = ENV['MAKE'] || 'make'
  sh(%Q(#{make} -C Documentation))
  examples = `git ls-files examples`.split("\n")

  gzip_touch = lambda do |txt|
    gz = "#{txt}.gz"
    tmp = "#{gz}.#$$"
    sh("gzip -9 <#{txt} >#{tmp}")
    st = File.stat(txt)
    File.utime(st.atime, st.mtime, tmp) # make nginx gzip_static happy
    File.rename(tmp, gz)
    gz
  end

  Dir['Documentation/*.txt'].to_a.concat(top).each do |txt|
    files << txt
    files << gzip_touch[txt]
  end
  sh("rsync --chmod=Fugo=r -av #{files.join(' ')} #{dest}")

  examples = examples.map { |txt| [ txt, gzip_touch[txt] ] }
  sh("rsync --chmod=Fugo=r -av #{examples.join(' ')} #{dest}/examples/")
end