|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.docs; |
| 7 | + |
| 8 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +class FileSearch { |
| 23 | + |
| 24 | + private FileSearch() {} |
| 25 | + |
| 26 | + public static List<String> getJavaCodePaths(String rootDir) { |
| 27 | + Path rootPath = Paths.get(rootDir); |
| 28 | + |
| 29 | + try (Stream<Path> walk = Files.walk(rootPath)) { |
| 30 | + return walk.filter(Files::isRegularFile) |
| 31 | + .map(Path::toString) |
| 32 | + .filter(path -> !path.contains("/build/") && !path.contains("/test/")) |
| 33 | + .collect(Collectors.toList()); |
| 34 | + } catch (IOException e) { |
| 35 | + System.out.println("Error traversing directory: " + e.getMessage()); |
| 36 | + return List.of(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static Map<String, String> findStringInFiles( |
| 41 | + List<String> fileList, Map<String, String> searchStrings) { |
| 42 | + Map<String, String> matchingFiles = new HashMap<>(); |
| 43 | + for (String filePath : fileList) { |
| 44 | + if (filePath.endsWith(".java")) { |
| 45 | + try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), UTF_8)) { |
| 46 | + String line; |
| 47 | + while ((line = reader.readLine()) != null) { |
| 48 | + for (Map.Entry<String, String> entry : searchStrings.entrySet()) { |
| 49 | + if (line.contains(entry.getValue())) { |
| 50 | + matchingFiles.put(entry.getKey(), filePath); |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } catch (IOException e) { |
| 56 | + // File may have been removed or is inaccessible; ignore or log as needed |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return matchingFiles; |
| 61 | + } |
| 62 | + |
| 63 | + public static List<InstrumentationPath> getInstrumentationList(String rootDir) { |
| 64 | + Path rootPath = Paths.get(rootDir); |
| 65 | + |
| 66 | + try (Stream<Path> walk = Files.walk(rootPath)) { |
| 67 | + return walk.filter(Files::isDirectory) |
| 68 | + .filter(dir -> !dir.toString().contains("/build")) |
| 69 | + .filter(dir -> isValidInstrumentationPath(dir.toString())) |
| 70 | + .map(dir -> parseInstrumentationPath(dir.toString())) |
| 71 | + .collect(Collectors.toList()); |
| 72 | + } catch (IOException e) { |
| 73 | + System.out.println("Error traversing directory: " + e.getMessage()); |
| 74 | + return new ArrayList<>(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + static InstrumentationPath parseInstrumentationPath(String filePath) { |
| 79 | + if (filePath == null || filePath.isEmpty()) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + String instrumentationSegment = "/instrumentation/"; |
| 84 | + int startIndex = filePath.indexOf(instrumentationSegment) + instrumentationSegment.length(); |
| 85 | + String[] parts = filePath.substring(startIndex).split("/"); |
| 86 | + |
| 87 | + if (parts.length < 2) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + InstrumentationType instrumentationType = |
| 92 | + InstrumentationType.fromString(parts[parts.length - 1]); |
| 93 | + String name = parts[parts.length - 2]; |
| 94 | + String namespace = name.contains("-") ? name.split("-")[0] : name; |
| 95 | + |
| 96 | + return new InstrumentationPath(name, filePath, namespace, namespace, instrumentationType); |
| 97 | + } |
| 98 | + |
| 99 | + public static boolean isValidInstrumentationPath(String filePath) { |
| 100 | + if (filePath == null || filePath.isEmpty()) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + String instrumentationSegment = "instrumentation/"; |
| 104 | + |
| 105 | + if (!filePath.startsWith(instrumentationSegment)) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + int javaagentCount = filePath.split("/javaagent", -1).length - 1; |
| 110 | + if (javaagentCount > 1) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + if (filePath.contains("/test/") |
| 115 | + || filePath.contains("/testing") |
| 116 | + || filePath.contains("-common/") |
| 117 | + || filePath.contains("bootstrap/src")) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + return filePath.endsWith("javaagent") || filePath.endsWith("library"); |
| 122 | + } |
| 123 | + |
| 124 | + public static List<String> findBuildGradleFiles(String rootDir) { |
| 125 | + Path rootPath = Paths.get(rootDir); |
| 126 | + |
| 127 | + try (Stream<Path> walk = Files.walk(rootPath)) { |
| 128 | + return walk.filter(Files::isRegularFile) |
| 129 | + .filter( |
| 130 | + path -> |
| 131 | + path.getFileName().toString().equals("build.gradle.kts") |
| 132 | + && !path.toString().contains("/testing/")) |
| 133 | + .map(Path::toString) |
| 134 | + .collect(Collectors.toList()); |
| 135 | + } catch (IOException e) { |
| 136 | + System.out.println("Error traversing directory: " + e.getMessage()); |
| 137 | + return new ArrayList<>(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public static String readFileToString(String filePath) { |
| 142 | + try { |
| 143 | + return Files.readString(Paths.get(filePath)); |
| 144 | + } catch (IOException e) { |
| 145 | + System.out.println("Error reading file: " + e.getMessage()); |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments