-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemDependencyGraphBuilder.java
More file actions
94 lines (76 loc) · 3.46 KB
/
FileSystemDependencyGraphBuilder.java
File metadata and controls
94 lines (76 loc) · 3.46 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
package blue.contract.packager.graphbuilder;
import blue.contract.packager.BluePackageExporter;
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 java.util.HashSet;
import java.util.Set;
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;
private final Set<String> ignorePatterns;
public FileSystemDependencyGraphBuilder(Path rootPath, Set<String> ignorePatterns) {
this.rootPath = rootPath;
this.ignorePatterns = ignorePatterns;
}
public FileSystemDependencyGraphBuilder(Path rootPath) {
this(rootPath, new HashSet<>());
}
@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) && !shouldIgnore(path)) {
String dirName = path.getFileName().toString();
// Check if this directory contains any .blue files
if (containsBlueFiles(path)) {
Path extendsFile = path.resolve(EXTENDS_FILE_NAME);
String dependency = Files.exists(extendsFile) ?
readDependency(extendsFile) :
BluePackageExporter.ROOT_DEPENDENCY;
graph.addDirectory(dirName, dependency);
processFiles(path, dirName, graph);
}
}
}
}
return graph;
}
private boolean shouldIgnore(Path path) {
String pathName = path.getFileName().toString();
return ignorePatterns.stream().anyMatch(pattern ->
pathName.matches(pattern.replace("*", ".*").replace("?", ".?")));
}
private boolean containsBlueFiles(Path directory) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, "*.blue")) {
return stream.iterator().hasNext();
}
}
private String 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);
}
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);
}
}
}
}