|
| 1 | +require 'bundler' |
| 2 | +require 'bundler/gem_helper' |
| 3 | + |
| 4 | +class PgGemHelper < Bundler::GemHelper |
| 5 | + attr_accessor :cross_platforms |
| 6 | + |
| 7 | + def install |
| 8 | + super |
| 9 | + |
| 10 | + task "release:guard_clean" => ["release:update_history"] |
| 11 | + |
| 12 | + task "release:update_history" do |
| 13 | + update_history |
| 14 | + end |
| 15 | + |
| 16 | + task "release:rubygem_push" => ["gem:native"] |
| 17 | + end |
| 18 | + |
| 19 | + def hfile |
| 20 | + "CHANGELOG.md" |
| 21 | + end |
| 22 | + |
| 23 | + def headline |
| 24 | + '([^\w]*)v(\d+\.\d+\.\d+(?:\.\w+)?)([^\w]+)([2Y][0Y][0-9Y][0-9Y]-[0-1M][0-9M]-[0-3D][0-9D])([^\w]*|$)' |
| 25 | + end |
| 26 | + |
| 27 | + def reldate |
| 28 | + Time.now.strftime("%Y-%m-%d") |
| 29 | + end |
| 30 | + |
| 31 | + def update_history |
| 32 | + hin = File.read(hfile) |
| 33 | + hout = hin.sub(/#{headline}/) do |
| 34 | + raise "#{hfile} isn't up-to-date for version #{version}" unless $2==version.to_s |
| 35 | + $1 + $2 + $3 + reldate + $5 |
| 36 | + end |
| 37 | + if hout != hin |
| 38 | + Bundler.ui.confirm "Updating #{hfile} for release." |
| 39 | + File.write(hfile, hout) |
| 40 | + Rake::FileUtilsExt.sh "git", "commit", hfile, "-m", "Update release date in #{hfile}" |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def tag_version |
| 45 | + Bundler.ui.confirm "Tag release with annotation:" |
| 46 | + m = File.read(hfile).match(/(?<annotation>#{headline}.*?)#{headline}/m) || raise("Unable to find release notes in #{hfile}") |
| 47 | + Bundler.ui.info(m[:annotation].gsub(/^/, " ")) |
| 48 | + IO.popen(["git", "tag", "--file=-", version_tag], "w") do |fd| |
| 49 | + fd.write m[:annotation] |
| 50 | + end |
| 51 | + yield if block_given? |
| 52 | + rescue |
| 53 | + Bundler.ui.error "Untagging #{version_tag} due to error." |
| 54 | + sh_with_code "git tag -d #{version_tag}" |
| 55 | + raise |
| 56 | + end |
| 57 | + |
| 58 | + def rubygem_push(path) |
| 59 | + cross_platforms.each do |ruby_platform| |
| 60 | + super(path.gsub(/\.gem\z/, "-#{ruby_platform}.gem")) |
| 61 | + end |
| 62 | + super(path) |
| 63 | + end |
| 64 | +end |
0 commit comments