|
1 |
| -namespace :book do |
2 |
| - desc 'prepare build' |
3 |
| - task :prebuild do |
4 |
| - Dir.mkdir 'images' unless Dir.exists? 'images' |
5 |
| - Dir.glob("book/*/images/*").each do |image| |
6 |
| - FileUtils.copy(image, "images/" + File.basename(image)) |
7 |
| - end |
8 |
| - end |
| 1 | +# coding: utf-8 |
| 2 | +require 'octokit' |
9 | 3 |
|
| 4 | +namespace :book do |
10 | 5 | desc 'build basic book formats'
|
11 |
| - task :build => :prebuild do |
| 6 | + task :build do |
| 7 | + |
| 8 | + puts "Generating contributors list" |
| 9 | + `git shortlog -s --all| grep -v -E "(Straub|Chacon)" | cut -f 2- | column -c 120 > book/contributors.txt` |
| 10 | + |
12 | 11 | puts "Converting to HTML..."
|
13 |
| - `bundle exec asciidoctor -r ./config.rb progit.asc` |
| 12 | + `bundle exec asciidoctor progit.asc` |
14 | 13 | puts " -- HTML output at progit.html"
|
15 | 14 |
|
16 | 15 | puts "Converting to EPub..."
|
17 |
| - `bundle exec asciidoctor-epub3 -r ./config.rb progit.asc` |
| 16 | + `bundle exec asciidoctor-epub3 progit.asc` |
18 | 17 | puts " -- Epub output at progit.epub"
|
19 | 18 |
|
| 19 | + sh "epubcheck progit.epub" |
| 20 | + |
20 | 21 | puts "Converting to Mobi (kf8)..."
|
21 |
| - `bundle exec asciidoctor-epub3 -r ./config.rb -a ebook-format=kf8 progit.asc` |
| 22 | + `bundle exec asciidoctor-epub3 -a ebook-format=kf8 progit.asc` |
22 | 23 | puts " -- Mobi output at progit.mobi"
|
23 | 24 |
|
24 | 25 | puts "Converting to PDF... (this one takes a while)"
|
25 |
| - `bundle exec asciidoctor-pdf -r ./config.rb -a pdf-style=KaiGenGothicCN progit.asc 2>/dev/null` |
26 |
| - puts " -- PDF output at progit.pdf" |
| 26 | + `bundle exec asciidoctor-pdf progit.asc 2>/dev/null` |
| 27 | + puts " -- PDF output at progit.pdf" |
| 28 | + end |
| 29 | + |
| 30 | + desc 'tag the repo with the latest version' |
| 31 | + task :tag do |
| 32 | + api_token = ENV['GITHUB_API_TOKEN'] |
| 33 | + if (api_token && (ENV['TRAVIS_PULL_REQUEST'] == 'false') && (ENV['TRAVIS_BRANCH']=='master') && !ENV['TRAVIS_TAG']) |
| 34 | + repo = ENV['TRAVIS_REPO_SLUG'] |
| 35 | + @octokit = Octokit::Client.new(:access_token => api_token) |
| 36 | + begin |
| 37 | + last_version=@octokit.latest_release(repo).tag_name |
| 38 | + rescue |
| 39 | + last_version="2.1.-1" |
| 40 | + end |
| 41 | + new_patchlevel= last_version.split('.')[-1].to_i + 1 |
| 42 | + new_version="2.1.#{new_patchlevel}" |
| 43 | + obj = @octokit.create_tag(repo, new_version, "Version " + new_version, ENV['TRAVIS_COMMIT'], |
| 44 | + 'commit', |
| 45 | + 'Automatic build', '[email protected]', |
| 46 | + Time.now.utc.iso8601) |
| 47 | + @octokit.create_ref(repo, "tags/#{new_version}", obj.sha) |
| 48 | + p "Created tag #{new_version}" |
| 49 | + else |
| 50 | + p 'This only runs on a commit to master' |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + desc 'convert book to asciidoctor compatibility' |
| 55 | + task:convert do |
| 56 | + `cp -aR ../progit2/images .` |
| 57 | + `sed -i -e 's!/images/!!' .gitignore` |
| 58 | + `git add images` |
| 59 | + `git rm -r book/*/images` |
| 60 | + |
| 61 | + chapters = [ |
| 62 | + ["01", "introduction" ], |
| 63 | + ["02", "git-basics" ], |
| 64 | + ["03", "git-branching" ], |
| 65 | + ["04", "git-server" ], |
| 66 | + ["05", "distributed-git" ], |
| 67 | + ["06", "github" ], |
| 68 | + ["07", "git-tools" ], |
| 69 | + ["08", "customizing-git" ], |
| 70 | + ["09", "git-and-other-scms" ], |
| 71 | + ["10", "git-internals" ], |
| 72 | + ["A", "git-in-other-environments" ], |
| 73 | + ["B", "embedding-git" ], |
| 74 | + ["C", "git-commands" ] |
| 75 | + ] |
| 76 | + |
| 77 | + crossrefs = {} |
| 78 | + chapters.each { | num, title | |
| 79 | + if num =~ /[ABC]/ |
| 80 | + chap = "#{num}-#{title}" |
| 81 | + else |
| 82 | + chap = "ch#{num}-#{title}" |
| 83 | + end |
| 84 | + Dir[File.join ["book","#{num}-#{title}" , "sections","*.asc"]].map { |filename| |
| 85 | + File.read(filename).scan(/\[\[(.*?)\]\]/) |
| 86 | + }.flatten.each { |ref| |
| 87 | + crossrefs[ref] = "#{chap}" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + headrefs = {} |
| 92 | + chapters.each { | num, title | |
| 93 | + if num =~ /[ABC]/ |
| 94 | + chap = "#{num}-#{title}" |
| 95 | + else |
| 96 | + chap = "ch#{num}-#{title}" |
| 97 | + end |
| 98 | + Dir[File.join ["book","#{num}-#{title}", "*.asc"]].map { |filename| |
| 99 | + File.read(filename).scan(/\[\[(.*?)\]\]/) |
| 100 | + }.flatten.each { |ref| |
| 101 | + headrefs[ref] = "#{chap}" |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + # transform all internal cross refs |
| 106 | + chapters.each { | num, title | |
| 107 | + if num =~ /[ABC]/ |
| 108 | + chap = "#{num}-#{title}" |
| 109 | + else |
| 110 | + chap = "ch#{num}-#{title}" |
| 111 | + end |
| 112 | + files = Dir[File.join ["book","#{num}-#{title}" , "sections","*.asc"]] + |
| 113 | + Dir[File.join ["book","#{num}-#{title}" ,"1-*.asc"]] |
| 114 | + p files |
| 115 | + files.each { |filename| |
| 116 | + content = File.read(filename) |
| 117 | + new_contents = content.gsub(/\[\[(.*?)\]\]/, '[[r\1]]').gsub( |
| 118 | + "→", "→").gsub(/<<(.*?)>>/) { |match| |
| 119 | + ch = crossrefs[$1] |
| 120 | + h = headrefs[$1] |
| 121 | + # p " #{match} -> #{ch}, #{h}" |
| 122 | + if ch |
| 123 | + # if local do not add the file |
| 124 | + if ch==chap |
| 125 | + "<<r#{$1}>>" |
| 126 | + else |
| 127 | + "<<#{ch}#r#{$1}>>" |
| 128 | + end |
| 129 | + elsif h |
| 130 | + if h==chap |
| 131 | + "<<#{chap}>>" |
| 132 | + else |
| 133 | + "<<#{h}##{h}>>" |
| 134 | + end |
| 135 | + end |
| 136 | + } |
| 137 | + File.open(filename, "w") {|file| file.puts new_contents } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + chapters.each { | num, title | |
| 142 | + if num =~ /[ABC]/ |
| 143 | + chap = "#{num}-#{title}" |
| 144 | + else |
| 145 | + chap = "ch#{num}-#{title}" |
| 146 | + end |
| 147 | + Dir[File.join ["book","#{num}-#{title}" ,"1*.asc"]].map { |filename| |
| 148 | + content = File.read (filename) |
| 149 | + new_contents = content.gsub(/include::(.*?)asc/) {|match| |
| 150 | + "include::book/#{num}-#{title}/#{$1}asc"} |
| 151 | + `git rm #{filename}` |
| 152 | + File.open("#{chap}.asc", "w") {|file| |
| 153 | + file.puts "[##{chap}]\n" |
| 154 | + file.puts new_contents } |
| 155 | + `git add "#{chap}.asc"` |
| 156 | + } |
| 157 | + } |
27 | 158 | end
|
28 | 159 | end
|
29 | 160 |
|
| 161 | + |
| 162 | + |
30 | 163 | task :default => "book:build"
|
0 commit comments