|
1 | | -require 'open-uri' |
2 | | - |
3 | 1 | namespace :book do |
4 | | - def exec_or_raise(command) |
5 | | - puts `#{command}` |
6 | | - if (! $?.success?) |
7 | | - raise "[ERROR] '#{command}' failed" |
8 | | - end |
9 | | - end |
10 | | - |
11 | | - def generate_contributors_list(column_size) |
12 | | - # Generating preformatted contributors list... |
13 | | - `git shortlog -s HEAD | grep -v -E "(Straub|Chacon|dependabot)" | cut -f 2- | column -c #{column_size} > book/contributors.txt` |
14 | | - end |
15 | | - |
16 | | - def download_locale(locale_file) |
17 | | - locale_file_url = "https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/data/locale/#{locale_file}" |
18 | | - if not File.exist?(locale_file) |
19 | | - puts "Downloading locale attributes file..." |
20 | | - l10n_text = URI.open(locale_file_url).read |
21 | | - File.open(locale_file, 'w') { |file| file.puts l10n_text } |
22 | | - puts " -- Saved at #{locale_file}" |
23 | | - else |
24 | | - puts "Use existing file with locale attributes #{locale_file}" |
25 | | - end |
26 | | - end |
27 | 2 |
|
28 | 3 | # Variables referenced for build |
29 | | - lang = 'ru' |
30 | | - locale_file = "attributes-#{lang}.adoc" |
31 | | - date_string = Time.now.strftime('%d.%m.%Y') |
32 | | - |
33 | | - version_string = `git describe --tags`.chomp |
| 4 | + version_string = `git describe --tags --abbrev=0`.chomp |
34 | 5 | if version_string.empty? |
35 | 6 | version_string = '0' |
| 7 | + else |
| 8 | + versions = version_string.split('.') |
| 9 | + version_string = versions[0] + '.' + versions[1] + '.' + versions[2].to_i.next.to_s |
| 10 | + end |
| 11 | + date_string = Time.now.strftime('%Y-%m-%d') |
| 12 | + params = "--attribute revnumber='#{version_string}' --attribute revdate='#{date_string}'" |
| 13 | + header_hash = `git rev-parse --short HEAD`.strip |
| 14 | + |
| 15 | + # Check contributors list |
| 16 | + # This checks commit hash stored in the header of list against current HEAD |
| 17 | + def check_contrib |
| 18 | + if File.exist?('book/contributors.txt') |
| 19 | + current_head_hash = `git rev-parse --short HEAD`.strip |
| 20 | + header = `head -n 1 book/contributors.txt`.strip |
| 21 | + # Match regex, then coerce resulting array to string by join |
| 22 | + header_hash = header.scan(/[a-f0-9]{7,}/).join |
| 23 | + |
| 24 | + if header_hash == current_head_hash |
| 25 | + puts "Hash on header of contributors list (#{header_hash}) matches the current HEAD (#{current_head_hash})" |
| 26 | + else |
| 27 | + puts "Hash on header of contributors list (#{header_hash}) does not match the current HEAD (#{current_head_hash}), refreshing" |
| 28 | + sh "rm book/contributors.txt" |
| 29 | + # Reenable and invoke task again |
| 30 | + Rake::Task['book/contributors.txt'].reenable |
| 31 | + Rake::Task['book/contributors.txt'].invoke |
| 32 | + end |
| 33 | + end |
36 | 34 | end |
37 | | - params = "--attribute revnumber='#{version_string}' --attribute revdate='#{date_string}' --attribute lang=#{lang} " |
38 | | - ignore_urls = "'https://developer.github.com','https://developer.github.com/webhooks/','https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent','https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit','https://www.mercurial-scm.org/'" |
39 | 35 |
|
40 | | - # Tasks list |
41 | 36 | desc 'build basic book formats' |
42 | 37 | task :build => [:build_html, :build_epub, :build_fb2, :build_mobi, :build_pdf] do |
43 | 38 | begin |
44 | | - puts 'Validating generated files...' |
45 | | - Rake::Task['book:check'].invoke |
| 39 | + # Run check |
| 40 | + Rake::Task['book:check'].invoke |
| 41 | + |
| 42 | + # Rescue to ignore checking errors |
| 43 | + rescue => e |
| 44 | + puts e.message |
| 45 | + puts 'Error when checking books (ignored)' |
46 | 46 | end |
47 | 47 | end |
48 | 48 |
|
49 | | - desc 'prepare necessary data to start build' |
50 | | - task :prebuild, [:column_size] do |t, args| |
51 | | - args.with_defaults(:column_size => 72) |
| 49 | + desc 'build basic book formats (for ci)' |
| 50 | + task :ci => [:build_html, :build_epub, :build_fb2, :build_mobi, :build_pdf] do |
| 51 | + # Run check, but don't ignore any errors |
| 52 | + Rake::Task['book:check'].invoke |
| 53 | + end |
52 | 54 |
|
53 | | - download_locale(locale_file) |
54 | | - generate_contributors_list(args.column_size) |
| 55 | + desc 'generate contributors list' |
| 56 | + file 'book/contributors.txt' do |
| 57 | + puts 'Generating contributors list' |
| 58 | + sh "echo 'Contributors as of #{header_hash}:\n' > book/contributors.txt" |
| 59 | + sh "git shortlog -s HEAD | grep -v -E '(Straub|Chacon|dependabot)' | cut -f 2- | sort | column -c 120 >> book/contributors.txt" |
55 | 60 | end |
56 | 61 |
|
57 | 62 | desc 'build HTML format' |
58 | | - task :build_html do |
59 | | - Rake::Task['book:prebuild'].invoke(72) |
| 63 | + task :build_html => 'book/contributors.txt' do |
| 64 | + check_contrib() |
| 65 | + |
| 66 | + puts 'Converting to HTML...' |
| 67 | + sh "bundle exec asciidoctor #{params} -a data-uri progit.asc" |
| 68 | + puts ' -- HTML output at progit.html' |
60 | 69 |
|
61 | | - puts 'Converting to HTML...' |
62 | | - `bundle exec asciidoctor #{params} -a data-uri progit.asc` |
63 | | - puts ' -- HTML output at progit.html' |
64 | 70 | end |
65 | 71 |
|
66 | | - desc 'build EPUB format' |
67 | | - task :build_epub do |
68 | | - Rake::Task['book:prebuild'].invoke(48) |
| 72 | + desc 'build Epub format' |
| 73 | + task :build_epub => 'book/contributors.txt' do |
| 74 | + check_contrib() |
| 75 | + |
| 76 | + puts 'Converting to EPub...' |
| 77 | + sh "bundle exec asciidoctor-epub3 #{params} progit.asc" |
| 78 | + puts ' -- Epub output at progit.epub' |
69 | 79 |
|
70 | | - puts 'Converting to EPUB...' |
71 | | - `bundle exec asciidoctor-epub3 #{params} progit.asc` |
72 | | - puts ' -- EPUB output at progit.epub' |
73 | 80 | end |
74 | 81 |
|
75 | 82 | desc 'build FB2 format' |
76 | | - task :build_fb2 do |
77 | | - Rake::Task['book:prebuild'].invoke(48) |
| 83 | + task :build_fb2 => 'book/contributors.txt' do |
| 84 | + check_contrib() |
| 85 | + |
| 86 | + puts 'Converting to FB2...' |
| 87 | + sh "bundle exec asciidoctor-fb2 #{params} progit.asc" |
| 88 | + puts ' -- FB2 output at progit.fb2.zip' |
78 | 89 |
|
79 | | - puts 'Converting to FB2...' |
80 | | - `bundle exec asciidoctor-fb2 #{params} progit.asc --trace --verbose` |
81 | | - puts ' -- FB2 output at progit.fb2.zip' |
82 | 90 | end |
83 | 91 |
|
84 | 92 | desc 'build Mobi format' |
85 | | - task :build_mobi do |
86 | | - Rake::Task['book:prebuild'].invoke(48) |
| 93 | + task :build_mobi => 'book/contributors.txt' do |
| 94 | + check_contrib() |
87 | 95 |
|
88 | | - puts 'Converting to Mobi (kf8)...' |
89 | | - `bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc` |
90 | | - puts ' -- Mobi output at progit.mobi' |
| 96 | + puts "Converting to Mobi (kf8)..." |
| 97 | + sh "bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc" |
| 98 | + puts " -- Mobi output at progit.mobi" |
91 | 99 | end |
92 | 100 |
|
93 | 101 | desc 'build PDF format' |
94 | | - task :build_pdf do |
95 | | - Rake::Task['book:prebuild'].invoke(72) |
96 | | - |
97 | | - puts 'Converting to PDF... (this one takes a while)' |
98 | | - `bundle exec asciidoctor-pdf #{params} progit.asc 2>/dev/null` |
99 | | - puts ' -- PDF output at progit.pdf' |
100 | | - end |
| 102 | + task :build_pdf => 'book/contributors.txt' do |
| 103 | + check_contrib() |
101 | 104 |
|
102 | | - desc 'check HTML book' |
103 | | - task :check_html do |
104 | | - if not File.exist?('progit.html') |
105 | | - Rake::Task['book:build_html'].invoke |
106 | | - end |
107 | | - |
108 | | - puts ' -- Validate HTML file progit.html' |
109 | | - exec_or_raise("bundle exec htmlproofer --url-ignore #{ignore_urls} --check-html progit.html") |
| 105 | + puts 'Converting to PDF... (this one takes a while)' |
| 106 | + sh "bundle exec asciidoctor-pdf #{params} progit.asc 2>/dev/null" |
| 107 | + puts ' -- PDF output at progit.pdf' |
110 | 108 | end |
111 | 109 |
|
112 | | - desc 'check EPUB book' |
113 | | - task :check_epub do |
114 | | - if not File.exist?('progit.epub') |
115 | | - Rake::Task['book:build_epub'].invoke |
116 | | - end |
| 110 | + desc 'Check generated books' |
| 111 | + task :check => [:build_html, :build_epub] do |
| 112 | + puts 'Checking generated books' |
117 | 113 |
|
118 | | - puts ' -- Validate EPUB output file progit.epub' |
119 | | - exec_or_raise('bundle exec epubcheck progit.epub') |
| 114 | + sh "htmlproofer progit.html" |
| 115 | + sh "epubcheck progit.epub" |
120 | 116 | end |
121 | 117 |
|
122 | | - desc 'check generated books' |
123 | | - task :check => [:check_html, :check_epub] |
124 | | - |
125 | | - desc 'clean all generated files' |
| 118 | + desc 'Clean all generated files' |
126 | 119 | task :clean do |
127 | 120 | begin |
128 | | - puts 'Removing downloaded and generated files' |
129 | | - |
130 | | - FileList[locale_file, 'book/contributors.txt', 'progit.html', 'progit.epub', 'progit.fb2.zip', 'progit.pdf', 'progit.mobi'].each do |file| |
131 | | - rm file |
132 | | - rescue Errno::ENOENT |
133 | | - end |
| 121 | + puts 'Removing generated files' |
| 122 | + |
| 123 | + FileList['book/contributors.txt', 'progit.html', 'progit-kf8.epub', 'progit.epub', 'progit.fb2.zip', 'progit.mobi', 'progit.pdf'].each do |file| |
| 124 | + rm file |
| 125 | + |
| 126 | + # Rescue if file not found |
| 127 | + rescue Errno::ENOENT => e |
| 128 | + begin |
| 129 | + puts e.message |
| 130 | + puts 'Error removing files (ignored)' |
| 131 | + end |
| 132 | + end |
134 | 133 | end |
135 | 134 | end |
| 135 | + |
136 | 136 | end |
137 | 137 |
|
138 | 138 | task :default => "book:build" |
0 commit comments