Skip to content

Commit

Permalink
Merge pull request #41 from collectiveidea/streamline-release-task
Browse files Browse the repository at this point in the history
Streamline gem release with bundler gem task release override
  • Loading branch information
darronschall authored May 29, 2024
2 parents 739e63f + 7ed83a3 commit 891adce
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [Unreleased]

- Streamline gem release with rake task - [#41](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/41)
- Make `skip-empty` the default behavior; remove recognizing the option flag - [#40](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/40)
- Update GitHub action to run specs on all supported Ruby versions - [#37](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/37)

Expand Down
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,16 @@ protoc --ruby_out=. --twirp_ruby_out=. ./example/hello_world.proto

## Releasing

Install the [GitHub CLI](https://cli.github.com): `brew install gh` or [follow the instructions](https://github.com/cli/cli#installation).

To release a new version:

* Submit a PR with the following changes (see [#30](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/30)):
* Submit a PR with the following changes (see e.g. [#30](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/30)):
* Update the version number in `version.rb`
* Update the CHANGELOG.md
* Create a section for the new version and move the unreleased version there
* Re-generate the example: `bundle exec rake example`
* Once merged, run the release task from main. Note that we prepend `gem_push=no` to avoid
pushing to RubyGems directly; our GitHub publish action will do this for us.
* `gem_push=no bundle exec rake release`
* Create a GitHub release:
* `gh release create v<version>`
* Edit the release notes to link to the notes in the CHANGELOG.md for the version
* Once merged and `main` is up-to-date, run `bundle exec rake release`.

## Contributing

Expand Down
14 changes: 14 additions & 0 deletions lib/twirp/protoc_plugin/core_ext/string/to_anchor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class String
# Converts the string to an acceptable URL anchor.
#
# Thw rules for GitHub markdown links are:
# - force lowercase
# - strip punctuation
# - replace spaces with dashes
# @return [String] the string converted to an acceptable URL anchor
def to_anchor
downcase.gsub(/[^a-z0-9_ -]/, "").tr(" ", "-")
end
end
15 changes: 15 additions & 0 deletions spec/core_ext/string/to_anchor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "twirp/protoc_plugin/core_ext/string/to_anchor"

RSpec.describe String do
describe "#to_anchor" do
it "converts a string with brackets, numbers, and dates" do
expect("[1.1.1] - 2024-05-22".to_anchor).to eq("111---2024-05-22")
end

it "converts a string with mixed case and backticks" do
expect("Install the `protoc-gen-twirp_ruby` plugin gem".to_anchor).to eq("install-the-protoc-gen-twirp_ruby-plugin-gem")
end
end
end
68 changes: 68 additions & 0 deletions tasks/release.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "date"
require "rake"
require "twirp/protoc_plugin/core_ext/string/to_anchor"

# See: https://github.com/rubygems/bundler-features/issues/81
# Remove the Bundler release task and override it with our own
Rake::Task["release"].clear

# Customize the release task originally defined at
# https://github.com/rubygems/rubygems/blob/v3.5.10/bundler/lib/bundler/gem_helper.rb#L67
#
# * Do NOT push to RubyGems (remove need to specify `gem_push=no` by
# removing `release:rubygem_push` dependency). The `release:source_control_push`
# task triggers a RubyGems release using our GitHub Action as a trusted publisher.
# * Create a GitHub release for the current version, with release notes
desc "Creates a release tag, pushes the tag to GitHub (which auto-releases to RubyGems), and creates a GitHub release."
task "release", [:remote] => %w[
build
release:guard_clean
release:source_control_push
release:create_github_release
]

desc "Gets the latest GitHub release version, e.g: v1.1.1"
task "release:latest_github_release" do
# We could use `git` here to get the most recent version tag, like:
# `git tag --list --sort='-version:refname' 'v*' | head -n1`
# but we go through the `gh` because we want the latest _GitHub_ release.
$stdout << `gh release view --json tagName --jq '.tagName'`
end

desc "Creates a GitHub release for v#{Bundler::GemHelper.gemspec.version}"
task "release:create_github_release" do
github_release = `bundle exec rake release:latest_github_release`.chomp
version = Bundler::GemHelper.gemspec.version.to_s

notes = <<~NOTES
See [CHANGELOG.md](#{changelog_link(version)}) for release details.
Full changes: #{compare_link(github_release, version)}
NOTES

`gh release create v#{version} --verify-tag --latest --notes "#{notes}"`
end

def repo_base_url
"https://github.com/collectiveidea/protoc-gen-twirp_ruby"
end

def changelog_link(version)
"#{repo_base_url}/blob/main/CHANGELOG.md##{changelog_heading(version).to_anchor}"
end

# @param version [String] the version, e.g. "1.2.0"
def changelog_heading(version)
# Assume the heading in the CHANGELOG.md is always a consistent
# format: "[1.1.1] - 2024-05-22"
"[#{version}] - #{DateTime.now.strftime("%Y-%m-%d")}"
end

# @param previous_tag [String] the previous version tag, e.g. "v1.1.1"
# @param current_version [String] the current version, e.g. "1.2.0"
def compare_link(previous_tag, current_version)
"#{repo_base_url}/compare/#{previous_tag}...v#{current_version}"
end

0 comments on commit 891adce

Please sign in to comment.