|
| 1 | +# coding: utf-8 |
| 2 | +require 'octokit' |
| 3 | +require 'github_changelog_generator' |
| 4 | + |
| 5 | +def exec_or_raise(command) |
| 6 | + puts `#{command}` |
| 7 | + if (! $?.success?) |
| 8 | + raise "'#{command}' failed" |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +module GitHubChangelogGenerator |
| 13 | + |
| 14 | + #OPTIONS = %w[ user project token date_format output |
| 15 | + # bug_prefix enhancement_prefix issue_prefix |
| 16 | + # header merge_prefix issues |
| 17 | + # add_issues_wo_labels add_pr_wo_labels |
| 18 | + # pulls filter_issues_by_milestone author |
| 19 | + # unreleased_only unreleased unreleased_label |
| 20 | + # compare_link include_labels exclude_labels |
| 21 | + # bug_labels enhancement_labels |
| 22 | + # between_tags exclude_tags exclude_tags_regex since_tag max_issues |
| 23 | + # github_site github_endpoint simple_list |
| 24 | + # future_release release_branch verbose release_url |
| 25 | + # base configure_sections add_sections] |
| 26 | + |
| 27 | + def get_log(&task_block) |
| 28 | + options = Parser.default_options |
| 29 | + yield(options) if task_block |
| 30 | + |
| 31 | + options[:user],options[:project] = ENV['TRAVIS_REPO_SLUG'].split('/') |
| 32 | + options[:token] = ENV['GITHUB_API_TOKEN'] |
| 33 | + options[:unreleased] = false |
| 34 | + |
| 35 | + generator = Generator.new options |
| 36 | + generator.compound_changelog |
| 37 | + end |
| 38 | + |
| 39 | + module_function :get_log |
| 40 | +end |
| 41 | + |
1 | 42 | namespace :book do
|
2 | 43 | desc 'build basic book formats'
|
3 | 44 | task :build do
|
4 | 45 |
|
5 |
| - begin |
6 |
| - version_string = ENV['TRAVIS_TAG'] || `git describe --tags`.chomp |
7 |
| - if version_string.empty? |
8 |
| - version_string = '0' |
9 |
| - end |
10 |
| - date_string = Time.now.strftime("%Y-%m-%d") |
11 |
| - params = "--attribute revnumber='#{version_string}' --attribute revdate='#{date_string}'" |
12 |
| - puts "Generating contributors list" |
13 |
| - `git shortlog -s | grep -v -E "(Straub|Chacon)" | cut -f 2- | column -c 120 > book/contributors.txt` |
| 46 | + puts "Generating contributors list" |
| 47 | + exec_or_raise("git shortlog -s --all $translation_origin | grep -v -E '(Straub|Chacon)' | cut -f 2- | sort | column -c 110 > book/contributors.txt") |
14 | 48 |
|
15 |
| - puts "Converting to HTML..." |
16 |
| - `bundle exec asciidoctor #{params} progit.asc` |
17 |
| - puts " -- HTML output at progit.html" |
| 49 | + # detect if the deployment is using glob |
| 50 | + travis = File.read(".travis.yml") |
| 51 | + version_string = ENV['TRAVIS_TAG'] || '0' |
| 52 | + if travis.match(/file_glob/) |
| 53 | + progit_v = "progit_v#{version_string}" |
| 54 | + else |
| 55 | + progit_v = "progit" |
| 56 | + end |
| 57 | + text = File.read('progit.asc') |
| 58 | + new_contents = text.gsub("$$VERSION$$", version_string).gsub("$$DATE$$", Time.now.strftime("%Y-%m-%d")) |
| 59 | + File.open("#{progit_v}.asc", "w") {|file| file.puts new_contents } |
| 60 | + |
| 61 | + puts "Converting to HTML..." |
| 62 | + exec_or_raise("bundle exec asciidoctor #{progit_v}.asc") |
| 63 | + puts " -- HTML output at #{progit_v}.html" |
18 | 64 |
|
19 |
| - puts "Converting to EPub..." |
20 |
| - `bundle exec asciidoctor-epub3 #{params} progit.asc` |
21 |
| - puts " -- Epub output at progit.epub" |
| 65 | + puts "Converting to EPub..." |
| 66 | + exec_or_raise("bundle exec asciidoctor-epub3 #{progit_v}.asc") |
| 67 | + puts " -- Epub output at #{progit_v}.epub" |
22 | 68 |
|
23 |
| - puts "Converting to Mobi (kf8)..." |
24 |
| - `bundle exec asciidoctor-epub3 #{params} -a ebook-format=kf8 progit.asc` |
25 |
| - puts " -- Mobi output at progit.mobi" |
| 69 | + exec_or_raise("epubcheck #{progit_v}.epub") |
26 | 70 |
|
27 |
| - puts "Converting to PDF... (this one takes a while)" |
28 |
| - `bundle exec asciidoctor-pdf #{params} progit.asc 2>/dev/null` |
29 |
| - puts " -- PDF output at progit.pdf" |
| 71 | + puts "Converting to Mobi (kf8)..." |
| 72 | + exec_or_raise("bundle exec asciidoctor-epub3 -a ebook-format=kf8 #{progit_v}.asc") |
| 73 | + # remove the fake epub that would shadow the really one |
| 74 | + exec_or_raise("rm progit*kf8.epub") |
| 75 | + puts " -- Mobi output at #{progit_v}.mobi" |
30 | 76 |
|
| 77 | + repo = ENV['TRAVIS_REPO_SLUG'] |
| 78 | + puts "Converting to PDF... (this one takes a while)" |
| 79 | + if (repo == "progit/progit2-zh") |
| 80 | + exec_or_raise("asciidoctor-pdf-cjk-kai_gen_gothic-install") |
| 81 | + exec_or_raise("bundle exec asciidoctor-pdf -r asciidoctor-pdf-cjk -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicCN #{progit_v}.asc") |
| 82 | + elsif (repo == "progit/progit2-ja") |
| 83 | + exec_or_raise("asciidoctor-pdf-cjk-kai_gen_gothic-install") |
| 84 | + exec_or_raise("bundle exec asciidoctor-pdf -r asciidoctor-pdf-cjk -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicJP #{progit_v}.asc") |
| 85 | + elsif (repo == "progit/progit2-zh-tw") |
| 86 | + exec_or_raise("asciidoctor-pdf-cjk-kai_gen_gothic-install") |
| 87 | + exec_or_raise("bundle exec asciidoctor-pdf -r asciidoctor-pdf-cjk -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicTW #{progit_v}.asc") |
| 88 | + elsif (repo == "progit/progit2-ko") |
| 89 | + exec_or_raise("asciidoctor-pdf-cjk-kai_gen_gothic-install") |
| 90 | + exec_or_raise("bundle exec asciidoctor-pdf -r asciidoctor-pdf-cjk -r asciidoctor-pdf-cjk-kai_gen_gothic -a pdf-style=KaiGenGothicKR #{progit_v}.asc") |
| 91 | + else |
| 92 | + exec_or_raise("bundle exec asciidoctor-pdf #{progit_v}.asc 2>/dev/null") |
31 | 93 | end
|
| 94 | + puts " -- PDF output at #{progit_v}.pdf" |
| 95 | + end |
| 96 | + |
| 97 | + desc 'tag the repo with the latest version' |
| 98 | + task :tag do |
| 99 | + api_token = ENV['GITHUB_API_TOKEN'] |
| 100 | + if ((api_token) && (ENV['TRAVIS_PULL_REQUEST'] == 'false')) |
| 101 | + repo = ENV['TRAVIS_REPO_SLUG'] |
| 102 | + @octokit = Octokit::Client.new(:access_token => api_token) |
| 103 | + begin |
| 104 | + last_version=@octokit.latest_release(repo).tag_name |
| 105 | + rescue |
| 106 | + last_version="2.1.-1" |
| 107 | + end |
| 108 | + new_patchlevel= last_version.split('.')[-1].to_i + 1 |
| 109 | + new_version="2.1.#{new_patchlevel}" |
| 110 | + if (ENV['TRAVIS_BRANCH']=='master') |
| 111 | + obj = @octokit.create_tag(repo, new_version, "Version " + new_version, |
| 112 | + ENV['TRAVIS_COMMIT'], 'commit', |
| 113 | + 'Automatic build', '[email protected]', |
| 114 | + Time.now.utc.iso8601) |
| 115 | + begin |
| 116 | + @octokit.create_ref(repo, "tags/#{new_version}", obj.sha) |
| 117 | + rescue |
| 118 | + p "the ref already exists ???" |
| 119 | + end |
| 120 | + p "Created tag #{last_version}" |
| 121 | + elsif (ENV['TRAVIS_TAG']) |
| 122 | + version = ENV['TRAVIS_TAG'] |
| 123 | + changelog = GitHubChangelogGenerator.get_log do |config| |
| 124 | + config[:since_tag] = last_version |
| 125 | + end |
| 126 | + credit_line = "\\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*" |
| 127 | + changelog.gsub!(credit_line, "") |
| 128 | + @octokit.create_release(repo, new_version, {:name => "v#{new_version}", :body => changelog}) |
| 129 | + p "Created release #{new_version}" |
| 130 | + else |
| 131 | + p 'This only runs on a commit to master' |
| 132 | + end |
| 133 | + else |
| 134 | + p 'No interaction with GitHub' |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + desc 'convert book to asciidoctor compatibility' |
| 139 | + task:convert do |
| 140 | + `cp -aR ../progit2/images .` |
| 141 | + `sed -i -e 's!/images/!!' .gitignore` |
| 142 | + `git add images` |
| 143 | + `git rm -r book/*/images` |
| 144 | + |
| 145 | + chapters = [ |
| 146 | + ["01", "introduction" ], |
| 147 | + ["02", "git-basics" ], |
| 148 | + ["03", "git-branching" ], |
| 149 | + ["04", "git-server" ], |
| 150 | + ["05", "distributed-git" ], |
| 151 | + ["06", "github" ], |
| 152 | + ["07", "git-tools" ], |
| 153 | + ["08", "customizing-git" ], |
| 154 | + ["09", "git-and-other-scms" ], |
| 155 | + ["10", "git-internals" ], |
| 156 | + ["A", "git-in-other-environments" ], |
| 157 | + ["B", "embedding-git" ], |
| 158 | + ["C", "git-commands" ] |
| 159 | + ] |
| 160 | + |
| 161 | + crossrefs = {} |
| 162 | + chapters.each { | num, title | |
| 163 | + if num =~ /[ABC]/ |
| 164 | + chap = "#{num}-#{title}" |
| 165 | + else |
| 166 | + chap = "ch#{num}-#{title}" |
| 167 | + end |
| 168 | + Dir[File.join ["book","#{num}-#{title}" , "sections","*.asc"]].map { |filename| |
| 169 | + File.read(filename).scan(/\[\[(.*?)\]\]/) |
| 170 | + }.flatten.each { |ref| |
| 171 | + crossrefs[ref] = "#{chap}" |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + headrefs = {} |
| 176 | + chapters.each { | num, title | |
| 177 | + if num =~ /[ABC]/ |
| 178 | + chap = "#{num}-#{title}" |
| 179 | + else |
| 180 | + chap = "ch#{num}-#{title}" |
| 181 | + end |
| 182 | + Dir[File.join ["book","#{num}-#{title}", "*.asc"]].map { |filename| |
| 183 | + File.read(filename).scan(/\[\[([_a-z0-9]*?)\]\]/) |
| 184 | + }.flatten.each { |ref| |
| 185 | + headrefs[ref] = "#{chap}" |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + # transform all internal cross refs |
| 190 | + chapters.each { | num, title | |
| 191 | + if num =~ /[ABC]/ |
| 192 | + chap = "#{num}-#{title}" |
| 193 | + else |
| 194 | + chap = "ch#{num}-#{title}" |
| 195 | + end |
| 196 | + files = Dir[File.join ["book","#{num}-#{title}" , "sections","*.asc"]] + |
| 197 | + Dir[File.join ["book","#{num}-#{title}" ,"1-*.asc"]] |
| 198 | + p files |
| 199 | + files.each { |filename| |
| 200 | + content = File.read(filename) |
| 201 | + new_contents = content.gsub(/\[\[([_a-z0-9]*?)\]\]/, '[[r\1]]').gsub( |
| 202 | + "→", "→").gsub(/<<([_a-z0-9]*?)>>/) { |match| |
| 203 | + ch = crossrefs[$1] |
| 204 | + h = headrefs[$1] |
| 205 | + # p " #{match} -> #{ch}, #{h}" |
| 206 | + if ch |
| 207 | + # if local do not add the file |
| 208 | + if ch==chap |
| 209 | + "<<r#{$1}>>" |
| 210 | + else |
| 211 | + "<<#{ch}#r#{$1}>>" |
| 212 | + end |
| 213 | + elsif h |
| 214 | + if h==chap |
| 215 | + "<<#{chap}>>" |
| 216 | + else |
| 217 | + "<<#{h}##{h}>>" |
| 218 | + end |
| 219 | + else |
| 220 | + p "could not match xref #{$1}" |
| 221 | + "<<#{$1}>>" |
| 222 | + end |
| 223 | + } |
| 224 | + File.open(filename, "w") {|file| file.puts new_contents } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + chapters.each { | num, title | |
| 229 | + if num =~ /[ABC]/ |
| 230 | + chap = "#{num}-#{title}" |
| 231 | + else |
| 232 | + chap = "ch#{num}-#{title}" |
| 233 | + end |
| 234 | + Dir[File.join ["book","#{num}-#{title}" ,"1*.asc"]].map { |filename| |
| 235 | + content = File.read (filename) |
| 236 | + new_contents = content.gsub(/include::(.*?)asc/) {|match| |
| 237 | + "include::book/#{num}-#{title}/#{$1}asc"} |
| 238 | + `git rm -f #{filename}` |
| 239 | + File.open("#{chap}.asc", "w") {|file| |
| 240 | + file.puts "[##{chap}]\n" |
| 241 | + file.puts new_contents } |
| 242 | + `git add "#{chap}.asc"` |
| 243 | + } |
| 244 | + } |
32 | 245 | end
|
33 | 246 | end
|
34 | 247 |
|
| 248 | + |
| 249 | + |
35 | 250 | task :default => "book:build"
|
0 commit comments