Skip to content

Commit

Permalink
Enhance FindPlugins to locate plugins which are represented in Gradle…
Browse files Browse the repository at this point in the history
…Project metadata.

This should work with kotlin scripts, or with plugins applied transitively by other plugins.
  • Loading branch information
sambsnyd committed Aug 8, 2024
1 parent e9cb8c7 commit f86bfd2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.openrewrite.*;
import org.openrewrite.gradle.IsBuildGradle;
import org.openrewrite.gradle.IsSettingsGradle;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.tree.GradlePlugin;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -55,18 +57,43 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
MethodMatcher pluginMatcher = new MethodMatcher("PluginSpec id(..)", false);
return Preconditions.check(Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()), new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (pluginMatcher.matches(method)) {
if (method.getArguments().get(0) instanceof J.Literal &&
pluginId.equals(((J.Literal) method.getArguments().get(0)).getValue())) {
return SearchResult.found(method);
TreeVisitor<?, ExecutionContext> jv = Preconditions.check(
Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (pluginMatcher.matches(method)) {
if (method.getArguments().get(0) instanceof J.Literal &&
pluginId.equals(((J.Literal) method.getArguments().get(0)).getValue())) {
return SearchResult.found(method);
}
}
return super.visitMethodInvocation(method, ctx);
}
});

return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (!(tree instanceof SourceFile)) {
return tree;
}
SourceFile s = (SourceFile) tree;

if (jv.isAcceptable(s, ctx)) {
s = (SourceFile) jv.visitNonNull(s, ctx);
}

// Even if we couldn't find a declaration the metadata might show the plugin is in use
GradleProject gp = s.getMarkers().findFirst(GradleProject.class).orElse(null);
if (s == tree && gp != null && gp.getPlugins().stream()
.anyMatch(it -> pluginId.equals(it.getId()))) {
s = SearchResult.found(s);
}
return super.visitMethodInvocation(method, ctx);

return s;
}
});
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,70 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Tree;
import org.openrewrite.gradle.marker.GradlePluginDescriptor;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.test.SourceSpecs.text;

class FindPluginsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FindPlugins("com.jfrog.bintray"));
spec.recipe(new FindPlugins("org.openrewrite.rewrite"));
}

@DocumentExample
@Test
void findPlugin() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi()),
buildGradle(
"""
plugins {
id 'com.jfrog.bintray'
id 'com.jfrog.bintray' version '1.8.5'
id 'org.openrewrite.rewrite' version '6.18.0'
}
""",
"""
plugins {
/*~~>*/id 'com.jfrog.bintray'
/*~~>*/id 'com.jfrog.bintray' version '1.8.5'
/*~~>*/id 'org.openrewrite.rewrite' version '6.18.0'
}
""",
spec -> spec.beforeRecipe(cu -> assertThat(FindPlugins.find(cu, "com.jfrog.bintray"))
spec -> spec
.beforeRecipe(cu -> assertThat(FindPlugins.find(cu, "org.openrewrite.rewrite"))
.isNotEmpty()
.anySatisfy(p -> {
assertThat(p.getPluginId()).isEqualTo("com.jfrog.bintray");
assertThat(p.getVersion()).isEqualTo("1.8.5");
assertThat(p.getPluginId()).isEqualTo("org.openrewrite.rewrite");
assertThat(p.getVersion()).isEqualTo("6.18.0");
}))
)
);
}

@Test
void findPluginFromGradleProjectMarker() {
rewriteRun(
text(
"stand-in for a kotlin gradle script",
"~~>stand-in for a kotlin gradle script",
spec -> spec.markers(new GradleProject(
Tree.randomId(),
"group",
"name",
"version",
"path",
Collections.singletonList(new GradlePluginDescriptor("org.openrewrite.gradle.GradlePlugin", "org.openrewrite.rewrite")),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyMap())
)
)
);
}
}

0 comments on commit f86bfd2

Please sign in to comment.