-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemDependencyGraphBuilder.java
More file actions
79 lines (67 loc) · 3.11 KB
/
FileSystemDependencyGraphBuilder.java
File metadata and controls
79 lines (67 loc) · 3.11 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
package blue.contract.packager.graphbuilder;
import blue.contract.packager.model.DependencyGraph;
import blue.contract.packager.model.TypeDependency;
import blue.language.model.Node;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import static blue.contract.packager.BluePackageExporter.EXTENDS_FILE_NAME;
import static blue.language.utils.UncheckedObjectMapper.YAML_MAPPER;
public class FileSystemDependencyGraphBuilder implements DependencyGraphBuilder {
private final Path rootPath;
public FileSystemDependencyGraphBuilder(Path rootPath) {
this.rootPath = rootPath;
}
@Override
public DependencyGraph buildDependencyGraph(String rootDir) throws IOException {
DependencyGraph graph = new DependencyGraph();
Path fullPath = rootPath.resolve(rootDir);
if (!Files.exists(fullPath)) {
throw new IOException("Root directory not found: " + fullPath);
}
try (DirectoryStream<Path> typeStream = Files.newDirectoryStream(fullPath)) {
for (Path typePath : typeStream) {
if (Files.isDirectory(typePath)) {
String typeName = typePath.getFileName().toString();
processTypeDirectory(typePath, typeName, graph);
}
}
}
return graph;
}
private void processTypeDirectory(Path typePath, String typeName, DependencyGraph graph) throws IOException {
try (DirectoryStream<Path> versionStream = Files.newDirectoryStream(typePath)) {
for (Path versionPath : versionStream) {
if (Files.isDirectory(versionPath)) {
String version = versionPath.getFileName().toString();
TypeDependency dependency = readDependency(versionPath.resolve(EXTENDS_FILE_NAME));
graph.addDirectory(typeName, version, dependency);
processFiles(versionPath, typeName, version, graph);
}
}
}
}
private TypeDependency readDependency(Path path) throws IOException {
if (!Files.exists(path)) {
throw new IOException("Dependency file not found: " + path);
}
String content = Files.readString(path).trim();
if (content.isEmpty() || content.lines().count() != 1) {
throw new IOException("Invalid dependency file format. Expected one line in: " + path);
}
try {
return TypeDependency.parse(content);
} catch (IllegalArgumentException e) {
throw new IOException("Invalid dependency format in file: " + path + ". " + e.getMessage());
}
}
private void processFiles(Path versionPath, String typeName, String version, DependencyGraph graph) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(versionPath, "*.blue")) {
for (Path file : stream) {
Node contract = YAML_MAPPER.readValue(Files.readString(file), Node.class);
graph.addNode(typeName, version, contract);
}
}
}
}