|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "English" |
| 4 | +require "bundler" |
| 5 | +require_relative "task_helpers" |
| 6 | + |
| 7 | +desc "Updates CHANGELOG.md inserting headers for the new version. |
| 8 | +
|
| 9 | +Argument: Git tag. Defaults to the latest tag." |
| 10 | + |
| 11 | +task :update_changelog, %i[tag] do |_, args| |
| 12 | + tag = args[:tag] || `git describe --tags --abbrev=0`.strip |
| 13 | + anchor = "[#{tag}]" |
| 14 | + |
| 15 | + changelog = File.read("CHANGELOG.md") |
| 16 | + |
| 17 | + if changelog.include?(anchor) |
| 18 | + puts "Tag #{tag} is already documented in CHANGELOG.md, update manually if needed" |
| 19 | + next |
| 20 | + end |
| 21 | + |
| 22 | + tag_date_output = `git show -s --format=%cs #{tag} 2>&1` |
| 23 | + if $CHILD_STATUS.success? |
| 24 | + tag_date = tag_date_output.split("\n").last.strip |
| 25 | + else |
| 26 | + abort("Failed to find tag #{tag}") |
| 27 | + end |
| 28 | + |
| 29 | + # after "Changes since the last non-beta release.", insert link header |
| 30 | + changelog.sub!("Changes since the last non-beta release.", "\\0\n\n### #{anchor} - #{tag_date}") |
| 31 | + |
| 32 | + # find the link in "[Unreleased]: ...", update it, and add the link for our new tag after it |
| 33 | + compare_link_prefix = "https://github.com/shakacode/react_on_rails/compare" |
| 34 | + match_data = %r{#{compare_link_prefix}/(?<prev_tag>.*)\.\.\.master}.match(changelog) |
| 35 | + if match_data |
| 36 | + prev_tag = match_data[:prev_tag] |
| 37 | + new_unreleased_link = "#{compare_link_prefix}/#{tag}...master" |
| 38 | + new_tag_link = "#{anchor}: #{compare_link_prefix}/#{prev_tag}...#{tag}" |
| 39 | + changelog.sub!(match_data[0], "#{new_unreleased_link}\n#{new_tag_link}") |
| 40 | + end |
| 41 | + |
| 42 | + File.write("CHANGELOG.md", changelog) |
| 43 | + puts "Updated CHANGELOG.md with an entry for #{tag}" |
| 44 | +end |
0 commit comments