-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemDependencyGraphBuilder.java
More file actions
65 lines (53 loc) · 2.3 KB
/
FileSystemDependencyGraphBuilder.java
File metadata and controls
65 lines (53 loc) · 2.3 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
package blue.contract.packager.graphbuilder;
import blue.contract.packager.model.DependencyGraph;
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;
import static blue.contract.packager.BluePackageExporter.ROOT_DEPENDENCY;
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> stream = Files.newDirectoryStream(fullPath)) {
for (Path path : stream) {
if (Files.isDirectory(path)) {
String dirName = path.getFileName().toString();
String dependency = readDependency(path.resolve(EXTENDS_FILE_NAME));
graph.addDirectory(dirName, dependency);
processFiles(path, dirName, graph);
}
}
}
return graph;
}
private String readDependency(Path path) throws IOException {
if (!Files.exists(path)) {
return ROOT_DEPENDENCY;
}
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);
}
return content;
}
private void processFiles(Path dirPath, String dirName, DependencyGraph graph) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath, "*.blue")) {
for (Path file : stream) {
Node contract = YAML_MAPPER.readValue(Files.readString(file), Node.class);
graph.addNode(dirName, contract);
}
}
}
}