|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.gradle.spring; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.openrewrite.*; |
| 21 | +import org.openrewrite.gradle.IsBuildGradle; |
| 22 | +import org.openrewrite.gradle.marker.GradleProject; |
| 23 | +import org.openrewrite.gradle.plugins.AddBuildPlugin; |
| 24 | +import org.openrewrite.internal.lang.Nullable; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.tree.J; |
| 27 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 28 | +import org.openrewrite.marker.SearchResult; |
| 29 | + |
| 30 | +import java.util.Optional; |
| 31 | + |
| 32 | +@Value |
| 33 | +@EqualsAndHashCode(callSuper = true) |
| 34 | +public class AddSpringDependencyManagementPlugin extends Recipe { |
| 35 | + @Override |
| 36 | + public String getDisplayName() { |
| 37 | + return "Add `io.spring.dependency-management` plugin, if in use"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getDescription() { |
| 42 | + return "Add `io.spring.dependency-management` plugin, if in use."; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 47 | + return Preconditions.check( |
| 48 | + Preconditions.and( |
| 49 | + new IsBuildGradle<>(), |
| 50 | + new UsesSpringDependencyManagement() |
| 51 | + ), |
| 52 | + new AddBuildPlugin("io.spring.dependency-management", "1.0.6.RELEASE", null).getVisitor() |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + private static class UsesSpringDependencyManagement extends JavaIsoVisitor<ExecutionContext> { |
| 57 | + @Override |
| 58 | + public J visit(@Nullable Tree tree, ExecutionContext ctx) { |
| 59 | + if (tree instanceof JavaSourceFile) { |
| 60 | + JavaSourceFile cu = (JavaSourceFile) tree; |
| 61 | + Optional<GradleProject> maybeGp = cu.getMarkers().findFirst(GradleProject.class); |
| 62 | + if (!maybeGp.isPresent()) { |
| 63 | + return cu; |
| 64 | + } |
| 65 | + GradleProject gp = maybeGp.get(); |
| 66 | + if (gp.getPlugins().stream().anyMatch(plugin -> "io.spring.dependency-management".equals(plugin.getId()) || |
| 67 | + "io.spring.gradle.dependencymanagement.DependencyManagementPlugin".equals(plugin.getFullyQualifiedClassName()))) { |
| 68 | + return SearchResult.found(cu); |
| 69 | + } |
| 70 | + } |
| 71 | + return super.visit(tree, ctx); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments