This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
375 lines (307 loc) · 11.9 KB
/
main.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream>
#include <string>
#include <vector>
#include "json.hpp"
#include <experimental/filesystem>
#include <fstream>
using namespace std;
using json = nlohmann::json;
namespace fs = std::experimental::filesystem;
enum class DependencyOrder {
After,
Before
};
class ModuleDependency {
public:
string id;
string version;
bool optional;
DependencyOrder order;
};
class Module {
public:
string id;
string file;
string version;
std::vector<ModuleDependency> dependencies;
};
class ModuleNode {
public:
string id;
Module *module;
vector<ModuleNode *> dependencies;
};
Module *parseModule(fs::path path) {
// Get file name
string fileName;
fileName = path.stem().string();
// Parse json
ifstream fileStream(path);
json jsonFile;
fileStream >> jsonFile;
// Validate definition version
if (jsonFile["definitionVersion"] != 1) {
std::cout << fileName << " is in an incompatible format/version" << std::endl;
exit(2);
}
// Create module
Module *module = new Module();
module->id = jsonFile["id"];
module->version = jsonFile["version"];
module->file = fileName;
// Parse dependencies
if (jsonFile.find("dependencies") != jsonFile.end()) {
json dependenciesJson = jsonFile["dependencies"];
// Loop through items
for (json::iterator it = dependenciesJson.begin(); it != dependenciesJson.end(); ++it) {
json entry = it.value();
// Create dependency
ModuleDependency dependency;
dependency.id = it.key();
dependency.version = entry["version"];
dependency.order = DependencyOrder::After;
// Parse order
if (entry.find("order") != entry.end()) {
if (entry["order"] == "before") {
dependency.order = DependencyOrder::Before;
}
}
// Parse optional
if (entry.find("optional") != entry.end()) {
dependency.optional = entry["optional"];
}
module->dependencies.push_back(dependency);
}
}
return module;
}
vector<string> split(string str, char separator) {
vector<string> strings;
istringstream stream(str);
string s;
while (getline(stream, s, separator)) {
strings.push_back(s);
}
return strings;
}
bool areCompatible(string versionId, string version, string targetId, string target) {
// Parse actual version
vector<string> versionParts = split(version, '-');
vector<string> versionSegments = split(versionParts[0], '.');
string versionRelease = versionParts.size() > 1 ? versionParts[1] : "final";
// Parse target
vector<string> targetOptions = split(target, '|');
for (vector<string>::iterator it = targetOptions.begin(); it != targetOptions.end(); ++it) {
string targetOption = *it;
// Parse target option
vector<string> targetParts = split(targetOption, '-');
vector<string> targetSegments = split(targetParts[0], '.');
string targetRelease = targetParts.size() > 1 ? targetParts[1] : "final";
int segmentCount = min(versionSegments.size(), targetSegments.size());
for (int i = 0; i < segmentCount; i++) {
string versionSegment = versionSegments[i];
string targetSegment = targetSegments[i];
// Ensure not empty
if (versionSegment.size() < 1) {
cout << "Invalid version " << version << " (from " << versionId << ")" << endl;
exit(4);
}
// Ensure not empty
if (targetSegment.size() < 1) {
cout << "Invalid version target " << target << " (from " << targetId << " for " << versionId << ")"
<< endl;
exit(4);
}
int value = stoi(versionSegment);
// Check version target type
if (targetSegment[0] == '[') {
// Ensure format
if (targetSegment[targetSegment.size() - 1] != ']' || targetSegment.size() < 3) {
cout << "Invalid version target " << target << " (from " << targetId << " for " << versionId << ")"
<< endl;
exit(4);
}
// Parse
vector<string> rangeSegments = split(targetSegment.substr(1, targetSegment.size() - 2), ',');
// Ensure two values
if (rangeSegments.size() != 2) {
cout << "Invalid version target " << target << " (from " << targetId << " for " << versionId << ")"
<< endl;
exit(4);
}
// Parse min max
int min = stoi(rangeSegments[0]);
int max = stoi(rangeSegments[1]);
// Ensure within min max
if (!(value >= min && value <= max)) {
goto outerEnd;
}
} else if (targetSegment[targetSegment.size() - 1] == '+') {
// Validate format
if (targetSegment.size() < 2) {
cout << "Invalid version target " << target << " (from " << targetId << " for " << versionId << ")"
<< endl;
exit(4);
}
// Parse
int min = stoi(targetSegment.substr(0, targetSegment.size() - 1));
// Ensure within bound
if (value < min) {
goto outerEnd;
}
} else if (targetSegment[targetSegment.size() - 1] == '-') {
// Validate format
if (targetSegment.size() < 2) {
cout << "Invalid version target " << target << " (from " << targetId << " for " << versionId << ")"
<< endl;
exit(4);
}
// Parse
int max = stoi(targetSegment.substr(0, targetSegment.size() - 1));
// Ensure within bound
if (value > max) {
goto outerEnd;
}
} else {
// Parse
int targetValue = stoi(targetSegment);
// Ensure equals target
if (value != targetValue) {
goto outerEnd;
}
}
}
// If target requests a more fine version, then assume incorrect
if (targetSegments.size() > versionSegments.size()) {
goto outerEnd;
}
// If target release is later, then incorrect
if (targetRelease.compare(versionRelease) > 1) {
goto outerEnd;
}
// If others pass, then must be compatible
return true;
outerEnd:;
}
return false;
}
void resolve(ModuleNode *node, vector<ModuleNode *> *resolved, vector<ModuleNode *> *unresolved) {
// Mark node as unresolved
unresolved->push_back(node);
// Resolve dependencies
for (vector<ModuleNode *>::iterator it = node->dependencies.begin(); it != node->dependencies.end(); ++it) {
ModuleNode *dependency = *it;
// If node hasn't been resolved, resolve it
if (find(resolved->begin(), resolved->end(), dependency) == resolved->end()) {
// Check for circular dependencies
if (find(unresolved->begin(), unresolved->end(), dependency) != unresolved->end()) {
cout << "Circular dependency detected! " << node->id << " to " << dependency->id << endl;
exit(6);
}
// Resolve dependency
resolve(dependency, resolved, unresolved);
}
}
// Mark as resolved
resolved->push_back(node);
unresolved->erase(remove(unresolved->begin(), unresolved->end(), node), unresolved->end());
}
int main(int argc, char *argv[]) {
// Parse arguments
if (argc != 2) {
cout << "Invalid argument count" << endl;
exit(1);
}
string rootPath = argv[1];
// Parse all module json files
vector<Module *> modules;
for (const auto &entry : fs::directory_iterator(rootPath)) {
fs::path path = entry.path();
// Validate file
if (!fs::is_directory(path) && path.has_extension() && path.extension() == ".json") {
// Parse
Module *module = parseModule(path);
modules.push_back(module);
}
}
// Generate nodes for all modules
map<string, ModuleNode *> nodes;
for (vector<Module *>::iterator it = modules.begin(); it != modules.end(); ++it) {
Module *module = *it;
// Create node
ModuleNode *node = new ModuleNode();
node->id = module->id;
node->module = module;
// Store
nodes.insert(make_pair(node->id, node));
}
// Generate dependency links
for (map<string, ModuleNode *>::iterator it = nodes.begin(); it != nodes.end(); ++it) {
ModuleNode *node = it->second;
Module *module = node->module;
vector<ModuleDependency> *dependencies = &module->dependencies;
for (vector<ModuleDependency>::iterator it2 = dependencies->begin(); it2 != dependencies->end(); ++it2) {
ModuleDependency dependency = *it2;
// Check whether dependency exists
if (nodes.find(dependency.id) == nodes.end()) {
if (dependency.optional) {
continue;
}
cout << "Required dependency " << dependency.id << " is missing for " << module->id << endl;
exit(3);
}
// Fetch other node
ModuleNode *otherNode = nodes[dependency.id];
Module *otherModule = otherNode->module;
// Check compatible
if (!areCompatible(module->id, otherModule->version, dependency.id, dependency.version)) {
cout << "Required dependency " << dependency.id << "(" << dependency.version << ") for " << module->id
<< " is not compatible with " << otherModule->id << "(" << otherModule->version << ")" << endl;
exit(5);
}
// Add dependency
switch (dependency.order) {
case DependencyOrder::After:
node->dependencies.push_back(otherNode);
break;
case DependencyOrder::Before:
otherNode->dependencies.push_back(node);
break;
}
}
}
// Find unrooted nodes
vector<ModuleNode *> unrooted;
for (map<string, ModuleNode *>::iterator it = nodes.begin(); it != nodes.end(); ++it) {
ModuleNode *node = it->second;
unrooted.push_back(node);
}
for (map<string, ModuleNode *>::iterator it = nodes.begin(); it != nodes.end(); ++it) {
ModuleNode *node = it->second;
vector<ModuleNode *> dependencies;
for (vector<ModuleNode *>::iterator it2 = dependencies.begin(); it2 != dependencies.end(); ++it2) {
ModuleNode *dependency = *it2;
unrooted.erase(remove(unrooted.begin(), unrooted.end(), dependency), unrooted.end());
}
}
// Create fake node
ModuleNode *root = new ModuleNode();
root->id = "root";
for (vector<ModuleNode *>::iterator it = unrooted.begin(); it != unrooted.end(); ++it) {
ModuleNode *node = *it;
root->dependencies.push_back(node);
}
// Resolve
vector<ModuleNode *> resolved;
vector<ModuleNode *> unresolved;
resolve(root, &resolved, &unresolved);
// Remove fake node
resolved.erase(remove(resolved.begin(), resolved.end(), root), resolved.end());
// Print
for (vector<ModuleNode *>::iterator it = resolved.begin(); it != resolved.end(); ++it) {
ModuleNode *node = *it;
Module *module = node->module;
cout << module->id << "#" << module->file << endl;
}
return 0;
}