Skip to content

Commit e1996b0

Browse files
committed
Migrate legacy xlings index name
1 parent 01cd739 commit e1996b0

2 files changed

Lines changed: 155 additions & 1 deletion

File tree

src/config.cppm

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,75 @@ bool write_default_xlings_json(const std::filesystem::path& path,
216216
return std::filesystem::exists(path);
217217
}
218218

219+
bool replace_all(std::string& text, std::string_view from, std::string_view to) {
220+
bool changed = false;
221+
for (std::size_t pos = 0;
222+
(pos = text.find(from, pos)) != std::string::npos;) {
223+
text.replace(pos, from.size(), to);
224+
pos += to.size();
225+
changed = true;
226+
}
227+
return changed;
228+
}
229+
230+
bool write_text_if_changed(const std::filesystem::path& path,
231+
const std::string& original,
232+
const std::string& updated) {
233+
if (updated == original) return false;
234+
write_file(path, updated);
235+
return true;
236+
}
237+
238+
bool migrate_legacy_config_toml_index_names(const std::filesystem::path& path) {
239+
std::ifstream is(path);
240+
if (!is) return false;
241+
std::stringstream ss;
242+
ss << is.rdbuf();
243+
auto original = ss.str();
244+
auto updated = original;
245+
246+
replace_all(updated, "default = \"mcpp-index\"", "default = \"mcpplibs\"");
247+
replace_all(updated, "[index.repos.\"mcpp-index\"]", "[index.repos.\"mcpplibs\"]");
248+
249+
return write_text_if_changed(path, original, updated);
250+
}
251+
252+
bool migrate_legacy_xlings_json_index_names(const std::filesystem::path& path) {
253+
std::ifstream is(path);
254+
if (!is) return false;
255+
std::stringstream ss;
256+
ss << is.rdbuf();
257+
auto original = ss.str();
258+
auto updated = original;
259+
260+
// Older mcpp sandboxes seeded the package index under the repository
261+
// name. Keep the URL and all xlings state intact; only rename the index
262+
// key so xlings config/list output matches mcpp's default namespace.
263+
replace_all(updated, "\"name\": \"mcpp-index\"", "\"name\": \"mcpplibs\"");
264+
replace_all(updated, "\"name\":\"mcpp-index\"", "\"name\":\"mcpplibs\"");
265+
266+
return write_text_if_changed(path, original, updated);
267+
}
268+
269+
void canonicalize_legacy_index_names(GlobalConfig& cfg) {
270+
if (cfg.defaultIndex == "mcpp-index")
271+
cfg.defaultIndex = "mcpplibs";
272+
273+
std::vector<IndexRepo> normalized;
274+
for (auto r : cfg.indexRepos) {
275+
if (r.name == "mcpp-index"
276+
&& r.url == "https://github.com/mcpp-community/mcpp-index.git") {
277+
r.name = "mcpplibs";
278+
}
279+
bool duplicate = std::any_of(normalized.begin(), normalized.end(),
280+
[&](const IndexRepo& existing) {
281+
return existing.name == r.name && existing.url == r.url;
282+
});
283+
if (!duplicate) normalized.push_back(std::move(r));
284+
}
285+
cfg.indexRepos = std::move(normalized);
286+
}
287+
219288
// Try to acquire xlings binary. Returns the path if successful.
220289
std::expected<std::filesystem::path, std::string>
221290
acquire_xlings_binary(const std::filesystem::path& destBin, bool quiet)
@@ -345,6 +414,7 @@ std::expected<GlobalConfig, ConfigError> load_or_init(
345414
// 3. Seed config.toml if missing
346415
bool fresh_config = !std::filesystem::exists(cfg.configFile);
347416
if (fresh_config) write_default_config_toml(cfg.configFile);
417+
else migrate_legacy_config_toml_index_names(cfg.configFile);
348418

349419
// 4. Load config.toml
350420
auto doc = t::parse_file(cfg.configFile);
@@ -383,11 +453,16 @@ std::expected<GlobalConfig, ConfigError> load_or_init(
383453
cfg.indexRepos.push_back({ std::string(name), std::string(url) });
384454
};
385455
add_default("mcpplibs", "https://github.com/mcpp-community/mcpp-index.git");
456+
canonicalize_legacy_index_names(cfg);
386457

387-
// 5. Seed registry/.xlings.json if missing
458+
// 5. Seed registry/.xlings.json if missing; migrate legacy cached
459+
// sandboxes in place so CI/user caches move from mcpp-index to mcpplibs
460+
// without losing xlings' active subos or version bindings.
388461
auto xjson = cfg.xlingsHome() / ".xlings.json";
389462
if (!std::filesystem::exists(xjson)) {
390463
write_default_xlings_json(xjson, cfg.indexRepos);
464+
} else {
465+
migrate_legacy_xlings_json_index_names(xjson);
391466
}
392467

393468
// 6. Acquire xlings binary if needed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
# 39_xlings_index_migration.sh - legacy mcpp-index cache migrates to mcpplibs.
3+
set -e
4+
5+
TMP=$(mktemp -d)
6+
trap 'rm -rf "$TMP"' EXIT
7+
8+
export MCPP_HOME="$TMP/mcpp-home"
9+
10+
if [[ -z "${MCPP_VENDORED_XLINGS:-}" && -x "$HOME/.mcpp/registry/bin/xlings" ]]; then
11+
export MCPP_VENDORED_XLINGS="$HOME/.mcpp/registry/bin/xlings"
12+
fi
13+
14+
source "$(dirname "$0")/_inherit_toolchain.sh"
15+
16+
cat > "$MCPP_HOME/config.toml" <<'TOML'
17+
[xlings]
18+
binary = "bundled"
19+
home = ""
20+
21+
[index]
22+
default = "mcpp-index"
23+
24+
[index.repos."mcpp-index"]
25+
url = "https://github.com/mcpp-community/mcpp-index.git"
26+
27+
[cache]
28+
search_ttl_seconds = 3600
29+
30+
[build]
31+
default_jobs = 0
32+
default_backend = "ninja"
33+
TOML
34+
35+
mkdir -p "$MCPP_HOME/registry"
36+
cat > "$MCPP_HOME/registry/.xlings.json" <<'JSON'
37+
{
38+
"activeSubos": "default",
39+
"index_repos": [
40+
{
41+
"name": "mcpp-index",
42+
"url": "https://github.com/mcpp-community/mcpp-index.git"
43+
}
44+
],
45+
"lang": "en",
46+
"mirror": "GLOBAL",
47+
"subos": {
48+
"default": {
49+
"dir": ""
50+
}
51+
}
52+
}
53+
JSON
54+
55+
"$MCPP" self env > "$TMP/env.log"
56+
57+
grep -q 'default = "mcpplibs"' "$MCPP_HOME/config.toml" || {
58+
cat "$MCPP_HOME/config.toml"; echo "config.toml default index was not migrated"; exit 1; }
59+
grep -q '\[index.repos."mcpplibs"\]' "$MCPP_HOME/config.toml" || {
60+
cat "$MCPP_HOME/config.toml"; echo "config.toml repo table was not migrated"; exit 1; }
61+
if grep -q 'default = "mcpp-index"' "$MCPP_HOME/config.toml" \
62+
|| grep -q '\[index.repos."mcpp-index"\]' "$MCPP_HOME/config.toml"; then
63+
cat "$MCPP_HOME/config.toml"; echo "config.toml still contains legacy index key"; exit 1
64+
fi
65+
66+
grep -q '"name": "mcpplibs"' "$MCPP_HOME/registry/.xlings.json" || {
67+
cat "$MCPP_HOME/registry/.xlings.json"; echo ".xlings.json repo name was not migrated"; exit 1; }
68+
if grep -q '"name": "mcpp-index"' "$MCPP_HOME/registry/.xlings.json"; then
69+
cat "$MCPP_HOME/registry/.xlings.json"; echo ".xlings.json still contains legacy index name"; exit 1
70+
fi
71+
72+
"$MCPP" self config > "$TMP/self-config.log"
73+
grep -q 'mcpplibs' "$TMP/self-config.log" || {
74+
cat "$TMP/self-config.log"; echo "self config did not show mcpplibs"; exit 1; }
75+
if grep -q 'index-repo.*mcpp-index[[:space:]]*:' "$TMP/self-config.log"; then
76+
cat "$TMP/self-config.log"; echo "self config still shows legacy index key"; exit 1
77+
fi
78+
79+
echo "OK"

0 commit comments

Comments
 (0)