Skip to content

Commit

Permalink
Updated README.md for Diktat 2.0 (#1839)
Browse files Browse the repository at this point in the history
- updated section about CLI
- updated sections for plugins
  • Loading branch information
nulls authored Dec 19, 2023
1 parent b8062d8 commit 941679c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 45 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ target/
.gradle/
build/
!ktlint/src/main/resources/config/.idea
/.idea/
.idea/
*.iml
/examples/*/gradlew
/examples/*/gradlew.bat
/examples/*/gradle/wrapper/
out/
.DS_Store

Expand Down
71 changes: 50 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Main features of diktat are the following:
curl -sSLO https://github.com/saveourtool/diktat/releases/download/v2.0.0/diktat && chmod a+x diktat
```

2. _**For Windows only**_. Download diKTat.bat manually: [here](https://github.com/saveourtool/diktat/releases)

### Run diKTat

Finally, run KTlint (with diKTat injected) to check your '*.kt' files in 'dir/your/dir':
Expand All @@ -62,17 +64,19 @@ Finally, run KTlint (with diKTat injected) to check your '*.kt' files in 'dir/yo
$ ./diktat "dir/your/dir/**/*.kt"
```

> _**On Windows**_
> ```console
> ./diktat.bat "dir/your/dir/**/*.kt"
> ```
To **autofix** all code style violations, use `--mode fix` option.
## Run with Maven using diktat-maven-plugin
:heavy_exclamation_mark: If you are using **Java 16+**, you need to add `--add-opens java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED` flag to the JVM. For more information, see: https://github.com/pinterest/ktlint/issues/1195
This can be done by setting `MAVEN_OPTS` variable:
```
export MAVEN_OPTS="--add-opens java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED"
```
You can see how it is configured in our examples for self-checks:
- [Single project](examples/maven/pom.xml)
- [Multi-module project](examples/maven-multiproject/pom.xml)
This plugin is available since version 0.1.3. You can see how it is configured in our examples for self-checks: [examples](examples/maven/pom.xml).
If you use it and encounter any problems, feel free to open issues on [github](https://github.com/saveourtool/diktat/issues).
<details>
Expand Down Expand Up @@ -153,6 +157,9 @@ Requesting a specific _Maven_ `executionId` on the command line (the trailing
```
</details>

* You can omit the `diktatConfigFile` or if it points to non-existed file
then DiKTat runs with default configuration.

If you omit the `executionId`:

```console
Expand All @@ -164,31 +171,47 @@ $ mvn diktat:check
rule sets by editing the YAML file).

## Run with Gradle using diktat-gradle-plugin
Requires a gradle version no lower than 5.3.
Requires a gradle version no lower than 7.0

This plugin is available since version 0.1.5. You can see how the plugin is configured in our examples: [build.gradle.kts](examples/gradle-kotlin-dsl/build.gradle.kts).
You can see how the plugin is configured in our examples:
- [Kotlin DSL](examples/gradle-kotlin-dsl/build.gradle.kts)
- [Kotlin DSL for multi-module project](examples/gradle-kotlin-dsl/build.gradle.kts)
- [Groovy DSL](examples/gradle-kotlin-dsl/build.gradle.kts)

<details>
<summary>Add this plugin to your `build.gradle.kts`:</summary>

```kotlin
plugins {
id("com.saveourtool.diktat.diktat-gradle-plugin") version "1.2.5"
id("com.saveourtool.diktat") version "2.0.0"
}
```

> _**Note**_ If you want to apply the plugin to multi-module projects"
> ```kotlin
> import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin
>
> plugins {
> id("com.saveourtool.diktat") version "2.0.0" apply false
> }
>
> allprojects {
> apply<DiktatGradlePlugin>()
> }
> ```
Or use buildscript syntax:
```kotlin
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.saveourtool.diktat:diktat-gradle-plugin:1.2.5")
classpath("com.saveourtool.diktat:diktat-gradle-plugin:2.0.0")
}
}
apply(plugin = "com.saveourtool.diktat.diktat-gradle-plugin")
apply(plugin = "com.saveourtool.diktat")
```
You can then configure diktat using `diktat` extension:
Expand All @@ -204,14 +227,20 @@ diktat {

Also in `diktat` extension you can configure different reporters and their output. You can specify `json`, `html`, `sarif`, `plain` (default).
If `output` is set, it should be a file path. If not set, results will be printed to stdout.
You can specify multiple reporters.
If no reporter is specified, `plain` will be used with `stdout` as output.
```kotlin
diktat {
// since 1.2.5 to keep in line with maven properties
reporter = "json" // "html", "json", "plain" (default), "sarif"
// before 1.2.5
// reporterType = "json" // "html", "json", "plain" (default), "sarif"

output = "someFile.json"
reporters {
plain()
json()
html {
output = file("someFile.html")
}
// checkstyle()
// sarif()
// gitHubActions()
}
}
```

Expand Down Expand Up @@ -250,7 +279,7 @@ spotless {
```kotlin
spotless {
kotlin {
diktat("1.2.5").configFile("full/path/to/diktat-analysis.yml")
diktat("2.0.0").configFile("full/path/to/diktat-analysis.yml")
}
}
```
Expand Down Expand Up @@ -281,14 +310,14 @@ Diktat can be run via spotless-maven-plugin since version 2.8.0

```xml
<diktat>
<version>1.2.5</version> <!-- optional -->
<version>2.0.0</version> <!-- optional -->
<configFile>full/path/to/diktat-analysis.yml</configFile> <!-- optional, configuration file path -->
</diktat>
```
</details>

## GitHub Integration
We suggest everyone to use common ["sarif"](https://docs.oasis-open.org/sarif/sarif/v2.0/sarif-v2.0.html) format as a `reporter` (`reporterType`) in CI/CD.
We suggest everyone to use common ["sarif"](https://docs.oasis-open.org/sarif/sarif/v2.0/sarif-v2.0.html) format as a `reporter` in CI/CD.
GitHub has an [integration](https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning)
with SARIF format and provides you a native reporting of diktat issues in Pull Requests.

Expand Down Expand Up @@ -328,7 +357,7 @@ the limit will be exceeded and the step will fail. To solve this issue one can m

`diktat-gradle-plugin` provides this capability with `mergeDiktatReports` task. This task aggregates reports of all diktat tasks
of all Gradle project, which produce SARIF reports, and outputs the merged report into root project's build directory. Then this single
file can be used as an input for Github action:
file can be used as an input for GitHub action:
```yaml
with:
sarif_file: build/reports/diktat/diktat-merged.sarif
Expand Down
2 changes: 1 addition & 1 deletion diktat-cli/src/test/resources/test/smoke/build.gradle.kts_
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import com.saveourtool.diktat.generation.docs.generateAvailableRules

