Skip to content

Commit

Permalink
Move release code
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Jul 26, 2022
1 parent 4e00b05 commit 7c9524d
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 101 deletions.
66 changes: 1 addition & 65 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/rake

require 'English'

unless defined?(Bundler)
Expand All @@ -12,69 +13,4 @@ POD_NAME = 'StencilSwiftKit'.freeze
MIN_XCODE_VERSION = 13.0
BUILD_DIR = File.absolute_path('./.build')

## [ Release a new version ] ##################################################

namespace :release do
desc 'Create a new release on CocoaPods'
task :new => [:check_versions, :check_tag_and_ask_to_release, 'spm:test', :cocoapods]

desc 'Check if all versions from the podspecs and CHANGELOG match'
task :check_versions do
results = []

# Check if bundler is installed first, as we'll need it for the cocoapods task (and we prefer to fail early)
`which bundler`
results << Utils.table_result(
$CHILD_STATUS.success?,
'Bundler installed',
'Please install bundler using `gem install bundler` and run `bundle install` first.'
)

# Extract version from podspec
podspec_version = Utils.podspec_version(POD_NAME)
Utils.table_info("#{POD_NAME}.podspec", podspec_version)

# Check if entry present in CHANGELOG
changelog_entry = system(%(grep -q '^## #{Regexp.quote(podspec_version)}$' CHANGELOG.md))
results << Utils.table_result(
changelog_entry,
'CHANGELOG, Entry added',
"Please add an entry for #{podspec_version} in CHANGELOG.md"
)

changelog_has_stable = system("grep -qi '^## Stable Branch' CHANGELOG.md")
results << Utils.table_result(
!changelog_has_stable,
'CHANGELOG, No stable',
'Please remove section for stable branch in CHANGELOG'
)

exit 1 unless results.all?
end

desc "Check tag and ask to release"
task :check_tag_and_ask_to_release do
results = []
podspec_version = Utils.podspec_version(POD_NAME)

tag_set = !`git ls-remote --tags . refs/tags/#{podspec_version}`.empty?
results << Utils.table_result(
tag_set,
'Tag pushed',
'Please create a tag and push it'
)

exit 1 unless results.all?

print "Release version #{podspec_version} [Y/n]? "
exit 2 unless STDIN.gets.chomp == 'Y'
end

desc "pod trunk push #{POD_NAME} to CocoaPods"
task :cocoapods do
Utils.print_header 'Pushing pod to CocoaPods Trunk'
sh "bundle exec pod trunk push #{POD_NAME}.podspec"
end
end

task :default => 'spm:test'
29 changes: 12 additions & 17 deletions rakelib/changelog.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

# Used constants:
# none
# _none_

require_relative 'check_changelog'

namespace :changelog do
desc 'Add the empty CHANGELOG entries after a new release'
Expand Down Expand Up @@ -35,23 +39,14 @@ namespace :changelog do

