Compare commits

..

2 commits

Author SHA1 Message Date
a13a2b814b Added detekt-main & detekt-test examples 2023-11-25 16:26:43 -08:00
ba4741062c Added PMD extension 2023-11-25 15:00:41 -08:00
11 changed files with 176 additions and 35 deletions

3
.idea/misc.xml generated
View file

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<pattern value="rife.bld.extension.DetektOperationBuild" method="pmd" />
</component>
<component name="PDMPlugin"> <component name="PDMPlugin">
<option name="skipTestSources" value="false" /> <option name="skipTestSources" value="false" />
</component> </component>

View file

@ -13,7 +13,7 @@ To install, please refer to the [extensions documentation](https://github.com/ri
To check all Kotlin source code located `src/main/kotlin` and `src/test/kotlin`, add the following to your build file To check all Kotlin source code located `src/main/kotlin` and `src/test/kotlin`, add the following to your build file
```java ```java
@BuildCommand(summary = "Check source with Detekt") @BuildCommand(summary = "Checks source with Detekt")
public void detekt() throws ExitStatusException, IOException, InterruptedException { public void detekt() throws ExitStatusException, IOException, InterruptedException {
new DetektOperation() new DetektOperation()
.fromProject(this) .fromProject(this)
@ -25,10 +25,12 @@ public void detekt() throws ExitStatusException, IOException, InterruptedExcepti
./bld compile detekt ./bld compile detekt
``` ```
- [View Examples Project](https://github.com/rife2/bld-detekt/tree/main/examples)
To generate a Detekt baseline, add the following to your build file: To generate a Detekt baseline, add the following to your build file:
```java ```java
@BuildCommand(value = "detekt-baseline", summary = "Creates a Detekt baseline") @BuildCommand(value = "detekt-baseline", summary = "Creates the Detekt baseline")
public void detektBaseline() throws ExitStatusException, IOException, InterruptedException { public void detektBaseline() throws ExitStatusException, IOException, InterruptedException {
new DetektOperation() new DetektOperation()
.fromProject(this) .fromProject(this)
@ -41,5 +43,6 @@ public void detektBaseline() throws ExitStatusException, IOException, Interrupte
```console ```console
./bld compile detekt-baseline ./bld compile detekt-baseline
``` ```
- [View Examples Project](https://github.com/rife2/bld-detekt/tree/main/examples)
Please check the [DetektOperation documentation](https://rife2.github.io/bld-detekt/rife/bld/extension/DetektOperation.html#method-summary) for all available configuration options. Please check the [DetektOperation documentation](https://rife2.github.io/bld-detekt/rife/bld/extension/DetektOperation.html#method-summary) for all available configuration options.

110
config/pmd.xml Normal file
View file

@ -0,0 +1,110 @@
<?xml version="1.0"?>
<ruleset name="erik"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Erik's Ruleset</description>
<!-- BEST PRACTICES -->
<rule ref="category/java/bestpractices.xml">
<exclude name="AvoidPrintStackTrace"/>
<exclude name="JUnit4TestShouldUseTestAnnotation"/>
<exclude name="JUnitTestContainsTooManyAsserts"/>
<exclude name="GuardLogStatement"/>
</rule>
<rule ref="category/java/bestpractices.xml/MissingOverride">
<properties>
<property name="violationSuppressXPath"
value="//MethodDeclaration[@Name='hashCode' or @Name='equals' or @Name='toString']"/>
</properties>
</rule>
<!-- CODE STYLE -->
<rule ref="category/java/codestyle.xml">
<exclude name="AtLeastOneConstructor"/>
<exclude name="ClassNamingConventions"/>
<exclude name="ConfusingTernary"/>
<exclude name="CommentDefaultAccessModifier"/>
<exclude name="FieldNamingConventions"/>
<exclude name="LocalVariableCouldBeFinal"/>
<exclude name="LocalVariableNamingConventions"/>
<exclude name="LongVariable"/>
<exclude name="MethodArgumentCouldBeFinal"/>
<exclude name="OnlyOneReturn"/>
<exclude name="PackageCase"/>
<exclude name="ShortClassName"/>
<exclude name="ShortMethodName"/>
<exclude name="ShortVariable"/>
<exclude name="UselessParentheses"/>
<exclude name="UseUnderscoresInNumericLiterals"/>
</rule>
<rule ref="category/java/codestyle.xml/UnnecessaryImport">
<properties>
<property name="violationSuppressRegex" value="Unused (static|.*\.\*).*"/>
</properties>
</rule>
<!-- DESIGN -->
<rule ref="category/java/design.xml">
<exclude name="AvoidCatchingGenericException"/>
<exclude name="AvoidDeeplyNestedIfStmts"/>
<exclude name="AvoidUncheckedExceptionsInSignatures"/>
<exclude name="CognitiveComplexity"/>
<exclude name="CyclomaticComplexity"/>
<exclude name="ExcessiveClassLength"/>
<exclude name="ExcessiveMethodLength"/>
<exclude name="ExcessiveParameterList"/>
<exclude name="ExcessivePublicCount"/>
<exclude name="GodClass"/>
<exclude name="LawOfDemeter"/>
<exclude name="LoosePackageCoupling"/>
<exclude name="NPathComplexity"/>
<exclude name="NcssCount"/>
<exclude name="TooManyFields"/>
<exclude name="TooManyMethods"/>
<exclude name="UseObjectForClearerAPI"/>
</rule>
<!-- DOCUMENTATION -->
<rule ref="category/java/documentation.xml">
<exclude name="CommentRequired"/>
<exclude name="CommentSize"/>
</rule>
<!-- ERROR PRONE -->
<rule ref="category/java/errorprone.xml">
<exclude name="AssignmentInOperand"/>
<exclude name="AvoidCatchingNPE"/>
<exclude name="AvoidDuplicateLiterals"/>
<exclude name="AvoidFieldNameMatchingMethodName"/>
<exclude name="AvoidFieldNameMatchingTypeName"/>
<exclude name="AvoidLiteralsInIfCondition"/>
<exclude name="NullAssignment"/>
</rule>
<rule ref="category/java/errorprone.xml/AssignmentInOperand">
<properties>
<property name="allowWhile" value="true"/>
<property name="allowFor" value="true"/>
<property name="allowIf" value="true"/>
</properties>
</rule>
<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals">
<properties>
<property name="skipAnnotations" value="true"/>
</properties>
</rule>
<!-- MULTITHREADING -->
<rule ref="category/java/multithreading.xml">
</rule>
<!-- PERFORMANCE -->
<rule ref="category/java/performance.xml">
</rule>
<!-- SECURITY -->
<rule ref="category/java/security.xml">
</rule>
</ruleset>

View file

@ -6,6 +6,10 @@
<pattern value="com.example.ExampleBuild" method="dokkaHtml" /> <pattern value="com.example.ExampleBuild" method="dokkaHtml" />
<pattern value="com.example.ExampleBuild" method="dokkaGfm" /> <pattern value="com.example.ExampleBuild" method="dokkaGfm" />
<pattern value="com.example.ExampleBuild" method="dokkaJekyll" /> <pattern value="com.example.ExampleBuild" method="dokkaJekyll" />
<pattern value="com.example.ExampleBuild" method="detekt" />
<pattern value="com.example.ExampleBuild" method="detektBaseline" />
<pattern value="com.example.ExampleBuild" method="detektMain" />
<pattern value="com.example.ExampleBuild" method="detektTest" />
</component> </component>
<component name="PDMPlugin"> <component name="PDMPlugin">
<option name="customRuleSets"> <option name="customRuleSets">

View file

@ -1,4 +1,4 @@
j## Compile the Kotlin Example ## Compile the Kotlin Example
```console ```console
./bld compile ./bld compile
@ -8,6 +8,9 @@ j## Compile the Kotlin Example
```console ```console
./bld detekt ./bld detekt
./bld detekt-main
./bld detekt-test
``` ```
## Generate Detekt baseline ## Generate Detekt baseline
@ -16,4 +19,4 @@ j## Compile the Kotlin Example
./bld detekt-baseline ./bld detekt-baseline
``` ```
The `detekt-baseline.xml` will be created in the project's root directory. The `detekt-baseline.xml` file will be created in the project's root directory.

View file

@ -4,14 +4,10 @@ import rife.bld.BuildCommand;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.extension.CompileKotlinOperation; import rife.bld.extension.CompileKotlinOperation;
import rife.bld.extension.DetektOperation; import rife.bld.extension.DetektOperation;
import rife.bld.extension.dokka.DokkaOperation;
import rife.bld.extension.dokka.LoggingLevel;
import rife.bld.extension.dokka.OutputFormat;
import rife.bld.operations.exceptions.ExitStatusException; import rife.bld.operations.exceptions.ExitStatusException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.logging.ConsoleHandler; import java.util.logging.ConsoleHandler;
import java.util.logging.Level; import java.util.logging.Level;
@ -49,7 +45,7 @@ public class ExampleBuild extends Project {
var logger = Logger.getLogger("rife.bld.extension"); var logger = Logger.getLogger("rife.bld.extension");
var consoleHandler = new ConsoleHandler(); var consoleHandler = new ConsoleHandler();
// Enable detailed logging for the Kotlin extension // Enable detailed logging
consoleHandler.setLevel(level); consoleHandler.setLevel(level);
logger.addHandler(consoleHandler); logger.addHandler(consoleHandler);
logger.setLevel(level); logger.setLevel(level);
@ -58,23 +54,22 @@ public class ExampleBuild extends Project {
new ExampleBuild().start(args); new ExampleBuild().start(args);
} }
@BuildCommand(summary = "Compile the Kotlin project") @BuildCommand(summary = "Compiles the Kotlin project")
@Override @Override
public void compile() throws IOException { public void compile() throws IOException {
// The source code located in src/main/kotlin and src/test/kotlin will be compiled
new CompileKotlinOperation() new CompileKotlinOperation()
.fromProject(this) .fromProject(this)
.execute(); .execute();
} }
@BuildCommand(summary = "Check source with Detekt") @BuildCommand(summary = "Checks source with Detekt")
public void detekt() throws ExitStatusException, IOException, InterruptedException { public void detekt() throws ExitStatusException, IOException, InterruptedException {
new DetektOperation() new DetektOperation()
.fromProject(this) .fromProject(this)
.execute(); .execute();
} }
@BuildCommand(value = "detekt-baseline", summary = "Creates a Detekt baseline") @BuildCommand(value = "detekt-baseline", summary = "Creates the Detekt baseline")
public void detektBaseline() throws ExitStatusException, IOException, InterruptedException { public void detektBaseline() throws ExitStatusException, IOException, InterruptedException {
new DetektOperation() new DetektOperation()
.fromProject(this) .fromProject(this)
@ -82,4 +77,18 @@ public class ExampleBuild extends Project {
.createBaseline(true) .createBaseline(true)
.execute(); .execute();
} }
@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();
}
@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();
}
} }

View file

@ -1,10 +1,5 @@
package com.example package com.example
import java.io.IOException
/**
* Example class.
*/
class Example { class Example {
val message: String val message: String
get() = "Hello World!" get() = "Hello World!"

View file

@ -1,6 +1,6 @@
bld.downloadExtensionJavadoc=false bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true bld.downloadExtensionSources=true
bld.extension-jacoco=com.uwyn.rife2:bld-jacoco-report:0.9.1 bld.extension-pmd=com.uwyn.rife2:bld-pmd:0.9.4
bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.downloadLocation= bld.downloadLocation=
bld.sourceDirectories= bld.sourceDirectories=

View file

@ -16,6 +16,7 @@
package rife.bld.extension; package rife.bld.extension;
import rife.bld.BuildCommand;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.publish.PublishDeveloper; import rife.bld.publish.PublishDeveloper;
import rife.bld.publish.PublishLicense; import rife.bld.publish.PublishLicense;
@ -40,13 +41,11 @@ public class DetektOperationBuild extends Project {
autoDownloadPurge = true; autoDownloadPurge = true;
repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES); repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
var detekt = version(1, 23, 3);
scope(compile) scope(compile)
.include(dependency("com.uwyn.rife2", "bld", version(1, 7, 5))) .include(dependency("com.uwyn.rife2", "bld", version(1, 7, 5)))
.include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", version(1, 23, 3))) .include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", detekt))
// .include(dependency("io.gitlab.arturbosch.detekt", "detekt-core", version(1, 23, 3))) .include(dependency("io.gitlab.arturbosch.detekt", "detekt-tooling", detekt))
// .include(dependency("io.gitlab.arturbosch.detekt", "detekt-rules", version(1, 23, 3)))
// .include(dependency("io.gitlab.arturbosch.detekt", "detekt-utils", version(1, 23, 3)))
.include(dependency("io.gitlab.arturbosch.detekt", "detekt-tooling", version(1, 23, 3)))
.include(dependency("com.beust", "jcommander", "1.82")) .include(dependency("com.beust", "jcommander", "1.82"))
.include(dependency("com.fasterxml:aalto-xml:1.3.2")); .include(dependency("com.fasterxml:aalto-xml:1.3.2"));
scope(test) scope(test)
@ -93,4 +92,13 @@ public class DetektOperationBuild extends Project {
public static void main(String[] args) { public static void main(String[] args) {
new DetektOperationBuild().start(args); new DetektOperationBuild().start(args);
} }
@BuildCommand(summary = "Check source code with PMD")
public void pmd() {
new PmdOperation()
.fromProject(this)
.ruleSets("config/pmd.xml")
.execute();
}
} }

View file

@ -40,15 +40,15 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
private final Collection<String> input_ = new ArrayList<>(); private final Collection<String> input_ = new ArrayList<>();
private final Collection<String> plugins_ = new ArrayList<>(); private final Collection<String> plugins_ = new ArrayList<>();
private final Collection<DetektReport> report_ = new ArrayList<>(); private final Collection<DetektReport> report_ = new ArrayList<>();
private boolean allRules_ = false; private boolean allRules_;
private boolean autoCorrect_ = false; private boolean autoCorrect_;
private String basePath_; private String basePath_;
private String baseline_; private String baseline_;
private boolean buildUponDefaultConfig_; private boolean buildUponDefaultConfig_;
private String configResource_; private String configResource_;
private boolean createBaseline_; private boolean createBaseline_;
private boolean debug_ = false; private boolean debug_;
private boolean disableDefaultRuleSets_ = false; private boolean disableDefaultRuleSets_;
private String excludes_; private String excludes_;
private boolean generateConfig_; private boolean generateConfig_;
private String includes_; private String includes_;
@ -448,6 +448,15 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
return this; return this;
} }
/**
* Returns the input paths to analyze.
*
* @return the input paths
*/
public Collection<String> input() {
return input_;
}
/** /**
* EXPERIMENTAL: Use a custom JDK home directory to include into the * EXPERIMENTAL: Use a custom JDK home directory to include into the
* classpath. * classpath.
@ -514,9 +523,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
/** /**
* Enables parallel compilation and analysis of source files. Do some * Extra paths to plugin jars.
* benchmarks first before enabling this flag. Heuristics show performance
* benefits starting from 2000 lines of Kotlin code.
* *
* @param jars one or more jars * @param jars one or more jars
* @return this operation instance * @return this operation instance
@ -527,9 +534,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
/** /**
* Enables parallel compilation and analysis of source files. Do some * Extra paths to plugin jars.
* benchmarks first before enabling this flag. Heuristics show performance
* benefits starting from 2000 lines of Kotlin code.
* *
* @param jars the list of jars * @param jars the list of jars
* @return this operation instance * @return this operation instance

View file

@ -29,9 +29,10 @@ import java.util.logging.ConsoleHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*; // NOPMD
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
class DetektOperationTest { class DetektOperationTest {
@BeforeAll @BeforeAll
static void beforeAll() { static void beforeAll() {