From 4382b83b8c1be5f817da5337b940731bae803b0d Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 26 Nov 2023 16:41:33 -0800 Subject: [PATCH] Upgrade to Deteket 1.23.4 --- .idea/misc.xml | 1 - .../bld/java/com/example/ExampleBuild.java | 24 +++- .../bld/extension/DetektOperationBuild.java | 7 +- .../rife/bld/extension/DetektOperation.java | 128 +++++++++++++----- .../bld/extension/DetektOperationTest.java | 28 ++-- 5 files changed, 129 insertions(+), 59 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 03e18e6..6f1a8cf 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/examples/src/bld/java/com/example/ExampleBuild.java b/examples/src/bld/java/com/example/ExampleBuild.java index 514c6a0..e716605 100644 --- a/examples/src/bld/java/com/example/ExampleBuild.java +++ b/examples/src/bld/java/com/example/ExampleBuild.java @@ -29,8 +29,12 @@ public class ExampleBuild extends Project { autoDownloadPurge = true; repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES); + final var kotlin = version(1, 9, 21); + scope(compile) - .include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", version(1, 9, 21))); + .include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-stdlib-jdk7", kotlin)) + .include(dependency("org.jetbrains.kotlin", "kotlin-stdlib-jdk8", kotlin)); scope(test) .include(dependency("org.jetbrains.kotlin:kotlin-test-junit5:1.9.21")) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1))) @@ -64,6 +68,7 @@ public class ExampleBuild extends Project { @BuildCommand(summary = "Checks source with Detekt") public void detekt() throws ExitStatusException, IOException, InterruptedException { + // The source code located in the project's root will be checked new DetektOperation() .fromProject(this) .execute(); @@ -71,6 +76,7 @@ public class ExampleBuild extends Project { @BuildCommand(value = "detekt-baseline", summary = "Creates the Detekt baseline") public void detektBaseline() throws ExitStatusException, IOException, InterruptedException { + // The detekt-baseline.xml file will be created in the project's root new DetektOperation() .fromProject(this) .baseline("detekt-baseline.xml") @@ -80,15 +86,19 @@ public class ExampleBuild extends Project { @BuildCommand(value = "detekt-main", summary = "Checks main source with Detekt") public void detektMain() throws ExitStatusException, IOException, InterruptedException { - var op = new DetektOperation().fromProject(this); - op.input().clear(); - op.input("src/main/kotlin").execute(); + // The source code located in src/main/kotlin will be checked + new DetektOperation() + .fromProject(this) + .input("src/main/kotlin") + .execute(); } @BuildCommand(value = "detekt-test", summary = "Checks test source with Detekt") public void detektTest() throws ExitStatusException, IOException, InterruptedException { - var op = new DetektOperation().fromProject(this); - op.input().clear(); - op.input("src/test/kotlin").execute(); + // The source code located in src/test/kotlin will be checked + new DetektOperation() + .fromProject(this) + .input("src/test/kotlin") + .execute(); } } \ No newline at end of file diff --git a/src/bld/java/rife/bld/extension/DetektOperationBuild.java b/src/bld/java/rife/bld/extension/DetektOperationBuild.java index 2d1d4b7..9950bfa 100644 --- a/src/bld/java/rife/bld/extension/DetektOperationBuild.java +++ b/src/bld/java/rife/bld/extension/DetektOperationBuild.java @@ -41,15 +41,10 @@ public class DetektOperationBuild extends Project { autoDownloadPurge = true; repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES); - var detekt = version(1, 23, 3); scope(compile) .include(dependency("com.uwyn.rife2", "bld", version(1, 7, 5))) - .include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", detekt)) - .include(dependency("io.gitlab.arturbosch.detekt", "detekt-tooling", detekt)) - .include(dependency("com.beust", "jcommander", "1.82")) - .include(dependency("com.fasterxml:aalto-xml:1.3.2")); + .include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", version(1, 23, 4))); scope(test) - .include(dependency("com.puppycrawl.tools", "checkstyle", version(10, 12, 5))) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1))) .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 1))) .include(dependency("org.assertj", "assertj-core", version(3, 24, 2))); diff --git a/src/main/java/rife/bld/extension/DetektOperation.java b/src/main/java/rife/bld/extension/DetektOperation.java index 3bdc877..a9f24d4 100644 --- a/src/main/java/rife/bld/extension/DetektOperation.java +++ b/src/main/java/rife/bld/extension/DetektOperation.java @@ -20,7 +20,6 @@ import rife.bld.BaseProject; import rife.bld.operations.AbstractProcessOperation; import java.io.File; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -34,6 +33,46 @@ import java.util.logging.Logger; * @since 1.0 */ public class DetektOperation extends AbstractProcessOperation { + private static final List DETEKT_JARS = List.of( + "detekt-cli-", + "jcommander-", + "detekt-core-", + "detekt-rules-", + "detekt-rules-errorprone-", + "detekt-tooling-", + "detekt-parser-", + "detekt-report-md-", + "detekt-metrics-", + "detekt-api-", + "detekt-psi-utils-", + "kotlin-compiler-embeddable-", + "kotlin-reflect-", + "detekt-report-html-", + "detekt-report-txt-", + "detekt-report-xml-", + "detekt-report-sarif-", + "detekt-utils-", + "detekt-rules-complexity-", + "detekt-rules-coroutines-", + "detekt-rules-documentation-", + "detekt-rules-empty-", + "detekt-rules-exceptions-", + "detekt-rules-naming-", + "detekt-rules-performance-", + "detekt-rules-style-", + "sarif4k-jvm-", + "kotlinx-serialization-json-jvm-", + "kotlinx-serialization-core-jvm-", + "kotlin-stdlib-jdk8-", + "kotlin-stdlib-jdk7-", + "kotlin-stdlib-", + "contester-breakpoint-", + "kotlin-script-runtime-", + "kotlin-daemon-embeddable-", + "trove4j-", + "annotations-", + "snakeyaml-engine-", + "kotlinx-html-jvm-"); private static final Logger LOGGER = Logger.getLogger(DetektReport.class.getName()); private final Collection classpath_ = new ArrayList<>(); private final Collection config_ = new ArrayList<>(); @@ -71,10 +110,10 @@ public class DetektOperation extends AbstractProcessOperation { } /** - * Allow rules to auto correct code if they support it. The default rule - * sets do NOT support auto correcting and won't change any line in the - * users code base. However custom rules can be written to support auto - * correcting. The additional 'formatting' rule set, added with + * Allow rules to autocorrect code if they support it. The default rule + * sets do NOT support autocorrecting and won't change any line in the + * users code base. However, custom rules can be written to support + * autocorrecting. The additional 'formatting' rule set, added with * {@link #plugins(String...) Plugins}, does support it and needs this flag. * * @param autoCorrect {@code true} or {@code false} @@ -86,7 +125,7 @@ public class DetektOperation extends AbstractProcessOperation { } /** - * Specifies a directory as the base path. Currently it impacts all file + * Specifies a directory as the base path. Currently, it impacts all file * paths in the formatted reports. File paths in console output and txt * report are not affected and remain as absolute paths. * @@ -124,8 +163,8 @@ public class DetektOperation extends AbstractProcessOperation { } /** - * EXPERIMENTAL: Paths where to find user class files and depending jar - * files. Used for type resolution. + * EXPERIMENTAL: Paths where to find user class files and depending jar files. + * Used for type resolution. * * @param paths one or more files * @return this operation instance @@ -136,8 +175,8 @@ public class DetektOperation extends AbstractProcessOperation { } /** - * EXPERIMENTAL: Paths where to find user class files and depending jar - * files. Used for type resolution. + * EXPERIMENTAL: Paths where to find user class files and depending jar files. + * Used for type resolution. * * @param paths the list of files * @return this operation instance @@ -238,7 +277,7 @@ public class DetektOperation extends AbstractProcessOperation { final List args = new ArrayList<>(); args.add(javaTool()); args.add("-cp"); - args.add(Path.of(project_.libBldDirectory().getAbsolutePath(), "*").toString()); + args.add(getDetektJarList(project_.libBldDirectory())); args.add("io.gitlab.arturbosch.detekt.cli.Main"); // all-rules @@ -252,13 +291,13 @@ public class DetektOperation extends AbstractProcessOperation { } // base-path - if (basePath_ != null) { + if (isNotBlank(basePath_)) { args.add("--base-path"); args.add(basePath_); } // baseline - if (baseline_ != null) { + if (isNotBlank(baseline_)) { args.add("--baseline"); args.add(baseline_); } @@ -281,7 +320,7 @@ public class DetektOperation extends AbstractProcessOperation { } // config-resource - if (configResource_ != null) { + if (isNotBlank(configResource_)) { args.add("--config-resource"); args.add(configResource_); } @@ -302,9 +341,9 @@ public class DetektOperation extends AbstractProcessOperation { } // excludes - if (excludes_ != null) { + if (isNotBlank(excludes_)) { args.add("--excludes"); - + args.add(excludes_); } // generate-config @@ -313,7 +352,7 @@ public class DetektOperation extends AbstractProcessOperation { } // includes - if (includes_ != null) { + if (isNotBlank(includes_)) { args.add("--includes"); args.add(includes_); } @@ -325,19 +364,19 @@ public class DetektOperation extends AbstractProcessOperation { } // jdk-home - if (jdkHome_ != null) { + if (isNotBlank(jdkHome_)) { args.add("--jdk-home"); args.add(jdkHome_); } // jvm-target - if (jvmTarget_ != null) { + if (isNotBlank(jvmTarget_)) { args.add("--jvm-target"); args.add(jvmTarget_); } // language-version - if (languageVersion_ != null) { + if (isNotBlank(languageVersion_)) { args.add("--language-version"); args.add(languageVersion_); } @@ -377,8 +416,11 @@ public class DetektOperation extends AbstractProcessOperation { /** * Configures the operation from a {@link BaseProject}. *

- * Sets the {@link #input input} to {@code src/main/kotlin}, {@code src/test/kotlin} and {@code detekt-baseline.xml} - * if they exist. + * Sets the following: + *

    + *
  • {@link #baseline baseline} to {@code detekt-baseline.xml}, if it exists
  • + *
  • {@link #excludes excludes} to exclude {@code build} and {@code resources} directories
  • + *
* * @param project the project to configure the operation from * @return this operation instance @@ -386,18 +428,11 @@ public class DetektOperation extends AbstractProcessOperation { @Override public DetektOperation fromProject(BaseProject project) { project_ = project; - var main = new File(project.srcMainDirectory(), "kotlin"); - if (main.exists()) { - input_.add(main.getAbsolutePath()); - } - var test = new File(project.srcTestDirectory(), "kotlin"); - if (test.exists()) { - input_.add(test.getAbsolutePath()); - } var baseline = new File(project.workDirectory(), "detekt-baseline.xml"); if (baseline.exists()) { baseline_ = baseline.getAbsolutePath(); } + excludes(".*/build/.*,.*/resources/.*"); return this; } @@ -414,6 +449,30 @@ public class DetektOperation extends AbstractProcessOperation { return this; } + /* + * Retrieves the matching JARs files from the given directory. + */ + private String getDetektJarList(File directory) { + var jars = new ArrayList(); + + if (directory.isDirectory()) { + var files = directory.listFiles(); + if (files != null) { + for (var f : files) { + if (!f.getName().endsWith("-sources.jar") && !f.getName().endsWith("-javadoc.jar")) { + for (var m : DETEKT_JARS) { + if (f.getName().startsWith(m)) { + jars.add(f.getAbsolutePath()); + break; + } + } + } + } + } + } + return String.join(":", jars); + } + /** * Globbing patterns describing paths to include in the analysis. Useful in * combination with {@link #excludes(String) excludes} patterns. @@ -457,6 +516,13 @@ public class DetektOperation extends AbstractProcessOperation { return input_; } + /* + * Determines if a string is not blank. + */ + private boolean isNotBlank(String s) { + return s != null && !s.isBlank(); + } + /** * EXPERIMENTAL: Use a custom JDK home directory to include into the * classpath. @@ -545,7 +611,7 @@ public class DetektOperation extends AbstractProcessOperation { } /** - * Generates a report for given 'report-id' and stores it on given 'path'. + * Generates a report for given {@link DetektReportId report-id} and stores it on given 'path'. * * @param reports one or more reports * @return this operation instance diff --git a/src/test/java/rife/bld/extension/DetektOperationTest.java b/src/test/java/rife/bld/extension/DetektOperationTest.java index a66ec11..c678a17 100644 --- a/src/test/java/rife/bld/extension/DetektOperationTest.java +++ b/src/test/java/rife/bld/extension/DetektOperationTest.java @@ -29,7 +29,7 @@ import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; -import static org.assertj.core.api.Assertions.*; // NOPMD +import static org.assertj.core.api.Assertions.*; @SuppressWarnings("PMD.AvoidDuplicateLiterals") @@ -46,12 +46,19 @@ class DetektOperationTest { } @Test - void testExamplesExecute() { + void testExampleBaseline() throws IOException, ExitStatusException, InterruptedException { + var tmpDir = Files.createTempDirectory("bld-detekt-").toFile(); + tmpDir.deleteOnExit(); + + var baseline = new File(tmpDir, "detekt-baseline.xml"); + var op = new DetektOperation() .fromProject(new BaseProjectBlueprint(new File("examples"), "com.example", "Example")) - .debug(true); - assertThatThrownBy(op::execute).isInstanceOf(ExitStatusException.class); + .baseline(baseline.getAbsolutePath()) + .createBaseline(true); + op.execute(); + assertThat(baseline).exists(); } @Test @@ -89,18 +96,11 @@ class DetektOperationTest { } @Test - void testExampleBaseline() throws IOException, ExitStatusException, InterruptedException { - var tmpDir = Files.createTempDirectory("bld-detekt-").toFile(); - tmpDir.deleteOnExit(); - - var baseline = new File(tmpDir, "detekt-baseline.xml"); - + void testExamplesExecute() { var op = new DetektOperation() .fromProject(new BaseProjectBlueprint(new File("examples"), "com.example", "Example")) - .baseline(baseline.getAbsolutePath()) - .createBaseline(true); - op.execute(); - assertThat(baseline).exists(); + .debug(true); + assertThatThrownBy(op::execute).isInstanceOf(ExitStatusException.class); } } \ No newline at end of file