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
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <system_error>
#include "tools/Repository.hpp"
namespace Progmasoft::Catch3::Tools
{
std::optional<std::string>
ReadEnvironment(const char *name)
{
#if defined(_WIN32)
char *value = nullptr;
std::size_t length = 0;
if (_dupenv_s(&value, &length, name) != 0 || value == nullptr)
{
return std::nullopt;
}
std::string result{ value };
std::free(value);
return result;
#else
const char *value = std::getenv(name);
return value == nullptr ? std::nullopt
: std::optional<std::string>{ value };
#endif
}
std::optional<std::filesystem::path>
RepositoryRoot()
{
const auto root = ReadEnvironment("BUILD_WORKSPACE_DIRECTORY");
if (!root)
{
return std::nullopt;
}
return std::filesystem::path{ *root };
}
std::optional<std::string>
ReadText(const std::filesystem::path &file)
{
std::ifstream stream{ file, std::ios::binary };
if (!stream)
{
return std::nullopt;
}
const std::string bytes{ std::istreambuf_iterator<char>{ stream },
std::istreambuf_iterator<char>{} };
std::string text;
text.reserve(bytes.size());
for (std::size_t index = 0; index < bytes.size(); ++index)
{
if (bytes[index] != '\r' || index + 1 == bytes.size()
|| bytes[index + 1] != '\n')
{
text.push_back(bytes[index]);
}
}
return text;
}
std::string
ReadOrReport(const std::filesystem::path &file, Problems &problems)
{
auto text = ReadText(file);
if (!text)
{
problems.push_back("cannot read " + file.generic_string());
return {};
}
return *std::move(text);
}
bool
WriteText(const std::filesystem::path &file, std::string_view text)
{
std::ofstream stream{ file, std::ios::binary | std::ios::trunc };
stream.write(text.data(), static_cast<std::streamsize>(text.size()));
return static_cast<bool>(stream);
}
std::vector<std::string>
SplitLines(std::string_view text)
{
std::vector<std::string> lines;
std::size_t start = 0;
while (start < text.size())
{
const auto end = text.find('\n', start);
const auto next
= end == std::string_view::npos ? text.size() : end + 1;
lines.emplace_back(text.substr(start, next - start));
start = next;
}
return lines;
}
std::string_view
Trimmed(std::string_view text)
{
constexpr std::string_view kSpace = " \t\r\n";
const auto first = text.find_first_not_of(kSpace);
if (first == std::string_view::npos)
{
return {};
}
return text.substr(first, text.find_last_not_of(kSpace) - first + 1);
}
bool
StartsWith(std::string_view text, std::string_view prefix)
{
return text.substr(0, prefix.size()) == prefix;
}
bool
EndsWith(std::string_view text, std::string_view suffix)
{
return text.size() >= suffix.size()
&& text.substr(text.size() - suffix.size()) == suffix;
}
std::set<std::string>
UpstreamFiles(const std::filesystem::path &root, Problems &problems)
{
std::set<std::string> files;
const auto listing
= ReadOrReport(root / "tools" / "upstreamFiles.txt", problems);
for (const auto &line : SplitLines(listing))
{
const auto entry = Trimmed(line);
if (!entry.empty() && entry.front() != '#')
{
files.emplace(entry);
}
}
return files;
}
std::vector<std::string>
FilesBelow(const std::filesystem::path &root,
const std::filesystem::path &directory,
const std::vector<std::string> &extensions,
bool recursive)
{
namespace fs = std::filesystem;
std::vector<std::string> files;
const auto add = [&](const fs::directory_entry &entry) {
const auto extension = entry.path().extension().string();
if (entry.is_regular_file()
&& std::find(extensions.begin(), extensions.end(), extension)
!= extensions.end())
{
files.push_back(
entry.path().lexically_relative(root).generic_string());
}
};
std::error_code error;
if (recursive)
{
for (const auto &entry :
fs::recursive_directory_iterator{ directory, error })
{
add(entry);
}
}
else
{
for (const auto &entry : fs::directory_iterator{ directory, error })
{
add(entry);
}
}
std::sort(files.begin(), files.end());
return files;
}
int
Finish(const Problems &problems)
{
for (const auto &problem : problems)
{
std::cerr << problem << '\n';
}
return problems.empty() ? 0 : 1;
}
} // namespace Progmasoft::Catch3::Tools
|