Skip to content

Commit

Permalink
Fix Gradle AddDependency not doing anything when no onlyIfUsing param…
Browse files Browse the repository at this point in the history
…eter is specified
  • Loading branch information
sambsnyd committed Nov 15, 2024
1 parent 5ad8a05 commit 58f15f7
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public class AddDependency extends ScanningRecipe<AddDependency.Scanned> {
@Nullable
Boolean acceptTransitive;

static final String DEPENDENCY_PRESENT = "org.openrewrite.gradle.AddDependency.DEPENDENCY_PRESENT";

@Override
public String getDisplayName() {
return "Add Gradle dependency";
Expand Down Expand Up @@ -155,7 +153,8 @@ public Scanned getInitialValue(ExecutionContext ctx) {
public TreeVisitor<?, ExecutionContext> getScanner(Scanned acc) {
return new TreeVisitor<Tree, ExecutionContext>() {

UsesType<ExecutionContext> usesType;
@Nullable
UsesType<ExecutionContext> usesType = null;
private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) {
if(onlyIfUsing == null) {
return true;
Expand All @@ -168,14 +167,17 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) {

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
if (!(tree instanceof SourceFile)) {
return tree;
}
SourceFile sourceFile = (SourceFile) tree;
sourceFile.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject ->
sourceFile.getMarkers().findFirst(JavaSourceSet.class).ifPresent(sourceSet -> {
if (usesType(sourceFile, ctx)) {
acc.usingType = true;
Set<String> configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>());
configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation");
}
Set<String> configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>());
configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation");
}));
return tree;
}
Expand All @@ -184,7 +186,7 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
return Preconditions.check(acc.usingType && !acc.configurationsByProject.isEmpty(),
return Preconditions.check((onlyIfUsing == null || acc.usingType) && !acc.configurationsByProject.isEmpty(),
Preconditions.check(new IsBuildGradle<>(), new GroovyIsoVisitor<ExecutionContext>() {

@Override
Expand Down Expand Up @@ -225,7 +227,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {

tmpConfigurations = new HashSet<>(resolvedConfigurations);
for (String tmpConfiguration : tmpConfigurations) {
GradleDependencyConfiguration gdc = gp.getConfiguration(tmpConfiguration);
GradleDependencyConfiguration gdc = requireNonNull((gp.getConfiguration(tmpConfiguration)));
for (GradleDependencyConfiguration transitive : gp.configurationsExtendingFrom(gdc, true)) {
if (resolvedConfigurations.contains(transitive.getName()) ||
(Boolean.TRUE.equals(acceptTransitive) && transitive.findResolvedDependency(groupId, artifactId) != null)) {
Expand All @@ -248,7 +250,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
}

private boolean isTopLevel(Cursor cursor) {
return cursor.getParent().firstEnclosing(J.MethodInvocation.class) == null;
return cursor.getParentOrThrow().firstEnclosing(J.MethodInvocation.class) == null;
}
})
);
Expand Down
Loading

0 comments on commit 58f15f7

Please sign in to comment.