|
1 | 1 | namespace :book do |
2 | | - desc 'build basic book formats' |
3 | | - task :build do |
| 2 | + def exec_or_raise(command) |
| 3 | + puts `#{command}` |
| 4 | + if (! $?.success?) |
| 5 | + raise "'#{command}' failed" |
| 6 | + end |
| 7 | + end |
4 | 8 |
|
5 | | - `cp progit.asc progit.asc.bak` |
6 | | - begin |
7 | | - version_string = ENV['TRAVIS_TAG'] || `git describe --tags`.chomp |
8 | | - if version_string.empty? |
9 | | - version_string = '0' |
| 9 | + # Variables referenced for build |
| 10 | + version_string = ENV['TRAVIS_TAG'] || `git describe --tags`.chomp |
| 11 | + if version_string.empty? |
| 12 | + version_string = '0' |
| 13 | + end |
| 14 | + date_string = Time.now.strftime('%Y-%m-%d') |
| 15 | + params = "--attribute revnumber='#{version_string}' --attribute revdate='#{date_string}'" |
| 16 | + header_hash = `git rev-parse --short HEAD`.strip |
| 17 | + |
| 18 | + # Check contributors list |
| 19 | + # This checks commit hash stored in the header of list against current HEAD |
| 20 | + def check_contrib |
| 21 | + if File.exist?('book/contributors.txt') |
| 22 | + current_head_hash = `git rev-parse --short HEAD`.strip |
| 23 | + header = `head -n 1 book/contributors.txt`.strip |
| 24 | + # Match regex, then coerce resulting array to string by join |
| 25 | + header_hash = header.scan(/[a-f0-9]{7,}/).join |
| 26 | + |
| 27 | + if header_hash == current_head_hash |
| 28 | + puts "Hash on header of contributors list (#{header_hash}) matches the current HEAD (#{current_head_hash})" |
| 29 | + else |
| 30 | + puts "Hash on header of contributors list (#{header_hash}) does not match the current HEAD (#{current_head_hash}), refreshing" |
| 31 | + `rm book/contributors.txt` |
| 32 | + # Reenable and invoke task again |
| 33 | + Rake::Task['book/contributors.txt'].reenable |
| 34 | + Rake::Task['book/contributors.txt'].invoke |
10 | 35 | end |
11 | | - text = File.read('progit.asc') |
12 | | - new_contents = text.gsub("$$VERSION$$", version_string).gsub("$$DATE$$", Time.now.strftime("%Y-%m-%d")) |
13 | | - File.open("progit.asc", "w") {|file| file.puts new_contents } |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + desc 'build basic book formats' |
| 40 | + task :build => [:build_html, :build_epub, :build_pdf] do |
| 41 | + begin |
| 42 | + # Run check |
| 43 | + Rake::Task['book:check'].invoke |
| 44 | + |
| 45 | + # Rescue to ignore checking errors |
| 46 | + rescue => e |
| 47 | + puts e.message |
| 48 | + puts 'Error when checking books (ignored)' |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + desc 'build basic book formats (for ci)' |
| 53 | + task :ci => [:build_html, :build_epub, :build_pdf] do |
| 54 | + # Run check, but don't ignore any errors |
| 55 | + Rake::Task['book:check'].invoke |
| 56 | + end |
| 57 | + |
| 58 | + desc 'generate contributors list' |
| 59 | + file 'book/contributors.txt' do |
| 60 | + puts 'Generating contributors list' |
| 61 | + `echo "Contributors as of #{header_hash}:\n" > book/contributors.txt` |
| 62 | + `git shortlog -s | grep -v -E "(Straub|Chacon|dependabot)" | cut -f 2- | column -c 120 >> book/contributors.txt` |
| 63 | + end |
| 64 | + |
| 65 | + desc 'build HTML format' |
| 66 | + task :build_html => 'book/contributors.txt' do |
| 67 | + check_contrib() |
| 68 | + |
| 69 | + puts 'Converting to HTML...' |
| 70 | + `bundle exec asciidoctor #{params} -a data-uri progit.asc` |
| 71 | + puts ' -- HTML output at progit.html' |
| 72 | + |
| 73 | + end |
| 74 | + |
| 75 | + desc 'build Epub format' |
| 76 | + task :build_epub => 'book/contributors.txt' do |
| 77 | + check_contrib() |
| 78 | + |
| 79 | + puts 'Converting to EPub...' |
| 80 | + `bundle exec asciidoctor-epub3 #{params} progit.asc` |
| 81 | + puts ' -- Epub output at progit.epub' |
| 82 | + |
| 83 | + end |
14 | 84 |
|
15 | | - puts "Generating contributors list" |
16 | | - `git shortlog -s | grep -v -E "(Straub|Chacon)" | cut -f 2- | column -c 120 > book/contributors.txt` |
| 85 | + desc 'build Mobi format' |
| 86 | + task :build_mobi => 'book/contributors.txt' do |
| 87 | + # Commented out the .mobi file creation because the kindlegen dependency is not available. |
| 88 | + # For more information on this see: #1496. |
| 89 | + # This is a (hopefully) temporary fix until upstream asciidoctor-epub3 is fixed and we can offer .mobi files again. |
17 | 90 |
|
18 | | - puts "Converting to HTML..." |
19 | | - `bundle exec asciidoctor progit.asc` |
20 | | - puts " -- HTML output at progit.html" |
| 91 | + # puts "Converting to Mobi (kf8)..." |
| 92 | + # `bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc` |
| 93 | + # puts " -- Mobi output at progit.mobi" |
21 | 94 |
|
22 | | - puts "Converting to EPub..." |
23 | | - `bundle exec asciidoctor-epub3 progit.asc` |
24 | | - puts " -- Epub output at progit.epub" |
| 95 | + # FIXME: If asciidoctor-epub3 supports Mobi again, uncomment these lines below |
| 96 | + puts "Converting to Mobi isn't supported yet." |
| 97 | + puts "For more information see issue #1496 at https://github.com/progit/progit2/issues/1496." |
| 98 | + exit(127) |
| 99 | + end |
| 100 | + |
| 101 | + desc 'build PDF format' |
| 102 | + task :build_pdf => 'book/contributors.txt' do |
| 103 | + check_contrib() |
25 | 104 |
|
26 | | - puts "Converting to Mobi (kf8)..." |
27 | | - `bundle exec asciidoctor-epub3 -a ebook-format=kf8 progit.asc` |
28 | | - puts " -- Mobi output at progit.mobi" |
| 105 | + puts 'Converting to PDF... (this one takes a while)' |
| 106 | + `bundle exec asciidoctor-pdf #{params} -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicKR progit.asc ` |
| 107 | + puts ' -- PDF output at progit.pdf' |
| 108 | + end |
29 | 109 |
|
30 | | - puts "Converting to PDF... (this one takes a while)" |
31 | | - `bundle exec asciidoctor-pdf -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicKR progit.asc 2>/dev/null` |
32 | | - puts " -- PDF output at progit.pdf" |
| 110 | + desc 'Check generated books' |
| 111 | + task :check => [:build_html, :build_epub] do |
| 112 | + puts 'Checking generated books' |
| 113 | + |
| 114 | + exec_or_raise('htmlproofer --check-html progit.html') |
| 115 | + exec_or_raise('epubcheck progit.epub') |
| 116 | + end |
33 | 117 |
|
34 | | - ensure |
35 | | - `mv progit.asc.bak progit.asc` |
| 118 | + desc 'Clean all generated files' |
| 119 | + task :clean do |
| 120 | + begin |
| 121 | + puts 'Removing generated files' |
| 122 | + |
| 123 | + FileList['book/contributors.txt', 'progit.html', 'progit.epub', '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 |
36 | 133 | end |
37 | 134 | end |
| 135 | + |
38 | 136 | end |
39 | 137 |
|
40 | 138 | task :default => "book:build" |
0 commit comments