plugins {
kotlin("jvm") version "1.4.21"
id("com.saveourtool.diktat.diktat-gradle-plugin") version "0.1.7"
id("com.saveourtool.diktat") version "2.0.0"
}

repositories {
Expand Down
19 changes: 4 additions & 15 deletions diktat-gradle-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
## Building
Build of gradle plugin is performed by gradle, but is wrapped in maven build. The module's `pom.xml` isn't exactly accurate
and doesn't include gradle-specific dependencies, that are automatically provided by gradle when applying the plugin.
## Local testing

To avoid versions duplication, diktat and ktlint versions are passed to gradle via properties when running gradle task from maven.
These versions are then written in a file and then included in the plugin jar to determine dependencies for JavaExec.

Gradle plugin marker pom, which is normally produced by `java-gradle-plugin` plugin during gradle build,
is added manually as a maven module.

Please be advised that to run functional tests of Gradle plugin you will need to have Java 11 or **older**.
This does not affect the plugin itself and only affect functional tests :
```
Starting Build
java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7
```
You can build and publish to maven local by using `gradlew :diktat-gradle-plugin:publishToMavenLocal`.
Then you can use a built version in projects in examples.
A calculated version will be printed in logs by `reckon` plugin.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal fun createExampleProject(testProjectDir: TemporaryFolder,
testProjectDir.newFile(buildFileName).writeText(
"""
plugins {
id("com.saveourtool.diktat.diktat-gradle-plugin")
id("com.saveourtool.diktat")
}
repositories {
Expand Down
10 changes: 4 additions & 6 deletions diktat-maven-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
## Building
To build and test the plugin, use regular maven commands.
## Local testing

To generate plugin descriptor using data from KDocs, we use [kotlin-maven-plugin-tools](https://github.com/gantsign/kotlin-maven-plugin-tools).
This plugin is only available in github packages, which require authentication via `settings.xml`. However,
this plugin is activated only in release profile (`-Prelease`) and package repository doesn't require any authentication for local development.
If you need to run it locally, see [release.yml](../.github/workflows/release.yml) as an example of adding an entry to `settings.xml` `servers` section.
You can build and publish to maven local by using `:diktat-api:publishToMavenLocal :diktat-rules:publishToMavenLocal :diktat-runner:publishToMavenLocal :diktat-common:publishToMavenLocal :diktat-ktlint-engine:publishToMavenLocal :diktat-maven-plugin:publishToMavenLocal`.
Then you can use a built version in projects in examples.
A calculated version will be printed in logs by `reckon` plugin.

0 comments on commit 941679c

Please sign in to comment.