@@ -6,31 +6,84 @@ namespace :book do
66 end
77 end
88
9- desc 'build basic book formats'
10- task :build do
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
1117
12- begin
13- version_string = ENV [ 'TRAVIS_TAG' ] || `git describe --tags` . chomp
14- if version_string . empty?
15- version_string = '0'
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
1635 end
17- date_string = Time . now . strftime ( "%Y-%m-%d" )
18- params = "--attribute revnumber='#{ version_string } ' --attribute revdate='#{ date_string } '"
19- puts "Generating contributors list"
20- `git shortlog -s | grep -v -E "(Straub|Chacon|dependabot)" | cut -f 2- | column -c 120 > book/contributors.txt`
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
2144
22- puts "Converting to HTML..."
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...'
2370 `bundle exec asciidoctor #{ params } -a data-uri progit.asc`
24- puts " -- HTML output at progit.html"
71+ puts ' -- HTML output at progit.html'
2572
26- exec_or_raise ( 'htmlproofer --check-html progit.html' )
73+ end
74+
75+ desc 'build Epub format'
76+ task :build_epub => 'book/contributors.txt' do
77+ check_contrib ( )
2778
28- puts " Converting to EPub..."
79+ puts ' Converting to EPub...'
2980 `bundle exec asciidoctor-epub3 #{ params } progit.asc`
30- puts " -- Epub output at progit.epub"
81+ puts ' -- Epub output at progit.epub'
3182
32- exec_or_raise ( 'epubcheck progit.epub' )
83+ end
3384
85+ desc 'build Mobi format'
86+ task :build_mobi => 'book/contributors.txt' do
3487 # Commented out the .mobi file creation because the kindlegen dependency is not available.
3588 # For more information on this see: #1496.
3689 # This is a (hopefully) temporary fix until upstream asciidoctor-epub3 is fixed and we can offer .mobi files again.
@@ -39,12 +92,47 @@ namespace :book do
3992 # `bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc`
4093 # puts " -- Mobi output at progit.mobi"
4194
42- puts "Converting to PDF... (this one takes a while)"
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 ( )
104+
105+ puts 'Converting to PDF... (this one takes a while)'
43106 `bundle exec asciidoctor-pdf #{ params } progit.asc 2>/dev/null`
44- puts " -- PDF output at progit.pdf"
107+ puts ' -- PDF output at progit.pdf'
108+ end
109+
110+ desc 'Check generated books'
111+ task :check => [ :build_html , :build_epub ] do
112+ puts 'Checking generated books'
45113
114+ exec_or_raise ( 'htmlproofer --check-html progit.html' )
115+ exec_or_raise ( '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.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
46133 end
47134 end
135+
48136end
49137
50138task :default => "book:build"
0 commit comments