|
| 1 | +require 'open-uri' |
| 2 | + |
1 | 3 | namespace :book do |
2 | 4 | def exec_or_raise(command) |
3 | 5 | puts `#{command}` |
4 | 6 | if (! $?.success?) |
5 | | - raise "'#{command}' failed" |
| 7 | + raise "[ERROR] '#{command}' failed" |
6 | 8 | end |
7 | 9 | end |
8 | 10 |
|
9 | | - desc 'build basic book formats' |
10 | | - task :build do |
| 11 | + def generate_contributors_list(column_size) |
| 12 | + # Generating preformatted contributors list... |
| 13 | + `git shortlog -s | 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 | + |
| 28 | + # Variables referenced for build |
| 29 | + lang = 'be' |
| 30 | + locale_file = "attributes-#{lang}.adoc" |
| 31 | + date_string = Time.now.strftime('%d.%m.%Y') |
| 32 | + |
| 33 | + version_string = `git describe --tags`.chomp |
| 34 | + if version_string.empty? |
| 35 | + version_string = '0' |
| 36 | + 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'" |
11 | 39 |
|
| 40 | + # Tasks list |
| 41 | + desc 'build basic book formats' |
| 42 | + task :build => [:build_html, :build_epub, :build_mobi, :build_pdf] do |
12 | 43 | begin |
13 | | - lang = "be" |
14 | | - begin |
15 | | - locale_file = "attributes-#{lang}.adoc" |
16 | | - if not File.exist?(locale_file) |
17 | | - puts "Downloading locale attributes file #{lang_file} from asciidoc repo..." |
18 | | - l10n_text = URI.open("https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/data/locale/#{locale_file}").read |
19 | | - File.open(locale_file, 'w') {|file| file.puts l10n_text} |
20 | | - else |
21 | | - puts "Use existing file with locale attributes #{locale_file}" |
22 | | - end |
23 | | - rescue |
24 | | - puts "[ERROR] Can not download attributes list for language #{lang}" |
25 | | - end |
| 44 | + puts 'Validating generated files...' |
| 45 | + Rake::Task['book:check'].invoke |
| 46 | + end |
| 47 | + end |
26 | 48 |
|
27 | | - version_string = ENV['TRAVIS_TAG'] || `git describe --tags`.chomp |
28 | | - if version_string.empty? |
29 | | - version_string = '0' |
30 | | - end |
31 | | - date_string = Time.now.strftime("%Y-%m-%d") |
32 | | - params = "--attribute revnumber='#{version_string}' --attribute revdate='#{date_string}'" |
33 | | - puts "Generating contributors list" |
34 | | - `git shortlog -s | grep -v -E "(Straub|Chacon|dependabot)" | cut -f 2- | column -c 120 > book/contributors.txt` |
| 49 | + desc 'prepare necessary data to start build' |
| 50 | + task :prebuild, [:column_size] do |t, args| |
| 51 | + args.with_defaults(:column_size => 96) |
| 52 | + |
| 53 | + download_locale(locale_file) |
| 54 | + generate_contributors_list(args.column_size) |
| 55 | + end |
| 56 | + |
| 57 | + desc 'build HTML format' |
| 58 | + task :build_html do |
| 59 | + Rake::Task['book:prebuild'].invoke(96) |
35 | 60 |
|
36 | | - puts "Converting to HTML..." |
37 | | - `bundle exec asciidoctor #{params} -a data-uri progit.asc` |
38 | | - puts " -- HTML output at progit.html" |
| 61 | + puts 'Converting to HTML...' |
| 62 | + `bundle exec asciidoctor #{params} -a data-uri progit.asc` |
| 63 | + puts ' -- HTML output at progit.html' |
| 64 | + end |
39 | 65 |
|
40 | | - exec_or_raise('htmlproofer --check-html progit.html') |
| 66 | + desc 'build EPUB format' |
| 67 | + task :build_epub do |
| 68 | + Rake::Task['book:prebuild'].invoke(48) |
41 | 69 |
|
42 | | - puts "Converting to EPub..." |
43 | | - `bundle exec asciidoctor-epub3 #{params} progit.asc` |
44 | | - puts " -- Epub output at progit.epub" |
| 70 | + puts 'Converting to EPUB...' |
| 71 | + `bundle exec asciidoctor-epub3 #{params} progit.asc` |
| 72 | + puts ' -- EPUB output at progit.epub' |
| 73 | + end |
45 | 74 |
|
46 | | - exec_or_raise('epubcheck progit.epub') |
| 75 | + desc 'build Mobi format' |
| 76 | + task :build_mobi do |
| 77 | + Rake::Task['book:prebuild'].invoke(96) |
47 | 78 |
|
48 | | - # Commented out the .mobi file creation because the kindlegen dependency is not available. |
49 | | - # For more information on this see: #1496. |
50 | | - # This is a (hopefully) temporary fix until upstream asciidoctor-epub3 is fixed and we can offer .mobi files again. |
| 79 | + # Commented out the .mobi file creation because the KindleGen dependency is not available. |
| 80 | + # |
| 81 | + # FIXME: If asciidoctor-epub3 supports Mobi again, uncomment these lines below |
| 82 | + # puts 'Converting to Mobi (kf8)...' |
| 83 | + # `bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc` |
| 84 | + # puts ' -- Mobi output at progit.mobi' |
51 | 85 |
|
52 | | - # puts "Converting to Mobi (kf8)..." |
53 | | - # `bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc` |
54 | | - # puts " -- Mobi output at progit.mobi" |
| 86 | + puts "Converting to Mobi is not longer supported." |
| 87 | + puts "For more information see issue #1496 at https://github.com/progit/progit2/issues/1496." |
| 88 | + end |
55 | 89 |
|
56 | | - puts "Converting to PDF... (this one takes a while)" |
57 | | - `bundle exec asciidoctor-pdf #{params} progit.asc 2>/dev/null` |
58 | | - puts " -- PDF output at progit.pdf" |
| 90 | + desc 'build PDF format' |
| 91 | + task :build_pdf do |
| 92 | + Rake::Task['book:prebuild'].invoke(88) |
| 93 | + |
| 94 | + puts 'Converting to PDF... (this one takes a while)' |
| 95 | + `bundle exec asciidoctor-pdf #{params} progit.asc 2>/dev/null` |
| 96 | + puts ' -- PDF output at progit.pdf' |
| 97 | + end |
| 98 | + |
| 99 | + desc 'check HTML book' |
| 100 | + task :check_html do |
| 101 | + if not File.exist?('progit.html') |
| 102 | + Rake::Task['book:build_html'].invoke |
| 103 | + end |
| 104 | + |
| 105 | + puts ' -- Validate HTML file progit.html' |
| 106 | + exec_or_raise("bundle exec htmlproofer --url-ignore #{ignore_urls} --check-html progit.html") |
| 107 | + end |
59 | 108 |
|
| 109 | + desc 'check EPUB book' |
| 110 | + task :check_epub do |
| 111 | + if not File.exist?('progit.epub') |
| 112 | + Rake::Task['book:build_epub'].invoke |
| 113 | + end |
| 114 | + |
| 115 | + puts ' -- Validate EPUB output file progit.epub' |
| 116 | + exec_or_raise('bundle exec epubcheck progit.epub') |
| 117 | + end |
| 118 | + |
| 119 | + desc 'check generated books' |
| 120 | + task :check => [:check_html, :check_epub] |
| 121 | + |
| 122 | + desc 'clean all generated files' |
| 123 | + task :clean do |
| 124 | + begin |
| 125 | + puts 'Removing downloaded and generated files' |
| 126 | + |
| 127 | + FileList[locale_file, 'book/contributors.txt', 'progit.html', 'progit.epub', 'progit.pdf', 'progit.mobi'].each do |file| |
| 128 | + rm file |
| 129 | + rescue Errno::ENOENT |
| 130 | + end |
60 | 131 | end |
61 | 132 | end |
62 | 133 | end |
|
0 commit comments