Skip to content

Commit

Permalink
Support non-main source sets (#8)
Browse files Browse the repository at this point in the history
* Support non-main source sets (eg `test`)
* Provide a `compileAllErrorProne` task that depends on all the compile tasks with that have errorprone applied and enabled.
  • Loading branch information
CRogers authored Nov 5, 2024
1 parent e5e2591 commit a04a5b6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 89 deletions.
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-8.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: fix
fix:
description: |-
* Support non-main source sets (eg `test`)
* Provide a `compileAllErrorProne` task that depends on all the compile tasks that have errorprone applied and enabled.
links:
- https://github.com/palantir/suppressible-error-prone/pull/8
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.ltgt.gradle.errorprone.ErrorProneOptions;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.compile.JavaCompile;
Expand All @@ -49,9 +48,9 @@ public final Set<String> patchChecksForCompilation(JavaCompile javaCompile) {
}

public final void configureEachErrorProneOptions(Action<ErrorProneOptions> action) {
project.getTasks().withType(JavaCompile.class).configureEach(javaCompile -> ((ExtensionAware)
javaCompile.getOptions())
.getExtensions()
.configure(ErrorProneOptions.class, action));
project.getTasks()
.withType(JavaCompile.class)
.configureEach(
javaCompile -> SuppressibleErrorPronePlugin.configureErrorProneOptions(javaCompile, action));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import net.ltgt.gradle.errorprone.CheckSeverity;
import net.ltgt.gradle.errorprone.ErrorProneOptions;
import net.ltgt.gradle.errorprone.ErrorPronePlugin;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.tasks.SourceSetContainer;
Expand Down Expand Up @@ -84,11 +86,9 @@ private void applyToJavaProject(Project project) {
project.getTasks().withType(JavaCompile.class).configureEach(javaCompile -> {
configureJavaCompile(project, javaCompile);

((ExtensionAware) javaCompile.getOptions())
.getExtensions()
.configure(ErrorProneOptions.class, errorProneOptions -> {
configureErrorProneOptions(project, extension, javaCompile, errorProneOptions);
});
configureErrorProneOptions(javaCompile, errorProneOptions -> {
setupErrorProneOptions(project, extension, javaCompile, errorProneOptions);
});
});

addAnnotationDependencyForSuppressingStage2(project, version);
Expand All @@ -110,6 +110,13 @@ private void applyToJavaProject(Project project) {
});
});
}

project.getTasks().register("compileAllErrorProne", Task.class, compileAll -> {
compileAll.dependsOn(project.provider(
() -> project.getTasks().withType(JavaCompile.class).matching(javaCompile -> {
return errorProneOptionsFor(javaCompile).getEnabled().get();
})));
});
}

private static void setupErrorProneArtifactTransform(Project project) {
Expand All @@ -123,22 +130,27 @@ private static void setupErrorProneArtifactTransform(Project project) {
.getAttributes()
.attribute(suppressible, false);

// It's the annotationProcessor configuration, not the errorprone that, is actually used by the compiler
// and so where we must put our transform. annotationProcessor extendsFrom errorprone.
project.getConfigurations().named("annotationProcessor").configure(errorProneConfiguration -> {
errorProneConfiguration
.getDependencies()
.add(project.getDependencies().create("com.google.errorprone:error_prone_check_api"));
errorProneConfiguration.getAttributes().attribute(suppressible, true);
});

project.getDependencies().registerTransform(ModifyErrorProneCheckApi.class, spec -> {
spec.getParameters().getSuppressionStage1().set(isSuppressingStageOne(project));

Attribute<String> artifactType = Attribute.of("artifactType", String.class);
spec.getFrom().attribute(suppressible, false).attribute(artifactType, "jar");
spec.getTo().attribute(suppressible, true).attribute(artifactType, "jar");
});

// We need to configure the transform in each source set as they each have their own compile task
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> {
// It's the annotationProcessor configuration, not the errorprone that, is actually used by the compiler
// and so where we must put our transform. annotationProcessor extendsFrom errorprone.
project.getConfigurations()
.named(sourceSet.getAnnotationProcessorConfigurationName())
.configure(errorProneConfiguration -> {
errorProneConfiguration
.getDependencies()
.add(project.getDependencies().create("com.google.errorprone:error_prone_check_api"));
errorProneConfiguration.getAttributes().attribute(suppressible, true);
});
});
}

private void configureJavaCompile(Project project, JavaCompile javaCompile) {
Expand All @@ -148,7 +160,7 @@ private void configureJavaCompile(Project project, JavaCompile javaCompile) {
}
}

private void configureErrorProneOptions(
private void setupErrorProneOptions(
Project project,
SuppressibleErrorProneExtension extension,
JavaCompile javaCompile,
Expand Down Expand Up @@ -241,6 +253,14 @@ private static void addAnnotationDependencyForSuppressingStage2(Project project,
}
}

private static ErrorProneOptions errorProneOptionsFor(JavaCompile javaCompile) {
return ((ExtensionAware) javaCompile.getOptions()).getExtensions().getByType(ErrorProneOptions.class);
}

static void configureErrorProneOptions(JavaCompile javaCompile, Action<ErrorProneOptions> action) {
((ExtensionAware) javaCompile.getOptions()).getExtensions().configure(ErrorProneOptions.class, action);
}

private static boolean isAnyKindOfPatching(Project project) {
return isApplyingSuggestedPatches(project) || isSuppressingStageOne(project) || isSuppressingStageTwo(project);
}
Expand Down
Loading

0 comments on commit a04a5b6

Please sign in to comment.