-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig_migration.cppm
More file actions
83 lines (64 loc) · 2.51 KB
/
config_migration.cppm
File metadata and controls
83 lines (64 loc) · 2.51 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
// mcpp.fallback.config_migration — legacy index name migration.
//
// Older mcpp sandboxes used "mcpp-index" as the default index name.
// These helpers rename it to "mcpplibs" in config.toml and .xlings.json
// so xlings config/list output matches mcpp's default namespace.
export module mcpp.fallback.config_migration;
import std;
export namespace mcpp::fallback {
// Migrate config.toml: rename "mcpp-index" to "mcpplibs".
// Returns true if the file was modified.
bool migrate_config_toml_index_names(const std::filesystem::path& path);
// Migrate .xlings.json: rename "mcpp-index" to "mcpplibs".
// Returns true if the file was modified.
bool migrate_xlings_json_index_names(const std::filesystem::path& path);
} // namespace mcpp::fallback
namespace mcpp::fallback {
namespace {
bool replace_all(std::string& text, std::string_view from, std::string_view to) {
bool changed = false;
for (std::size_t pos = 0;
(pos = text.find(from, pos)) != std::string::npos;) {
text.replace(pos, from.size(), to);
pos += to.size();
changed = true;
}
return changed;
}
void write_file(const std::filesystem::path& p, std::string_view content) {
std::error_code ec;
std::filesystem::create_directories(p.parent_path(), ec);
std::ofstream os(p);
os << content;
}
bool write_text_if_changed(const std::filesystem::path& path,
const std::string& original,
const std::string& updated) {
if (updated == original) return false;
write_file(path, updated);
return true;
}
} // namespace
bool migrate_config_toml_index_names(const std::filesystem::path& path) {
std::ifstream is(path);
if (!is) return false;
std::stringstream ss;
ss << is.rdbuf();
auto original = ss.str();
auto updated = original;
replace_all(updated, "default = \"mcpp-index\"", "default = \"mcpplibs\"");
replace_all(updated, "[index.repos.\"mcpp-index\"]", "[index.repos.\"mcpplibs\"]");
return write_text_if_changed(path, original, updated);
}
bool migrate_xlings_json_index_names(const std::filesystem::path& path) {
std::ifstream is(path);
if (!is) return false;
std::stringstream ss;
ss << is.rdbuf();
auto original = ss.str();
auto updated = original;
replace_all(updated, "\"name\": \"mcpp-index\"", "\"name\": \"mcpplibs\"");
replace_all(updated, "\"name\":\"mcpp-index\"", "\"name\":\"mcpplibs\"");
return write_text_if_changed(path, original, updated);
}
} // namespace mcpp::fallback