Skip to content

Commit

Permalink
Add checkstyle flag to allow ignoring generated sources (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jskov authored Jan 21, 2024
1 parent 7827a50 commit 0c4c1cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ By default uses [this configuration](./src/main/resources/config/checkstyle/chec

* `dk.mada.style.checkstyle.enabled = true`
Boolean flag allowing the checkstyle checker to be disabled
* `dk.mada.style.checkstyle.ignore-generated-source = false`
Boolean flag to control scanning of test source files
* `dk.mada.style.checkstyle.ignore-test-source = false`
Boolean flag to control scanning of test source
Boolean flag to control scanning of generated source files
* `dk.mada.style.checkstyle.config-path = null`
Optional path to an alternative checkstyle configuration file

Expand All @@ -59,11 +61,11 @@ By default uses [this configuration](./src/main/resources/config/checkstyle/chec

* `dk.mada.style.errorprone.enabled = true`
Boolean flag allowing the error prone checker to be disabled
* `dk.mada.style.errorprone.ignore-test-source = false`
Boolean flag to control scanning of test source
* `dk.mada.style.errorprone.ignore-generated-source = false`
Boolean flag to control scanning of generated source
Boolean flag to control scanning of generated source files
Note: works poorly with Immutable generated sources (as they cannot be referenced from the main sources when enabled)
* `dk.mada.style.errorprone.ignore-test-source = false`
Boolean flag to control scanning of test source files
* `dk.mada.style.errorprone.excluded-paths-regexp = `
Optional regular expression used to exclude files from scanning

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'com.gradle.plugin-publish' version '1.2.1'
id 'eclipse'
id 'dk.mada.style' version '0.9.1'
id 'dk.mada.style' version '0.9.2'
}

ext {
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/dk/mada/style/config/PluginConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public class PluginConfiguration {
/**
* Checkstyle configuration.
*
* @param enabled flag to activate checkstyle
* @param ignoreFailures flag to ignore failures
* @param ignoreTestSource flag to ignore test sources
* @param toolVersion an optional checkstyle version to use
* @param configPath an optional path to a checkstyle configuration file
* @param enabled flag to activate checkstyle
* @param ignoreFailures flag to ignore failures
* @param ignoreTestSource flag to ignore test source files
* @param ignoreGeneratedSource flag to ignore generated source files
* @param toolVersion an optional checkstyle version to use
* @param configPath an optional path to a checkstyle configuration file
*/
public record CheckstyleConfiguration(boolean enabled, boolean ignoreFailures, boolean ignoreTestSource, @Nullable String toolVersion,
public record CheckstyleConfiguration(boolean enabled, boolean ignoreFailures, boolean ignoreTestSource, boolean ignoreGeneratedSource,
@Nullable String toolVersion,
@Nullable String configPath) {
}

Expand Down Expand Up @@ -61,8 +63,8 @@ public record NullcheckerConfiguration(boolean enabled, String includePackages,
* ErrorProne configuration.
*
* @param enabled flag to activate error prone
* @param ignoreTestSource flag to ignore test sources
* @param ignoreGeneratedSource flag to ignore generated sources
* @param ignoreTestSource flag to ignore test source files
* @param ignoreGeneratedSource flag to ignore generated source files
* @param excludePathsRegexp a regular expression of source paths to ignore
* @param disabledRules a comma-separated list of rule names to disable
*/
Expand All @@ -82,6 +84,7 @@ public PluginConfiguration(Project project) {
getBoolProperty("checkstyle.enabled", true),
getBoolProperty("checkstyle.ignore-failures", true),
getBoolProperty("checkstyle.ignore-test-source", false),
getBoolProperty("checkstyle.ignore-generated-source", false),
getNullableProperty("checkstyle.config-path", null),
getNullableProperty("checkstyle.tool-version", null));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.nio.file.Paths;

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.quality.Checkstyle;
import org.gradle.api.plugins.quality.CheckstyleExtension;

import dk.mada.style.config.ConfigFileExtractor;
Expand Down Expand Up @@ -55,8 +57,20 @@ public void configure(CheckstyleExtension ce) {
ce.setToolVersion(toolVersion);
}
if (checkstyleConfig.ignoreTestSource()) {
project.getTasks().named("checkstyleTest", t -> t.setOnlyIf("disabled by mada style", ta -> false));
project.getTasks().named("checkstyleTest", this::disableTask);
}

if (checkstyleConfig.ignoreGeneratedSource()) {
project.getTasks().withType(Checkstyle.class, t -> {
if (t.getName().endsWith("Apt")) {
disableTask(t);
}
});
}
}

private void disableTask(Task t) {
t.setOnlyIf("disabled by mada style", ta -> false);
}

private Path getActiveConfigfile() {
Expand Down

0 comments on commit 0c4c1cd

Please sign in to comment.