-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.java
121 lines (101 loc) · 3.75 KB
/
FileManager.java
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.docs.utils;
import io.opentelemetry.instrumentation.docs.InstrumentationType;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileManager {
private static final Logger logger = Logger.getLogger(FileManager.class.getName());
private final String rootDir;
public FileManager(String rootDir) {
this.rootDir = rootDir;
}
public List<InstrumentationPath> getInstrumentationPaths() {
Path rootPath = Paths.get(rootDir);
try (Stream<Path> walk = Files.walk(rootPath)) {
return walk.filter(Files::isDirectory)
.filter(dir -> !dir.toString().contains("/build"))
.filter(dir -> isValidInstrumentationPath(dir.toString()))
.map(dir -> parseInstrumentationPath(dir.toString()))
.collect(Collectors.toList());
} catch (IOException e) {
logger.severe("Error traversing directory: " + e.getMessage());
return new ArrayList<>();
}
}
private static InstrumentationPath parseInstrumentationPath(String filePath) {
if (filePath == null || filePath.isEmpty()) {
return null;
}
String instrumentationSegment = "/instrumentation/";
int startIndex = filePath.indexOf(instrumentationSegment) + instrumentationSegment.length();
String[] parts = filePath.substring(startIndex).split("/");
if (parts.length < 2) {
return null;
}
InstrumentationType instrumentationType =
InstrumentationType.fromString(parts[parts.length - 1]);
String name = parts[parts.length - 2];
String namespace = name.contains("-") ? name.split("-")[0] : name;
return new InstrumentationPath(name, filePath, namespace, namespace, instrumentationType);
}
public static boolean isValidInstrumentationPath(String filePath) {
if (filePath == null || filePath.isEmpty()) {
return false;
}
String instrumentationSegment = "instrumentation/";
if (!filePath.contains(instrumentationSegment)) {
return false;
}
int javaagentCount = filePath.split("/javaagent", -1).length - 1;
if (javaagentCount > 1) {
return false;
}
if (filePath.contains("/test/")
|| filePath.contains("/testing")
|| filePath.contains("-common/")
|| filePath.contains("bootstrap/src")) {
return false;
}
return filePath.endsWith("javaagent") || filePath.endsWith("library");
}
public List<String> findBuildGradleFiles(String instrumentationDirectory) {
Path rootPath = Paths.get(instrumentationDirectory);
try (Stream<Path> walk = Files.walk(rootPath)) {
return walk.filter(Files::isRegularFile)
.filter(
path ->
path.getFileName().toString().equals("build.gradle.kts")
&& !path.toString().contains("/testing/"))
.map(Path::toString)
.collect(Collectors.toList());
} catch (IOException e) {
logger.severe("Error traversing directory: " + e.getMessage());
return new ArrayList<>();
}
}
public String getMetaDataFile(String instrumentationDirectory) {
String metadataFile = instrumentationDirectory + "/metadata.yaml";
if (Files.exists(Paths.get(metadataFile))) {
return readFileToString(metadataFile);
}
return null;
}
public String readFileToString(String filePath) {
try {
return Files.readString(Paths.get(filePath));
} catch (IOException e) {
logger.severe("Error reading file: " + e.getMessage());
return null;
}
}
}