desc 'Check if links to issues and PRs use matching numbers between text & link'
task :check do
current_repo = File.basename(`git remote get-url origin`.chomp, '.git').freeze
slug_re = '([a-zA-Z]*/[a-zA-Z]*)'
links = %r{\[#{slug_re}?\#([0-9]+)\]\(https://github.com/#{slug_re}/(issues|pull)/([0-9]+)\)}
all_wrong_links = []
File.readlines('CHANGELOG.md').each_with_index do |line, idx|
wrong_links = line.scan(links).reject do |m|
slug = m[0] || "SwiftGen/#{current_repo}"
(slug == m[2]) && (m[1] == m[4])
end
all_wrong_links.concat Array(wrong_links.map do |m|
" - Line #{idx + 1}, link text is #{m[0]}##{m[1]} but links points to #{m[2]}##{m[4]}"
end)
end
if all_wrong_links.empty?
puts "\u{2705} All links correct"
warnings = check_changelog
if warnings.empty?
puts "\u{2705} All entries seems OK (end with period + 2 spaces, correct links)"
else
puts "\u{274C} Some wrong links found:\n" + all_wrong_links.join("\n")
puts "\u{274C} Some warnings were found:\n" + Array(warnings.map do |warning|
" - Line #{warning[:line]}: #{warning[:message]}"
end).join("\n")
exit 1
end
end

Expand Down
61 changes: 61 additions & 0 deletions rakelib/check_changelog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

# This analyze the CHANGELOG.md file and report warnings on its content
#
# It checks:
# - if the description part of each entry ends with a period and two spaces
# - that all links to PRs & issues with format [#nn](repo_url/nn) are consistent
# (use the same number in the link title and URL)
#
# @return Array of Hashes with keys `:line` & `:message` for each element
#
def check_changelog
current_repo = File.basename(`git remote get-url origin`.chomp, '.git').freeze
slug_re = '([a-zA-Z]*/[a-zA-Z]*)'
links = %r{\[#{slug_re}?\#([0-9]+)\]\(https://github.com/#{slug_re}/(issues|pull)/([0-9]+)\)}
links_typos = %r{https://github.com/#{slug_re}/(issue|pulls)/([0-9]+)}

all_warnings = []
inside_entry = false
last_line_has_correct_ending = false

File.readlines('CHANGELOG.md').each_with_index do |line, idx|
line.chomp! # Remove \n the end, it's easier for checks below
was_inside_entry = inside_entry
just_started_new_entry = line.start_with?('* ')
inside_entry = true if just_started_new_entry
inside_entry = false if /^ \[.*\]\(.*\)$/ =~ line # link-only line

if was_inside_entry && !inside_entry && !last_line_has_correct_ending
# We just ended an entry's description by starting the links, but description didn't end with '. '
# Note: entry descriptions can be on multiple lines, hence the need to wait for the next line
# to not be inside an entry to be able to consider the previous line as the end of entry description.
all_warnings.concat [
{ line: idx, message: 'Line describing your entry should end with a period and 2 spaces.' }
]
end
# Store if current line has correct ending, for next iteration, so that if the next line isn't
# part of the entry description, we can check if previous line ends description correctly.
# Also, lines just linking to CHANGELOG to other repositories (StencilSwiftKit & Stencil mainly)
# should be considered as not needing the '. ' ending.
last_line_has_correct_ending = line.end_with?('. ') || line.end_with?('/CHANGELOG.md)')

# Now, check that links [#nn](.../nn) have matching numbers in link title & URL
wrong_links = line.scan(links).reject do |m|
slug = m[0] || "SwiftGen/#{current_repo}"
(slug == m[2]) && (m[1] == m[4])
end
all_warnings.concat Array(wrong_links.map do |m|
link_text = "#{m[0]}##{m[1]}"
link_url = "#{m[2]}##{m[4]}"
{ line: idx + 1, message: "Link text is #{link_text} but links points to #{link_url}." }
end)

# Flag common typos in GitHub issue/PR URLs
typo_links = line.scan(links_typos)
all_warnings.concat Array(typo_links.map do |_|
{ line: idx + 1, message: 'This looks like a GitHub link URL with a typo. Issue links should use `/issues/123` (plural) and PR links should use `/pull/123` (singular).' }
end)
end
all_warnings
end
2 changes: 2 additions & 0 deletions rakelib/lint.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Used constants:
# - BUILD_DIR

Expand Down
2 changes: 2 additions & 0 deletions rakelib/pod.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Used constants:
# - POD_NAME

Expand Down
76 changes: 76 additions & 0 deletions rakelib/release.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

# Used constants:
# - BUILD_DIR

def first_match_in_file(file, regexp)
File.foreach(file) do |line|
m = regexp.match(line)
return m if m
end
end

## [ Release a new version ] ##################################################

namespace :release do
desc 'Create a new release on CocoaPods'
task :new => [:check_versions, :check_tag_and_ask_to_release, 'spm:test', :cocoapods]

desc 'Check if all versions from the podspecs and CHANGELOG match'
task :check_versions do
results = []

# Check if bundler is installed first, as we'll need it for the cocoapods task (and we prefer to fail early)
`which bundler`
results << Utils.table_result(
$CHILD_STATUS.success?,
'Bundler installed',
'Please install bundler using `gem install bundler` and run `bundle install` first.'
)

# Extract version from podspec
podspec_version = Utils.podspec_version(POD_NAME)
Utils.table_info("#{POD_NAME}.podspec", podspec_version)

# Check if entry present in CHANGELOG
changelog_entry = first_match_in_file('CHANGELOG.md', /^## #{Regexp.quote(podspec_version)}$/)
results << Utils.table_result(
changelog_entry,
'CHANGELOG, Entry added',
"Please add an entry for #{podspec_version} in CHANGELOG.md"
)

changelog_has_stable = system("grep -qi '^## Stable Branch' CHANGELOG.md")
results << Utils.table_result(
!changelog_has_stable,
'CHANGELOG, No stable',
'Please remove section for stable branch in CHANGELOG'
)

exit 1 unless results.all?
end

desc "Check tag and ask to release"
task :check_tag_and_ask_to_release do
results = []
podspec_version = Utils.podspec_version(POD_NAME)

tag_set = !`git ls-remote --tags . refs/tags/#{podspec_version}`.empty?
results << Utils.table_result(
tag_set,
'Tag pushed',
'Please create a tag and push it'
)

exit 1 unless results.all?

print "Release version #{podspec_version} [Y/n]? "
exit 2 unless STDIN.gets.chomp == 'Y'
end

desc "pod trunk push #{POD_NAME} to CocoaPods"
task :cocoapods do
Utils.print_header 'Pushing pod to CocoaPods Trunk'
sh "bundle exec pod trunk push #{POD_NAME}.podspec"
end
end
5 changes: 3 additions & 2 deletions rakelib/spm.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Used constants:
# none
# frozen_string_literal: true

# Used constants:
# _none_

namespace :spm do
desc 'Build using SPM'
Expand Down
Loading

0 comments on commit 7c9524d

Please sign in to comment.