Skip to content

Commit

Permalink
fix: SpotBugsTask is not configuration cache safe if a stylesheet is …
Browse files Browse the repository at this point in the history
…set as String for the HTML report

Signed-off-by: Kengo TODA <[email protected]>
  • Loading branch information
KengoTODA committed Aug 15, 2023
1 parent a1e53ec commit be95ef5
Showing 1 changed file with 15 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package com.github.spotbugs.snom.internal;

import com.github.spotbugs.snom.SpotBugsPlugin;
import com.github.spotbugs.snom.SpotBugsReport;
import com.github.spotbugs.snom.SpotBugsTask;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -21,64 +22,48 @@
import java.util.Optional;
import javax.inject.Inject;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.resources.TextResource;
import org.gradle.api.resources.TextResourceFactory;

public abstract class SpotBugsHtmlReport extends SpotBugsReport {
private final Property<TextResource> stylesheet;
private final Property<String> stylesheetPath;

@Inject
public SpotBugsHtmlReport(ObjectFactory objects, SpotBugsTask task) {
super(objects, task);
// the default reportsDir is "$buildDir/reports/spotbugs/${baseName}.html"
getOutputLocation().convention(task.getReportsDir().file(task.getBaseName() + ".html"));
stylesheet = objects.property(TextResource.class);
stylesheetPath = objects.property(String.class);
}

@NonNull
@Override
public String toCommandLineOption() {
@Nullable TextResource stylesheet = getStylesheet();

if (stylesheet == null) {
return "-html";
} else {
return "-html:" + stylesheet.asFile().getAbsolutePath();
}
return stylesheet
.map(textResource -> "-html:" + textResource.asFile().getAbsolutePath())
.getOrElse("-html");
}

@Override
public TextResource getStylesheet() {
if (stylesheet.isPresent()) {
return stylesheet.get();
} else if (stylesheetPath.isPresent()) {
return resolve(stylesheetPath.get());
}

return null;
return stylesheet.getOrNull();
}

private TextResource resolve(String path) {
private TextResource resolve(
String path, Configuration configuration, TextResourceFactory factory) {
Optional<File> spotbugsJar =
getTask()
.getProject()
.getConfigurations()
.getByName("spotbugs")
configuration
.files(
dependency ->
dependency.getGroup().equals("com.github.spotbugs")
&& dependency.getName().equals("spotbugs"))
.stream()
.findFirst();
if (spotbugsJar.isPresent()) {
return getTask()
.getProject()
.getResources()
.getText()
.fromArchiveEntry(spotbugsJar.get(), path);
return factory.fromArchiveEntry(spotbugsJar.get(), path);
} else {
throw new InvalidUserDataException(
"The dependency on SpotBugs not found in 'spotbugs' configuration");
Expand All @@ -92,6 +77,9 @@ public void setStylesheet(@Nullable TextResource textResource) {

@Override
public void setStylesheet(@Nullable String path) {
stylesheetPath.set(path);
Configuration configuration =
getTask().getProject().getConfigurations().getByName(SpotBugsPlugin.CONFIG_NAME);
TextResourceFactory factory = getTask().getProject().getResources().getText();
stylesheet.set(getTask().getProject().provider(() -> resolve(path, configuration, factory)));
}
}

0 comments on commit be95ef5

Please sign in to comment.