-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathManifest.cpp
More file actions
225 lines (182 loc) · 4.92 KB
/
Copy pathManifest.cpp
File metadata and controls
225 lines (182 loc) · 4.92 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
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
/**
*
* @file Manifest.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/util/Manifest.hpp>
#include <vix/cli/util/Fs.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#include <stdexcept>
namespace fs = std::filesystem;
using json = nlohmann::json;
namespace vix::cli::util::manifest
{
namespace
{
json read_json_or_throw(const fs::path &path)
{
std::ifstream in(path);
if (!in)
{
throw std::runtime_error("cannot open file: " + path.string());
}
json j;
in >> j;
return j;
}
void write_json_or_throw(const fs::path &path, const json &j)
{
const std::string content = j.dump(2) + "\n";
if (vix::cli::util::file_exists(path) &&
vix::cli::util::read_text_file_or_empty(path) == content)
{
return;
}
if (!vix::cli::util::write_text_file_atomic(path, content))
{
throw std::runtime_error("cannot write file: " + path.string());
}
}
Dependency parse_dependency_or_throw(const json &item)
{
if (!item.is_object())
{
throw std::runtime_error("invalid vix.json: dependency entry must be an object");
}
const std::string id = item.value("id", "");
const std::string requested = item.value("version", "");
if (id.empty())
{
throw std::runtime_error("invalid vix.json: dependency id cannot be empty");
}
if (requested.empty())
{
throw std::runtime_error("invalid vix.json: dependency version cannot be empty");
}
return Dependency{id, requested};
}
json dependency_to_json(const Dependency &dependency)
{
return json{
{"id", dependency.id},
{"version", dependency.requested},
};
}
}
Manifest read_manifest_or_throw(const fs::path &manifestPath)
{
Manifest manifest;
if (!fs::exists(manifestPath))
{
return manifest;
}
const json root = read_json_or_throw(manifestPath);
if (!root.is_object())
{
throw std::runtime_error("invalid vix.json: root must be an object");
}
if (!root.contains("deps"))
{
return manifest;
}
if (!root["deps"].is_array())
{
throw std::runtime_error("invalid vix.json: deps must be an array");
}
for (const auto &item : root["deps"])
{
manifest.dependencies.push_back(parse_dependency_or_throw(item));
}
return manifest;
}
std::vector<Dependency> read_manifest_dependencies_or_throw(
const fs::path &manifestPath)
{
return read_manifest_or_throw(manifestPath).dependencies;
}
void write_manifest_or_throw(
const fs::path &manifestPath,
const Manifest &manifest)
{
json root = json::object();
if (fs::exists(manifestPath))
{
root = read_json_or_throw(manifestPath);
if (!root.is_object())
{
throw std::runtime_error("invalid vix.json: root must be an object");
}
}
root["deps"] = json::array();
for (const auto &dependency : manifest.dependencies)
{
if (dependency.id.empty())
{
throw std::runtime_error("manifest dependency id cannot be empty");
}
if (dependency.requested.empty())
{
throw std::runtime_error("manifest dependency requested version cannot be empty");
}
root["deps"].push_back(dependency_to_json(dependency));
}
write_json_or_throw(manifestPath, root);
}
void upsert_manifest_dependency_or_throw(
const fs::path &manifestPath,
const Dependency &dependency)
{
if (dependency.id.empty())
{
throw std::runtime_error("manifest dependency id cannot be empty");
}
if (dependency.requested.empty())
{
throw std::runtime_error("manifest dependency requested version cannot be empty");
}
json root = json::object();
if (fs::exists(manifestPath))
{
root = read_json_or_throw(manifestPath);
if (!root.is_object())
{
throw std::runtime_error("invalid vix.json: root must be an object");
}
}
if (!root.contains("deps"))
{
root["deps"] = json::array();
}
if (!root["deps"].is_array())
{
throw std::runtime_error("invalid vix.json: deps must be an array");
}
bool updated = false;
for (auto &item : root["deps"])
{
if (!item.is_object())
{
throw std::runtime_error("invalid vix.json: dependency entry must be an object");
}
if (item.value("id", "") == dependency.id)
{
item["version"] = dependency.requested;
updated = true;
break;
}
}
if (!updated)
{
root["deps"].push_back(dependency_to_json(dependency));
}
write_json_or_throw(manifestPath, root);
}
}