Skip to content

Commit

Permalink
fix #165: validate when plugin is configured
Browse files Browse the repository at this point in the history
current way create tesk instances even though it is not necessary.
this change applies the same way with the official CodeNarc plugin
validating configuration in setter method:
https://github.com/gradle/gradle/blob/df7ac484a2facbb4503ba911666367de22b1d16a/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/CodeNarcExtension.java
  • Loading branch information
KengoTODA committed Oct 15, 2019
1 parent 24e592a commit a727088
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Changelog

This is the changelog for SpotBugs Gradle Plugin. This follows [Keep a Changelog v1.0.0](http://keepachangelog.com/en/1.0.0/).
Expand All @@ -6,6 +7,10 @@ Currently the versioning policy of this project follows [Semantic Versioning](ht

## Unreleased - 2019-??-??

### Fixed

* Avoid needless task creation (#165)

## 2.0.0 - 2019-05-13

### Removed
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/com/github/spotbugs/SpotBugsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Properties;
import java.util.concurrent.Callable;

import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.file.FileCollection;
Expand Down Expand Up @@ -61,37 +60,6 @@ protected Class<SpotBugsTask> getTaskType() {
protected void beforeApply() {
verifyGradleVersion(GradleVersion.current());
configureSpotBugsConfigurations();
project.afterEvaluate(this::verify);
}

private void verify(Project p) {
p.getTasks().withType(SpotBugsTask.class).forEach(task -> {
SpotBugsReports reports = task.getReports();
if (reports.getText() != null && reports.getText().getDestination() == null) {
String message = String.format(
"Task '%s' has no destination for TEXT report. Set reports.text.destination to this task.",
task.getName());
throw new IllegalStateException(message);
}
if (reports.getXml() != null && reports.getXml().getDestination() == null) {
String message = String.format(
"Task '%s' has no destination for XML report. Set reports.xml.destination to this task.",
task.getName());
throw new IllegalStateException(message);
}
if (reports.getHtml() != null && reports.getHtml().getDestination() == null) {
String message = String.format(
"Task '%s' has no destination for HTML report. Set reports.html.destination. to this task",
task.getName());
throw new IllegalStateException(message);
}
if (reports.getEmacs() != null && reports.getEmacs().getDestination() == null) {
String message = String.format(
"Task '%s' has no destination for EMACS report. Set reports.emacs.destination. to this task",
task.getName());
throw new IllegalStateException(message);
}
});
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/github/spotbugs/SpotBugsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

import javax.inject.Inject;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Incubating;
import org.gradle.api.JavaVersion;
import org.gradle.api.*;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.logging.LogLevel;
Expand Down Expand Up @@ -186,9 +183,25 @@ public SpotBugsReports reports(Closure closure) {
@Override
public SpotBugsReports reports(Action<? super SpotBugsReports> configureAction) {
configureAction.execute(reports);
verify(reports);
return reports;
}

private void verify(SpotBugsReportsInternal reports) {
if (reports.getText() != null && reports.getText().getDestination() == null) {
throw new InvalidUserDataException("No destination for TEXT report. Set reports.text.destination to this task.");
}
if (reports.getXml() != null && reports.getXml().getDestination() == null) {
throw new InvalidUserDataException("No destination for XML report. Set reports.xml.destination to this task.");
}
if (reports.getHtml() != null && reports.getHtml().getDestination() == null) {
throw new InvalidUserDataException("No destination for HTML report. Set reports.html.destination to this task.");
}
if (reports.getEmacs() != null && reports.getEmacs().getDestination() == null) {
throw new InvalidUserDataException("No destination for EMACS report. Set reports.emacs.destination to this task.");
}
}

/**
* The filename of a filter specifying which bugs are reported.
*
Expand Down

0 comments on commit a727088

Please sign in to comment.