Skip to content

Commit

Permalink
Tweak AddDependency to be a bit more permissive when adding a depende…
Browse files Browse the repository at this point in the history
…ncy without onlyIfUsing constraint.
  • Loading branch information
sambsnyd committed Jan 16, 2025
1 parent 3d9653d commit a6afce0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.util.*;

import static java.util.Collections.*;
import static java.util.Objects.requireNonNull;

@Value
Expand Down Expand Up @@ -171,14 +172,14 @@ private boolean usesType(SourceFile sourceFile, ExecutionContext ctx) {
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");
}));
sourceFile.getMarkers().findFirst(JavaProject.class).ifPresent(javaProject -> {
if (usesType(sourceFile, ctx)) {
acc.usingType = true;
}
Set<String> configurations = acc.configurationsByProject.computeIfAbsent(javaProject, ignored -> new HashSet<>());
sourceFile.getMarkers().findFirst(JavaSourceSet.class).ifPresent(sourceSet ->
configurations.add("main".equals(sourceSet.getName()) ? "implementation" : sourceSet.getName() + "Implementation"));
});
return tree;
}
};
Expand Down Expand Up @@ -216,7 +217,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {

GradleProject gp = maybeGp.get();

Set<String> resolvedConfigurations = StringUtils.isBlank(configuration) ? acc.configurationsByProject.get(jp) : new HashSet<>(Collections.singletonList(configuration));
Set<String> resolvedConfigurations = StringUtils.isBlank(configuration) ?
acc.configurationsByProject.getOrDefault(jp, new HashSet<>()) :
new HashSet<>(singletonList(configuration));
if (resolvedConfigurations.isEmpty()) {
resolvedConfigurations.add("implementation");
}
Set<String> tmpConfigurations = new HashSet<>(resolvedConfigurations);
for (String tmpConfiguration : tmpConfigurations) {
GradleDependencyConfiguration gdc = gp.getConfiguration(tmpConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,6 @@ void addUnconditionally() {
rewriteRun(
spec -> spec.recipe(addDependency("org.apache.logging.log4j:log4j-core:2.22.1")),
mavenProject("project",
srcMainJava(
java(usingGuavaIntMath)
),
buildGradle("""
plugins {
id "java-library"
Expand Down

0 comments on commit a6afce0

Please sign in to comment.