|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.docs; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +class GradleParser { |
| 16 | + private static final Pattern passBlockPattern = |
| 17 | + Pattern.compile("pass\\s*\\{(.*?)}", Pattern.DOTALL); |
| 18 | + |
| 19 | + private static final Pattern libraryPattern = |
| 20 | + Pattern.compile("library\\(\"([^\"]+:[^\"]+:[^\"]+)\"\\)"); |
| 21 | + |
| 22 | + private static final Pattern variablePattern = |
| 23 | + Pattern.compile("val\\s+(\\w+)\\s*=\\s*\"([^\"]+)\""); |
| 24 | + |
| 25 | + private static final Pattern compileOnlyPattern = |
| 26 | + Pattern.compile( |
| 27 | + "compileOnly\\(\"([^\"]+)\"\\)\\s*\\{\\s*version\\s*\\{(?:\\s*//.*\\n)*\\s*strictly\\(\"([^\"]+)\"\\)\\s*}\\s*}"); |
| 28 | + |
| 29 | + /** |
| 30 | + * Parses gradle files for muzzle and dependency information |
| 31 | + * |
| 32 | + * @param gradleFileContents Contents of a Gradle build file as a String |
| 33 | + * @return A set of strings summarizing the group, module, and version ranges |
| 34 | + */ |
| 35 | + public static Set<String> parseGradleFile(String gradleFileContents, InstrumentationType type) { |
| 36 | + Set<String> results = new HashSet<>(); |
| 37 | + Map<String, String> variables = extractVariables(gradleFileContents); |
| 38 | + |
| 39 | + if (type.equals(InstrumentationType.JAVAAGENT)) { |
| 40 | + results.addAll(parseMuzzle(gradleFileContents, variables)); |
| 41 | + } else { |
| 42 | + results.addAll(parseLibraryDependencies(gradleFileContents, variables)); |
| 43 | + } |
| 44 | + |
| 45 | + return results; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Parses the "muzzle" block from the given Gradle file content and extracts information about |
| 50 | + * each "pass { ... }" entry, returning a set of version summary strings. |
| 51 | + * |
| 52 | + * @param gradleFileContents Contents of a Gradle build file as a String |
| 53 | + * @param variables Map of variable names to their values |
| 54 | + * @return A set of strings summarizing the group, module, and version ranges |
| 55 | + */ |
| 56 | + private static Set<String> parseMuzzle(String gradleFileContents, Map<String, String> variables) { |
| 57 | + Set<String> results = new HashSet<>(); |
| 58 | + Matcher passBlockMatcher = passBlockPattern.matcher(gradleFileContents); |
| 59 | + |
| 60 | + while (passBlockMatcher.find()) { |
| 61 | + String passBlock = passBlockMatcher.group(1); |
| 62 | + |
| 63 | + String group = extractValue(passBlock, "group\\.set\\(\"([^\"]+)\"\\)"); |
| 64 | + String module = extractValue(passBlock, "module\\.set\\(\"([^\"]+)\"\\)"); |
| 65 | + String versionRange = extractValue(passBlock, "versions\\.set\\(\"([^\"]+)\"\\)"); |
| 66 | + |
| 67 | + if (group != null && module != null && versionRange != null) { |
| 68 | + String summary = group + ":" + module + ":" + interpolate(versionRange, variables); |
| 69 | + results.add(summary); |
| 70 | + } |
| 71 | + } |
| 72 | + return results; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parses the "dependencies" block from the given Gradle file content and extracts information |
| 77 | + * about what library versions are supported. |
| 78 | + * |
| 79 | + * @param gradleFileContents Contents of a Gradle build file as a String |
| 80 | + * @param variables Map of variable names to their values |
| 81 | + * @return A set of strings summarizing the group, module, and versions |
| 82 | + */ |
| 83 | + private static Set<String> parseLibraryDependencies( |
| 84 | + String gradleFileContents, Map<String, String> variables) { |
| 85 | + Set<String> results = new HashSet<>(); |
| 86 | + Matcher libraryMatcher = libraryPattern.matcher(gradleFileContents); |
| 87 | + while (libraryMatcher.find()) { |
| 88 | + String dependency = libraryMatcher.group(1); |
| 89 | + results.add(interpolate(dependency, variables)); |
| 90 | + } |
| 91 | + |
| 92 | + Matcher compileOnlyMatcher = compileOnlyPattern.matcher(gradleFileContents); |
| 93 | + while (compileOnlyMatcher.find()) { |
| 94 | + String dependency = compileOnlyMatcher.group(1) + ":" + compileOnlyMatcher.group(2); |
| 95 | + results.add(interpolate(dependency, variables)); |
| 96 | + } |
| 97 | + return results; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Extracts variables from the given Gradle file content. |
| 102 | + * |
| 103 | + * @param gradleFileContents Contents of a Gradle build file as a String |
| 104 | + * @return A map of variable names to their values |
| 105 | + */ |
| 106 | + private static Map<String, String> extractVariables(String gradleFileContents) { |
| 107 | + Map<String, String> variables = new HashMap<>(); |
| 108 | + Matcher variableMatcher = variablePattern.matcher(gradleFileContents); |
| 109 | + |
| 110 | + while (variableMatcher.find()) { |
| 111 | + variables.put(variableMatcher.group(1), variableMatcher.group(2)); |
| 112 | + } |
| 113 | + |
| 114 | + return variables; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Interpolates variables in the given text using the provided variable map. |
| 119 | + * |
| 120 | + * @param text Text to interpolate |
| 121 | + * @param variables Map of variable names to their values |
| 122 | + * @return Interpolated text |
| 123 | + */ |
| 124 | + private static String interpolate(String text, Map<String, String> variables) { |
| 125 | + for (Map.Entry<String, String> entry : variables.entrySet()) { |
| 126 | + text = text.replace("$" + entry.getKey(), entry.getValue()); |
| 127 | + } |
| 128 | + return text; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Utility method to extract the first captured group from matching the given regex. |
| 133 | + * |
| 134 | + * @param text Text to search |
| 135 | + * @param regex Regex with a capturing group |
| 136 | + * @return The first captured group, or null if not found |
| 137 | + */ |
| 138 | + private static String extractValue(String text, String regex) { |
| 139 | + Pattern pattern = Pattern.compile(regex); |
| 140 | + Matcher matcher = pattern.matcher(text); |
| 141 | + if (matcher.find()) { |
| 142 | + return matcher.group(1); |
| 143 | + } |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + private GradleParser() {} |
| 148 | +} |
0 commit comments