Skip to content

Commit c29f102

Browse files
committed
Wrap versionning logic in its own class
This project "version" is special as it is based on the current date plus a trailing number to allow multiple releases on the same day. When cutting a new release, to determine version to use, we must check if the previous release was cut on the same day. If so, we only want to bumy hte trailing number, otherwise, we want to set the date to the current date and reset the trailing number to 1.
1 parent c4bac29 commit c29f102

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

tasks/vox.rake

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,53 @@
1-
def current_version
2-
@current_version ||= File.read(File.expand_path('../CHANGELOG.md', __dir__)).match(/^## \[([^\]]+)\]/) { |match| match[1] }
3-
rescue Errno::ENOENT
4-
puts 'Cannot find current version from CHANGELOG.md. Ignored'
5-
@current_version = ''
6-
end
1+
class Version
2+
attr_reader :date, :x, :raw
3+
4+
def initialize(v)
5+
@raw = v
6+
7+
if (m = v.match(%r{\A(?<date>\d{4}-\d{2}-\d{2})-(?<x>\d+)\z}))
8+
@date = m['date']
9+
@x = m['x'].to_i
10+
else
11+
@date = nil
12+
@x = 1
13+
end
14+
end
715

8-
def next_version
9-
today_prefix = Time.now.utc.strftime('%Y-%m-%d')
16+
def self.load_from_changelog
17+
changelog = File.expand_path('../CHANGELOG.md', __dir__)
18+
version = File.read(changelog).match(/^## \[([^\]]+)\]/) { |match| match[1] }
19+
new(version)
20+
rescue Errno::ENOENT
21+
new('')
22+
end
23+
24+
def to_s
25+
if malformed?
26+
raw
27+
else
28+
"#{date}-#{x}"
29+
end
30+
end
31+
32+
def next!
33+
if date == today
34+
@x += 1
35+
else
36+
@date = today
37+
@x = 1
38+
end
39+
40+
self
41+
end
42+
43+
private
44+
45+
def malformed?
46+
date.nil?
47+
end
1048

11-
if current_version.start_with?(today_prefix)
12-
current_version.next
13-
else
14-
"#{today_prefix}-1"
49+
def today
50+
Time.now.strftime('%Y-%m-%d')
1551
end
1652
end
1753

@@ -22,12 +58,12 @@ end
2258

2359
desc 'Get the current version of the project'
2460
task 'vox:version:current' do
25-
puts current_version
61+
puts Version.load_from_changelog
2662
end
2763

2864
desc 'Get the next version of the project'
2965
task 'vox:version:next' do
30-
puts next_version
66+
puts Version.load_from_changelog.next!
3167
end
3268

3369
begin
@@ -41,7 +77,7 @@ begin
4177
config.project = "puppet-runtime"
4278
config.exclude_labels = %w[dependencies duplicate question invalid wontfix wont-fix modulesync skip-changelog]
4379
config.since_tag = "202501080"
44-
config.future_release = next_version
80+
config.future_release = Version.load_from_changelog.next!.to_s
4581
end
4682
rescue LoadError
4783
task :changelog do

0 commit comments

Comments
 (0)