-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
99 lines (79 loc) · 3.07 KB
/
Rakefile
File metadata and controls
99 lines (79 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/testtask"
require "standard/rake"
require_relative "lib/git/markdown/version"
VERSION_PATH = File.expand_path("lib/git/markdown/version.rb", __dir__)
VALID_RELEASE_TARGETS = %w[major minor patch].freeze
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end
def current_branch
`git branch --show-current`.strip
end
def clean_worktree?
system("git diff --quiet") && system("git diff --cached --quiet")
end
def release_version(target)
target = target.to_s.strip
raise ArgumentError, "Provide patch, minor, major, or an explicit X.Y.Z version." if target.empty?
return target if target.match?(/\A\d+\.\d+\.\d+\z/)
unless VALID_RELEASE_TARGETS.include?(target)
raise ArgumentError, "Invalid release target #{target.inspect}. Use #{VALID_RELEASE_TARGETS.join(", ")} or X.Y.Z."
end
major, minor, patch = GitMarkdown::VERSION.split(".").map(&:to_i)
case target
when "major"
"#{major + 1}.0.0"
when "minor"
"#{major}.#{minor + 1}.0"
when "patch"
"#{major}.#{minor}.#{patch + 1}"
end
end
def update_version_file(version)
File.write(
VERSION_PATH,
<<~RUBY
# frozen_string_literal: true
module GitMarkdown
VERSION = "#{version}"
end
RUBY
)
end
def update_changelog(version)
success = system("git-cliff", "-c", "cliff.toml", "--unreleased", "--tag", "v#{version}", "-o", "CHANGELOG.md")
raise "git-cliff failed. Install git-cliff and make sure cliff.toml is valid." unless success
raise "git-cliff did not update CHANGELOG.md. Ensure there are Conventional Commits since the last tag." if system("git", "diff", "--quiet", "--", "CHANGELOG.md")
end
if Rake::Task.task_defined?("release")
Rake::Task["release"].clear
end
desc "Publishing is handled by GitHub Actions. Use release:prepare[...] instead."
task :release do
abort "Use `bundle exec rake 'release:prepare[patch]'` (or minor/major/X.Y.Z). Publishing runs in GitHub Actions after the tag is pushed."
end
namespace :release do
desc "Prepare a release: update CHANGELOG/version, commit, tag, and push. Accepts patch, minor, major, or X.Y.Z."
task :prepare, [:target] do |_task, args|
branch = current_branch
abort "Release must run on main or master. Current branch: #{branch.inspect}." unless %w[main master].include?(branch)
abort "Release requires a clean working tree." unless clean_worktree?
version = release_version(args[:target])
current = GitMarkdown::VERSION
abort "Release version #{version} is older than current version #{current}." if Gem::Version.new(version) < Gem::Version.new(current)
update_changelog(version)
update_version_file(version)
sh "git add CHANGELOG.md lib/git/markdown/version.rb"
sh %(git commit -m "chore(release): prepare v#{version}")
sh %(git tag -a v#{version} -m "Release v#{version}")
sh "git push origin #{branch}"
sh "git push origin v#{version}"
rescue ArgumentError, RuntimeError => e
abort e.message
end
end
task default: %i[test standard]