-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasspathDependencyGraphBuilder.java
More file actions
104 lines (87 loc) · 4.08 KB
/
ClasspathDependencyGraphBuilder.java
File metadata and controls
104 lines (87 loc) · 4.08 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
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.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import static blue.contract.packager.BluePackageExporter.BLUE_FILE_EXTENSION;
import static blue.contract.packager.BluePackageExporter.EXTENDS_FILE_NAME;
import static blue.language.utils.UncheckedObjectMapper.YAML_MAPPER;
public class ClasspathDependencyGraphBuilder implements DependencyGraphBuilder {
private final ClassLoader classLoader;
public ClasspathDependencyGraphBuilder(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public DependencyGraph buildDependencyGraph(String rootDir) throws IOException {
DependencyGraph graph = new DependencyGraph();
URL resource = classLoader.getResource(rootDir);
if (resource == null) {
throw new IOException("Root directory not found in classpath: " + rootDir);
}
try (InputStream is = resource.openStream()) {
Scanner scanner = new Scanner(is).useDelimiter("\\A");
String content = scanner.hasNext() ? scanner.next() : "";
String[] directories = content.split("\n");
for (String dir : directories) {
String typePath = rootDir + "/" + dir;
String[] versions = getVersions(typePath);
for (String version : versions) {
TypeDependency dependency = readDependency(typePath + "/" + version + "/" + EXTENDS_FILE_NAME);
graph.addDirectory(dir, version, dependency);
processFiles(rootDir, dir, version, graph);
}
}
}
return graph;
}
private String[] getVersions(String typePath) throws IOException {
URL typeUrl = classLoader.getResource(typePath);
if (typeUrl == null) {
throw new IOException("Type directory not found: " + typePath);
}
try (InputStream is = typeUrl.openStream()) {
Scanner scanner = new Scanner(is).useDelimiter("\\A");
String content = scanner.hasNext() ? scanner.next() : "";
return content.split("\n");
}
}
private TypeDependency readDependency(String path) throws IOException {
try (InputStream is = classLoader.getResourceAsStream(path)) {
if (is == null) {
throw new IOException("Dependency file not found: " + path);
}
Scanner scanner = new Scanner(is).useDelimiter("\\A");
String content = scanner.hasNext() ? scanner.next().trim() : "";
if (content.isEmpty() || content.lines().count() != 1) {
throw new IOException("Invalid dependency file format. Expected one line in: " + path);
}
return TypeDependency.parse(content);
}
}
private void processFiles(String rootDir, String dirName, String version, DependencyGraph graph) throws IOException {
String dirPath = rootDir + "/" + dirName + "/" + version + "/";
URL dirUrl = classLoader.getResource(dirPath);
if (dirUrl == null) {
return;
}
try (InputStream is = dirUrl.openStream()) {
Scanner scanner = new Scanner(is).useDelimiter("\\A");
String content = scanner.hasNext() ? scanner.next() : "";
String[] files = content.split("\n");
for (String file : files) {
if (!file.equals(EXTENDS_FILE_NAME) && file.endsWith(BLUE_FILE_EXTENSION)) {
String filePath = dirPath + file;
try (InputStream fileIs = classLoader.getResourceAsStream(filePath)) {
if (fileIs != null) {
Node contract = YAML_MAPPER.readValue(fileIs, Node.class);
graph.addNode(dirName, version, contract);
}
}
}
}
}
}
}