|
| 1 | +package net.thunderbird.gradle.plugin.quality.spotless |
| 2 | + |
| 3 | +import com.diffplug.gradle.spotless.SpotlessExtension |
| 4 | +import org.gradle.api.Plugin |
| 5 | +import org.gradle.api.Project |
| 6 | +import org.gradle.kotlin.dsl.configure |
| 7 | + |
| 8 | +/** |
| 9 | + * A Gradle plugin to configure Spotless code formatting for Kotlin, Kotlin Gradle scripts, Markdown, |
| 10 | + * and miscellaneous files like .gitignore. |
| 11 | + */ |
| 12 | +class SpotlessPlugin : Plugin<Project> { |
| 13 | + override fun apply(target: Project) { |
| 14 | + with(target) { |
| 15 | + pluginManager.apply("com.diffplug.spotless") |
| 16 | + |
| 17 | + if (this == rootProject) { |
| 18 | + configureSpotlessRoot() |
| 19 | + } else { |
| 20 | + configureSpotless() |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private fun Project.configureSpotless() { |
| 26 | + extensions.configure<SpotlessExtension> { |
| 27 | + kotlin { |
| 28 | + target( |
| 29 | + "src/*/kotlin/*.kt", |
| 30 | + "src/*/kotlin/**/*.kt", |
| 31 | + ) |
| 32 | + |
| 33 | + ktlint() |
| 34 | + .setEditorConfigPath("${rootProject.projectDir}/.editorconfig") |
| 35 | + .editorConfigOverride(kotlinEditorConfigOverride) |
| 36 | + } |
| 37 | + |
| 38 | + kotlinGradle { |
| 39 | + target( |
| 40 | + "*.gradle.kts", |
| 41 | + ) |
| 42 | + |
| 43 | + ktlint() |
| 44 | + .setEditorConfigPath("${rootProject.projectDir}/.editorconfig") |
| 45 | + .editorConfigOverride( |
| 46 | + mapOf( |
| 47 | + "ktlint_code_style" to "intellij_idea", |
| 48 | + "ktlint_standard_function-expression-body" to "disabled", |
| 49 | + "ktlint_standard_function-signature" to "disabled", |
| 50 | + ), |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + flexmark { |
| 55 | + target( |
| 56 | + "*.md", |
| 57 | + ) |
| 58 | + flexmark() |
| 59 | + } |
| 60 | + |
| 61 | + format("misc") { |
| 62 | + target(".gitignore") |
| 63 | + trimTrailingWhitespace() |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private fun Project.configureSpotlessRoot() { |
| 69 | + extensions.configure<SpotlessExtension> { |
| 70 | + kotlin { |
| 71 | + target( |
| 72 | + "build-plugin/plugin/src/*/kotlin/*.kt", |
| 73 | + "build-plugin/plugin/src/*/kotlin/**/*.kt", |
| 74 | + ) |
| 75 | + ktlint() |
| 76 | + .setEditorConfigPath("${project.rootProject.projectDir}/.editorconfig") |
| 77 | + .editorConfigOverride(kotlinEditorConfigOverride) |
| 78 | + } |
| 79 | + |
| 80 | + kotlinGradle { |
| 81 | + target( |
| 82 | + "*.gradle.kts", |
| 83 | + "build-plugin/*.gradle.kts", |
| 84 | + "build-plugin/plugin/*.gradle.kts", |
| 85 | + ) |
| 86 | + |
| 87 | + ktlint() |
| 88 | + .setEditorConfigPath("${project.rootProject.projectDir}/.editorconfig") |
| 89 | + .editorConfigOverride( |
| 90 | + mapOf( |
| 91 | + "ktlint_code_style" to "intellij_idea", |
| 92 | + "ktlint_standard_function-expression-body" to "disabled", |
| 93 | + "ktlint_standard_function-signature" to "disabled", |
| 94 | + ), |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + flexmark { |
| 99 | + target( |
| 100 | + "*.md", |
| 101 | + "docs/*.md", |
| 102 | + "docs/**/*.md", |
| 103 | + ) |
| 104 | + flexmark() |
| 105 | + } |
| 106 | + |
| 107 | + format("misc") { |
| 108 | + target(".gitignore") |
| 109 | + trimTrailingWhitespace() |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments