Release 0.14.0 (#300)

* Introduce script to automate release

* Rakefile levelup

* Version 0.14.0

* Fix newline at end of podspec.json

* Ensure we start and end on master branch

And that we pull latest master before tagging

* CRLF at EOF

* Remove [:version] param from `release:finish` task

It can be guessed from the current podspec version

* Fix create_release task

* Ensure we run rake via bundle exec

Co-authored-by: David Jennes <djbe@users.noreply.github.com>

Co-authored-by: David Jennes <djbe@users.noreply.github.com>
This commit is contained in:
Olivier Halligon
2020-08-17 20:42:00 +02:00
committed by GitHub
parent 19646bcddf
commit e93b33423b
12 changed files with 335 additions and 8 deletions

34
rakelib/changelog.rake Normal file
View File

@@ -0,0 +1,34 @@
NEW_CHANGELOG_SECTION = "## Master\n" + ['Breaking', 'Enhancements', 'Deprecations', 'Bug Fixes', 'Internal Changes'].map do |s|
<<~MARKDOWN
### #{s}
_None_
MARKDOWN
end.join
def changelog_first_section
content = []
section_count = 0
File.foreach(CHANGELOG_FILE) do |line|
section_count += 1 if line.start_with?('## ')
break if section_count > 1
content.append(line) if section_count == 1
end
content[1..].join
end
namespace :changelog do
# rake changelog:reset
desc "Add a new empty section at the top of the changelog and git push it"
task :reset do
header "Reset CHANGELOG"
content = File.read(CHANGELOG_FILE)
new_content = NEW_CHANGELOG_SECTION + "\n" + content
File.write(CHANGELOG_FILE, new_content)
sh("git", "add", CHANGELOG_FILE)
sh("git", "commit", "-m", "Reset CHANGELOG")
sh("git", "push")
end
end

52
rakelib/github.rake Normal file
View File

@@ -0,0 +1,52 @@
require 'octokit'
def repo_slug
url_parts = `git remote get-url origin`.chomp.split(%r{/|:})
last_two_parts = url_parts[-2..-1].join('/')
last_two_parts.gsub(/\.git$/, '')
end
def github_client
Octokit::Client.new(:netrc => true)
end
namespace :github do
# rake github:create_release_pr[version]
task :create_release_pr, [:version] do |_, args|
version = args[:version]
branch = release_branch(version)
title = "Release #{version}"
body = <<~BODY
This PR prepares the release for version #{version}.
Once the PR is merged into master, run `bundle exec rake release:finish` to tag and push to trunk.
BODY
header "Opening PR"
res = github_client.create_pull_request(repo_slug, "master", branch, title, body)
info "Pull request created: #{res['html_url']}"
end
# rake github:tag
task :tag do
tag = current_pod_version
sh("git", "tag", tag)
sh("git", "push", origin, tag)
end
# rake github:create_release
task :create_release do
tag_name = current_pod_version
title = tag_name
body = changelog_first_section()
res = github_client.create_release(repo_slug, tag_name, name: title, body: body)
info "GitHub Release created: #{res['html_url']}"
end
# rake github:pull_master
task :pull_master do
sh("git", "switch", "master")
sh("git", "pull")
end
end

21
rakelib/pod.rake Normal file
View File

@@ -0,0 +1,21 @@
require 'json'
def current_pod_version
JSON.parse(File.read(PODSPEC_FILE))['version']
end
namespace :pod do
# rake pod:lint
desc "Lint the podspec"
task :lint do
header "Linting podspec"
sh("pod", "lib", "lint", PODSPEC_FILE)
end
# rake pod:push
desc "Push the podspec to trunk"
task :push do
header "Pushing podspec to trunk"
sh("pod", "trunk", "push", PODSPEC_FILE)
end
end

67
rakelib/release.rake Normal file
View File

@@ -0,0 +1,67 @@
require 'json'
namespace :release do
# rake release:new
desc "Ask for a version number and prepare a release PR for that version"
task :new do
info "Current version is: #{current_pod_version}"
print "What version do you want to release? "
new_version = STDIN.gets.chomp
Rake::Task['release:start'].invoke(new_version)
end
# rake release:start[version]
desc "Start a release by creating a PR with the required changes to bump the version"
task :start, [:version] => ['release:create_branch', 'release:update_files', 'pod:lint', 'release:push_branch', 'github:create_release_pr', 'github:pull_master']
# rake release:finish[version]
desc "Finish a release after the PR has been merged, by tagging master and pushing to trunk"
task :finish => ['github:pull_master', 'github:tag', 'pod:push', 'github:create_release', 'changelog:reset']
### Helper tasks ###
# rake release:create_branch[version]
task :create_branch, [:version] do |_, args|
branch = release_branch(args[:version])
header "Creating release branch"
sh("git", "checkout", "-b", branch)
end
# rake release:update_files[version]
task :update_files, [:version] do |_, args|
version = args[:version]
header "Updating files for version #{version}"
podspec = JSON.parse(File.read(PODSPEC_FILE))
podspec['version'] = version
podspec['source']['tag'] = version
File.write(PODSPEC_FILE, JSON.pretty_generate(podspec) + "\n")
replace(CHANGELOG_FILE, '## Master' => "\#\# #{version}")
replace("docs/conf.py",
/^version = .*/ => %Q(version = '#{version}'),
/^release = .*/ => %Q(release = '#{version}')
)
replace("docs/installation.rst",
/pod 'Stencil', '.*'/ => %Q(pod 'Stencil', '~> #{version}'),
/github "stencilproject\/Stencil" ~> .*/ => %Q(github "stencilproject/Stencil" ~> #{version})
)
## Commit Changes
sh("git", "add", PODSPEC_FILE, CHANGELOG_FILE, "docs/*")
sh("git", "commit", "-m", "Version #{version}")
end
# rake release:push_branch[version]
task :push_branch, [:version] do |_, args|
branch = release_branch(args[:version])
header "Pushing #{branch} to origin"
sh("git", "push", "-u", "origin", branch)
end
end

28
rakelib/utils.rake Normal file
View File

@@ -0,0 +1,28 @@
def colorize(string, *codes)
if `tput colors`.chomp.to_i >= 8
code = codes.join(';')
puts "\e[#{code}m" + string + "\e[0m"
else
puts string
end
end
def header(title)
puts colorize("==> #{title}...", 1, 32) # bold, green
end
def info(string)
puts colorize(string, 34) # blue
end
def release_branch(version)
"release/#{version}"
end
def replace(file, replacements)
content = File.read(file)
replacements.each do |match, replacement|
content.gsub!(match, replacement)
end
File.write(file, content)
end