|
1 | 1 | namespace :book do |
| 2 | + |
| 3 | + # Variables referenced for build |
| 4 | + version_string = `git describe --tags --abbrev=0`.chomp |
| 5 | + if version_string.empty? |
| 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 |
| 34 | + end |
| 35 | + |
2 | 36 | desc 'build basic book formats' |
3 | | - task :build do |
| 37 | + task :build => [:build_html, :build_epub, :build_fb2, :build_mobi, :build_pdf] do |
| 38 | + begin |
| 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 | + end |
| 47 | + end |
| 48 | + |
| 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 |
| 54 | + |
| 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" |
| 60 | + end |
| 61 | + |
| 62 | + desc 'build HTML format' |
| 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' |
| 69 | + |
| 70 | + end |
4 | 71 |
|
5 | | - puts "Generating contributors list" |
6 | | - `git shortlog -s master| grep -v -E "(Straub|Chacon)" | cut -f 2- | column -c 120 > book/contributors.txt` |
| 72 | + desc 'build Epub format' |
| 73 | + task :build_epub => 'book/contributors.txt' do |
| 74 | + check_contrib() |
7 | 75 |
|
8 | | - puts "Converting to HTML..." |
9 | | - `bundle exec asciidoctor progit.asc` |
10 | | - puts " -- HTML output at progit.html" |
| 76 | + puts 'Converting to EPub...' |
| 77 | + sh "bundle exec asciidoctor-epub3 #{params} progit.asc" |
| 78 | + puts ' -- Epub output at progit.epub' |
11 | 79 |
|
12 | | - puts "Converting to EPub..." |
13 | | - `bundle exec asciidoctor-epub3 progit.asc` |
14 | | - puts " -- Epub output at progit.epub" |
| 80 | + end |
| 81 | + |
| 82 | + desc 'build FB2 format' |
| 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' |
| 89 | + |
| 90 | + end |
| 91 | + |
| 92 | + desc 'build Mobi format' |
| 93 | + task :build_mobi => 'book/contributors.txt' do |
| 94 | + check_contrib() |
| 95 | + |
| 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" |
| 99 | + end |
15 | 100 |
|
16 | | - puts "Converting to Mobi (kf8)..." |
17 | | - `bundle exec asciidoctor-epub3 -a ebook-format=kf8 progit.asc` |
18 | | - puts " -- Mobi output at progit.mobi" |
| 101 | + desc 'build PDF format' |
| 102 | + task :build_pdf => 'book/contributors.txt' do |
| 103 | + check_contrib() |
19 | 104 |
|
20 | | - puts "Converting to PDF... (this one takes a while)" |
21 | | - `bundle exec asciidoctor-pdf progit.asc 2>/dev/null` |
22 | | - puts " -- PDF output at progit.pdf" |
| 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' |
23 | 108 | end |
| 109 | + |
| 110 | + desc 'Check generated books' |
| 111 | + task :check => [:build_html, :build_epub] do |
| 112 | + puts 'Checking generated books' |
| 113 | + |
| 114 | + sh "htmlproofer progit.html" |
| 115 | + sh "epubcheck progit.epub" |
| 116 | + end |
| 117 | + |
| 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-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 |
| 133 | + end |
| 134 | + end |
| 135 | + |
24 | 136 | end |
25 | 137 |
|
26 | 138 | task :default => "book:build" |
0 commit comments