|
| 1 | +package io.jenkins.tools.pluginmodernizer.core.extractor; |
| 2 | + |
| 3 | +import io.jenkins.tools.pluginmodernizer.core.model.JDK; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import org.openrewrite.groovy.GroovyIsoVisitor; |
| 9 | +import org.openrewrite.groovy.tree.G; |
| 10 | +import org.openrewrite.java.tree.Expression; |
| 11 | +import org.openrewrite.java.tree.J; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * Visitor for a Jenkinsfile |
| 17 | + */ |
| 18 | +public class JenkinsfileVisitor extends GroovyIsoVisitor<PluginMetadata> { |
| 19 | + |
| 20 | + /** |
| 21 | + * LOGGER. |
| 22 | + */ |
| 23 | + public static final Logger LOG = LoggerFactory.getLogger(JenkinsfileVisitor.class); |
| 24 | + |
| 25 | + private final Map<String, Object> variableMap = new HashMap<>(); |
| 26 | + |
| 27 | + @Override |
| 28 | + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v, PluginMetadata pluginMetadata) { |
| 29 | + LOG.debug("Visiting variable declarations {}", v); |
| 30 | + J.VariableDeclarations variableDeclarations = super.visitVariableDeclarations(v, pluginMetadata); |
| 31 | + |
| 32 | + for (J.VariableDeclarations.NamedVariable variable : variableDeclarations.getVariables()) { |
| 33 | + variableMap.put(variable.getSimpleName(), variable.getInitializer()); |
| 34 | + } |
| 35 | + |
| 36 | + return variableDeclarations; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, PluginMetadata pluginMetadata) { |
| 41 | + LOG.debug("Visiting method invocation {}", method); |
| 42 | + method = super.visitMethodInvocation(method, pluginMetadata); |
| 43 | + if ("buildPlugin".equals(method.getSimpleName())) { |
| 44 | + List<Expression> args = method.getArguments(); |
| 45 | + |
| 46 | + List<Integer> jdkVersions = |
| 47 | + args.stream().flatMap(this::extractJdkVersions).distinct().toList(); |
| 48 | + |
| 49 | + LOG.info("JDK versions found: {}", jdkVersions); |
| 50 | + |
| 51 | + jdkVersions.forEach(jdkVersion -> pluginMetadata.addJdk(JDK.get(jdkVersion))); |
| 52 | + } |
| 53 | + |
| 54 | + return method; |
| 55 | + } |
| 56 | + |
| 57 | + private Stream<Integer> extractJdkVersions(Expression arg) { |
| 58 | + if (arg instanceof G.MapEntry) { |
| 59 | + return Stream.of(arg) |
| 60 | + .map(G.MapEntry.class::cast) |
| 61 | + .filter(entry -> "configurations".equals(((J.Literal) entry.getKey()).getValue())) |
| 62 | + .map(entry -> resolveConfigurations(entry.getValue())) |
| 63 | + .filter(value -> value instanceof G.ListLiteral) |
| 64 | + .flatMap(value -> ((G.ListLiteral) value).getElements().stream()) |
| 65 | + .filter(expression -> expression instanceof G.MapLiteral) |
| 66 | + .flatMap(expression -> ((G.MapLiteral) expression).getElements().stream()) |
| 67 | + .filter(mapExpr -> mapExpr instanceof G.MapEntry) |
| 68 | + .map(G.MapEntry.class::cast) |
| 69 | + .filter(mapEntry -> "jdk".equals(((J.Literal) mapEntry.getKey()).getValue())) |
| 70 | + .map(mapEntry -> Integer.parseInt( |
| 71 | + ((J.Literal) mapEntry.getValue()).getValue().toString())); |
| 72 | + } else { |
| 73 | + Expression resolvedArg = resolveVariable(arg); |
| 74 | + return Stream.of(resolvedArg) |
| 75 | + .filter(resolved -> resolved instanceof G.MapLiteral) |
| 76 | + .flatMap(resolved -> ((G.MapLiteral) resolved).getElements().stream()) |
| 77 | + .filter(entry -> entry instanceof G.MapEntry) |
| 78 | + .map(G.MapEntry.class::cast) |
| 79 | + .filter(entry -> "configurations".equals(((J.Literal) entry.getKey()).getValue())) |
| 80 | + .map(entry -> resolveConfigurations(entry.getValue())) |
| 81 | + .filter(value -> value instanceof G.ListLiteral) |
| 82 | + .flatMap(value -> ((G.ListLiteral) value).getElements().stream()) |
| 83 | + .filter(expression -> expression instanceof G.MapLiteral) |
| 84 | + .flatMap(expression -> ((G.MapLiteral) expression).getElements().stream()) |
| 85 | + .filter(mapExpr -> mapExpr instanceof G.MapEntry) |
| 86 | + .map(G.MapEntry.class::cast) |
| 87 | + .filter(mapEntry -> "jdk".equals(((J.Literal) mapEntry.getKey()).getValue())) |
| 88 | + .map(mapEntry -> Integer.parseInt( |
| 89 | + ((J.Literal) mapEntry.getValue()).getValue().toString())); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private Expression resolveVariable(Expression expression) { |
| 94 | + if (expression instanceof J.Identifier) { |
| 95 | + String variableName = ((J.Identifier) expression).getSimpleName(); |
| 96 | + if (variableMap.containsKey(variableName)) { |
| 97 | + return (Expression) variableMap.get(variableName); |
| 98 | + } |
| 99 | + } |
| 100 | + return expression; |
| 101 | + } |
| 102 | + |
| 103 | + private Expression resolveConfigurations(Expression entry) { |
| 104 | + return entry instanceof G.ListLiteral ? entry : resolveVariable(entry); |
| 105 | + } |
| 106 | +} |
0 commit comments