-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathresolver.cppm
More file actions
186 lines (157 loc) · 6.96 KB
/
resolver.cppm
File metadata and controls
186 lines (157 loc) · 6.96 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
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
// mcpp.pm.resolver — turn a SemVer constraint into a concrete version,
// using the package's xpkg lua descriptor as the version inventory.
//
// Part of the package-management subsystem refactor (PR-R4 in
// `.agents/docs/2026-05-08-pm-subsystem-architecture.md`). Strictly
// pulled out of `cli.cppm` with no behavior change; the same
// signatures, the same error strings, the same platform key picking.
//
// Implementation note: `resolve_semver` is **not** declared inline on
// purpose. Inlining it across modules makes every importer
// (cli.cppm, ...) instantiate `std::_Vector_base<vr::Version>`'s
// destructor locally. With musl-gcc 15.1's libstdc++ that triggers
// `undefined reference to std::_Vector_base<...>::_Vector_impl::~_Vector_impl()`
// at link time. Single-definition-point sidesteps the bug.
export module mcpp.pm.resolver;
import std;
import mcpp.manifest;
import mcpp.platform;
import mcpp.pm.compat;
import mcpp.pm.package_fetcher;
import mcpp.version_req;
export namespace mcpp::pm {
// xpkg.lua's `xpm.<key>` uses these names. (Distinct from
// `kCurrentPlatform` in cli.cppm, which is the [toolchain] table key —
// "macos" vs "macosx".)
inline constexpr std::string_view kXpkgPlatform = mcpp::platform::xpkg_platform;
// Returns true if `v` is a SemVer constraint (caret, tilde, range, glob,
// `*`, or empty) rather than a literal exact version. Empty counts as
// "constraint" so callers re-resolve via the index — bare `1.2.3` is
// treated as exact for back-compat with pre-SemVer pinning workflows;
// users opt into resolution by writing `^1.2.3` etc.
bool is_version_constraint(std::string_view v);
// ─── Namespace-aware overloads (canonical, 0.0.10+) ─────────────────
// Resolve a SemVer constraint against the index entry's available
// versions. Returns the chosen exact version string, or an error
// message. Uses structured (ns, shortName) for index lookup.
std::expected<std::string, std::string>
resolve_semver(std::string_view ns, std::string_view shortName,
std::string_view constraint,
mcpp::pm::Fetcher& fetcher);
// Try to AND-merge two version constraints and resolve to a single
// concrete version satisfying both. Uses structured (ns, shortName).
std::expected<std::string, std::string>
try_merge_semver(std::string_view ns, std::string_view shortName,
std::string_view a,
std::string_view b,
mcpp::pm::Fetcher& fetcher);
// ─── Legacy overloads (COMPAT, remove in 1.0.0) ─────────────────────
std::expected<std::string, std::string>
resolve_semver(std::string_view name,
std::string_view constraint,
mcpp::pm::Fetcher& fetcher);
std::expected<std::string, std::string>
try_merge_semver(std::string_view name,
std::string_view a,
std::string_view b,
mcpp::pm::Fetcher& fetcher);
} // namespace mcpp::pm
namespace mcpp::pm {
bool is_version_constraint(std::string_view v) {
if (v.empty()) return true;
if (v == "*") return true;
char c = v.front();
if (c == '^' || c == '~' || c == '>' || c == '<' || c == '=') return true;
if (v.find(',') != std::string_view::npos) return true;
return false;
}
// ─── Namespace-aware resolve_semver (canonical, 0.0.10+) ─────────────
std::expected<std::string, std::string>
resolve_semver(std::string_view ns, std::string_view shortName,
std::string_view constraint,
mcpp::pm::Fetcher& fetcher)
{
namespace vr = mcpp::version_req;
auto qname = mcpp::pm::compat::qualified_name(ns, shortName);
auto luaContent = fetcher.read_xpkg_lua(ns, shortName);
if (!luaContent) {
return std::unexpected(std::format(
"dependency '{}' has SemVer constraint '{}' but the index entry "
"isn't cloned locally yet — run `mcpp index update` first",
qname, constraint));
}
auto req = vr::parse_req(constraint);
if (!req) {
return std::unexpected(std::format(
"dependency '{}': invalid version constraint '{}': {}",
qname, constraint, req.error()));
}
auto rawVersions = mcpp::manifest::list_xpkg_versions(*luaContent, kXpkgPlatform);
if (rawVersions.empty()) {
return std::unexpected(std::format(
"dependency '{}': index entry has no versions for platform '{}'",
qname, kXpkgPlatform));
}
std::vector<vr::Version> parsed;
parsed.reserve(rawVersions.size());
for (auto& s : rawVersions) {
auto v = vr::parse_version(s);
if (!v) continue; // ignore unparseable entries
parsed.push_back(*v);
}
if (parsed.empty()) {
return std::unexpected(std::format(
"dependency '{}': no valid versions in index", qname));
}
auto idx = vr::choose(*req, parsed);
if (!idx) {
std::string avail;
for (auto& s : rawVersions) { if (!avail.empty()) avail += ", "; avail += s; }
return std::unexpected(std::format(
"dependency '{}': constraint '{}' matches none of: [{}]",
qname, constraint, avail));
}
return parsed[*idx].str();
}
// ─── Namespace-aware try_merge_semver (canonical, 0.0.10+) ───────────
std::expected<std::string, std::string>
try_merge_semver(std::string_view ns, std::string_view shortName,
std::string_view a,
std::string_view b,
mcpp::pm::Fetcher& fetcher)
{
auto canon = [](std::string_view v) -> std::string {
if (v.empty() || v == "*") return std::string{};
if (is_version_constraint(v)) return std::string(v);
return "=" + std::string(v);
};
std::string ca = canon(a);
std::string cb = canon(b);
std::string merged;
if (!ca.empty() && !cb.empty()) merged = ca + "," + cb;
else if (!ca.empty()) merged = ca;
else if (!cb.empty()) merged = cb;
else merged = "*";
return resolve_semver(ns, shortName, merged, fetcher);
}
// ─── Legacy overloads (COMPAT, remove in 1.0.0) ─────────────────────
std::expected<std::string, std::string>
resolve_semver(std::string_view name,
std::string_view constraint,
mcpp::pm::Fetcher& fetcher)
{
auto resolved = mcpp::pm::compat::resolve_package_name(name, "");
return resolve_semver(resolved.namespace_, resolved.shortName,
constraint, fetcher);
}
std::expected<std::string, std::string>
try_merge_semver(std::string_view name,
std::string_view a,
std::string_view b,
mcpp::pm::Fetcher& fetcher)
{
auto resolved = mcpp::pm::compat::resolve_package_name(name, "");
return try_merge_semver(resolved.namespace_, resolved.shortName,
a, b, fetcher);
}
} // namespace mcpp::pm