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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include <algorithm>
#include <cctype>
#include <charconv>
#include <limits>
#include <utility>
#include <vector>
#include "tools/Versions.hpp"
namespace Progmasoft::Catch3::Tools
{
namespace
{
constexpr std::string_view kVersionHeader
= "src/Progmasoft/Catch3/Version.hpp";
} // namespace
namespace
{
/// The dot-separated numbers of `text`, at most `limit` of them, none
/// empty or with a leading zero; nothing for anything else.
std::optional<std::vector<unsigned>>
Numbers(std::string_view text, std::size_t limit)
{
std::vector<unsigned> numbers;
while (true)
{
const auto dot = text.find('.');
const auto number = text.substr(0, dot);
if (numbers.size() == limit || number.empty()
|| (number.size() > 1 && number.front() == '0'))
{
return std::nullopt;
}
unsigned value = 0;
const auto *end = number.data() + number.size();
const auto parsed = std::from_chars(number.data(), end, value);
if (parsed.ec != std::errc{} || parsed.ptr != end)
{
return std::nullopt;
}
numbers.push_back(value);
if (dot == std::string_view::npos)
{
return numbers;
}
text.remove_prefix(dot + 1);
}
}
} // namespace
std::optional<Version>
ParseVersion(std::string_view text)
{
const auto numbers = Numbers(text, 3);
if (!numbers || numbers->size() != 3)
{
return std::nullopt;
}
return Version{ (*numbers)[0], (*numbers)[1], (*numbers)[2] };
}
std::string
FormatVersion(const Version &version)
{
return std::to_string(version[0]) + "." + std::to_string(version[1])
+ "." + std::to_string(version[2]);
}
std::optional<Version>
NextPatch(std::string_view current)
{
const auto numbers = Numbers(current, 4);
// A patch number at its largest has no next one; adding one would
// wrap around to 0 and move the version back.
if (!numbers || numbers->size() < 3
|| (*numbers)[2] == std::numeric_limits<unsigned>::max())
{
return std::nullopt;
}
return Version{ (*numbers)[0], (*numbers)[1], (*numbers)[2] + 1 };
}
const std::array<VersionSite, 5> &
VersionSites()
{
static constexpr std::array<VersionSite, 5> kSites{ {
{ "CMakeLists.txt", "\n VERSION ", "\n" },
{ "MODULE.bazel", "module(name = \"catch3\", version = \"", "\"" },
{ "meson.build", "\n version: '", "'" },
{ "xmake.lua", "\nset_version(\"", "\"" },
{ kVersionHeader, "kVersion = \"", "\"" },
} };
return kSites;
}
std::optional<std::string>
ReadVersionSite(std::string_view text, const VersionSite &site)
{
const auto before = text.find(site.Before);
if (before == std::string_view::npos)
{
return std::nullopt;
}
const auto first = before + site.Before.size();
const auto end = text.find(site.After, first);
if (end == std::string_view::npos)
{
return std::nullopt;
}
return std::string{ text.substr(first, end - first) };
}
std::optional<std::string>
WriteVersionSite(std::string_view text,
const VersionSite &site,
std::string_view version)
{
const auto current = ReadVersionSite(text, site);
if (!current)
{
return std::nullopt;
}
const auto first = text.find(site.Before) + site.Before.size();
std::string result{ text.substr(0, first) };
result += version;
result += text.substr(first + current->size());
return result;
}
std::optional<std::string>
CurrentVersion(const std::filesystem::path &root, Problems &problems)
{
const auto &sites = VersionSites();
const auto &site
= *std::find_if(sites.begin(),
sites.end(),
[](const VersionSite &candidate) {
return candidate.File == kVersionHeader;
});
auto version
= ReadVersionSite(ReadOrReport(root / site.File, problems), site);
if (!version)
{
problems.emplace_back("no version in Version.hpp");
}
return version;
}
void
CheckVersions(const std::filesystem::path &root, Problems &problems)
{
const auto current = CurrentVersion(root, problems);
if (!current)
{
return;
}
for (const auto &site : VersionSites())
{
const auto version
= ReadVersionSite(ReadOrReport(root / site.File, problems),
site);
if (version != current)
{
problems.push_back(std::string{ site.File } + " states "
+ version.value_or("no version")
+ ", Version.hpp states " + *current);
}
}
}
void
SetVersion(const std::filesystem::path &root,
const std::string &version,
Problems &problems)
{
std::vector<std::pair<std::filesystem::path, std::string>> writes;
for (const auto &site : VersionSites())
{
const auto path = root / site.File;
auto text
= WriteVersionSite(ReadOrReport(path, problems), site, version);
if (text)
{
writes.emplace_back(path, *std::move(text));
}
else
{
problems.push_back("cannot set the version in "
+ std::string{ site.File });
}
}
// All of them or none, so that the places never disagree.
if (!problems.empty())
{
return;
}
for (const auto &[path, text] : writes)
{
if (!WriteText(path, text))
{
problems.push_back("cannot write " + path.generic_string());
}
}
}
std::optional<std::string>
ReleaseNotesOf(std::string_view notes, std::string_view version)
{
// Every release is headed by its version as it was published: three
// numbers, or four for the releases before Semantic Versioning.
const auto heading = "\n## " + std::string{ version } + "\n";
const auto found = notes.find(heading);
if (found == std::string_view::npos)
{
return std::nullopt;
}
auto section = notes.substr(found + heading.size());
section = section.substr(0, section.find("\n## "));
return std::string{ Trimmed(section) } + "\n";
}
} // namespace Progmasoft::Catch3::Tools
|