Skip to content

Commit 8483398

Browse files
committed
Add spotless plugin with ktfmt configuration
1 parent 4f65b3f commit 8483398

2 files changed

Lines changed: 75 additions & 63 deletions

File tree

buildSrc/src/main/kotlin/faker-kotlin-conventions.gradle.kts

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
1414
import utils.Color
1515
import utils.times
1616

17-
/**
18-
* Plugin for base build setup of faker modules with kotlin
19-
*/
20-
17+
/** Plugin for base build setup of faker modules with kotlin */
2118
plugins {
2219
id("faker-base-conventions")
2320
java
2421
kotlin("jvm")
2522
id("org.jetbrains.dokka")
2623
id("org.jetbrains.kotlinx.binary-compatibility-validator")
24+
id("com.diffplug.spotless")
2725
}
2826

2927
val lib = project.libs
@@ -38,21 +36,13 @@ dependencies {
3836
testImplementation(lib.bundles.test.kotest)
3937
}
4038

41-
configure<JavaPluginExtension> {
42-
toolchain {
43-
languageVersion.set(JavaLanguageVersion.of(8))
44-
}
45-
}
39+
configure<JavaPluginExtension> { toolchain { languageVersion.set(JavaLanguageVersion.of(8)) } }
4640

4741
configure<KotlinJvmProjectExtension> {
48-
jvmToolchain {
49-
languageVersion.set(JavaLanguageVersion.of(8))
50-
}
42+
jvmToolchain { languageVersion.set(JavaLanguageVersion.of(8)) }
5143
}
5244

53-
tasks.withType<JavaCompile> {
54-
options.encoding = "UTF-8"
55-
}
45+
tasks.withType<JavaCompile> { options.encoding = "UTF-8" }
5646

5747
tasks.withType<Test> {
5848
jvmArgs = jvmArgs?.plus("-ea") ?: listOf("-ea")
@@ -62,59 +52,72 @@ tasks.withType<Test> {
6252

6353
testLogging {
6454
// set options for log level LIFECYCLE
65-
events = setOf(
66-
TestLogEvent.FAILED,
67-
TestLogEvent.SKIPPED,
68-
TestLogEvent.STANDARD_OUT
69-
)
55+
events = setOf(TestLogEvent.FAILED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_OUT)
7056
exceptionFormat = TestExceptionFormat.FULL
7157
showExceptions = true
7258
showCauses = true
7359
showStackTraces = true
7460
// set options for log level DEBUG and INFO
7561
debug {
76-
events = setOf(
77-
TestLogEvent.STARTED,
78-
TestLogEvent.FAILED,
79-
TestLogEvent.PASSED,
80-
TestLogEvent.SKIPPED,
81-
TestLogEvent.STANDARD_ERROR,
82-
TestLogEvent.STANDARD_OUT
83-
)
62+
events =
63+
setOf(
64+
TestLogEvent.STARTED,
65+
TestLogEvent.FAILED,
66+
TestLogEvent.PASSED,
67+
TestLogEvent.SKIPPED,
68+
TestLogEvent.STANDARD_ERROR,
69+
TestLogEvent.STANDARD_OUT,
70+
)
8471
exceptionFormat = TestExceptionFormat.FULL
8572
}
8673
info.events = debug.events
8774
info.exceptionFormat = debug.exceptionFormat
8875

89-
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
90-
if (desc.parent == null) { // will match the outermost suite
91-
val pass = "${Color.GREEN}${result.successfulTestCount} passed${Color.NONE}"
92-
val fail = "${Color.RED}${result.failedTestCount} failed${Color.NONE}"
93-
val skip = "${Color.YELLOW}${result.skippedTestCount} skipped${Color.NONE}"
94-
val type = when (val r: ResultType = result.resultType) {
95-
ResultType.SUCCESS -> "${Color.GREEN}$r${Color.NONE}"
96-
ResultType.FAILURE -> "${Color.RED}$r${Color.NONE}"
97-
ResultType.SKIPPED -> "${Color.YELLOW}$r${Color.NONE}"
76+
afterSuite(
77+
KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
78+
if (desc.parent == null) { // will match the outermost suite
79+
val pass = "${Color.GREEN}${result.successfulTestCount} passed${Color.NONE}"
80+
val fail = "${Color.RED}${result.failedTestCount} failed${Color.NONE}"
81+
val skip = "${Color.YELLOW}${result.skippedTestCount} skipped${Color.NONE}"
82+
val type =
83+
when (val r: ResultType = result.resultType) {
84+
ResultType.SUCCESS -> "${Color.GREEN}$r${Color.NONE}"
85+
ResultType.FAILURE -> "${Color.RED}$r${Color.NONE}"
86+
ResultType.SKIPPED -> "${Color.YELLOW}$r${Color.NONE}"
87+
}
88+
val output = "Results: $type (${result.testCount} tests, $pass, $fail, $skip)"
89+
val startItem = "| "
90+
val endItem = " |"
91+
val repeatLength = startItem.length + output.length + endItem.length - 36
92+
println("")
93+
println(
94+
"\n" +
95+
("-" * repeatLength) +
96+
"\n" +
97+
startItem +
98+
output +
99+
endItem +
100+
"\n" +
101+
("-" * repeatLength)
102+
)
98103
}
99-
val output = "Results: $type (${result.testCount} tests, $pass, $fail, $skip)"
100-
val startItem = "| "
101-
val endItem = " |"
102-
val repeatLength = startItem.length + output.length + endItem.length - 36
103-
println("")
104-
println("\n" + ("-" * repeatLength) + "\n" + startItem + output + endItem + "\n" + ("-" * repeatLength))
105-
}
106-
}))
104+
})
105+
)
107106
}
108107

109-
onOutput(KotlinClosure2({ _: TestDescriptor, event: TestOutputEvent ->
110-
if (event.destination == TestOutputEvent.Destination.StdOut) {
111-
logger.lifecycle(event.message.replace(Regex("""\s+$"""), ""))
112-
}
113-
}))
108+
onOutput(
109+
KotlinClosure2({ _: TestDescriptor, event: TestOutputEvent ->
110+
if (event.destination == TestOutputEvent.Destination.StdOut) {
111+
logger.lifecycle(event.message.replace(Regex("""\s+$"""), ""))
112+
}
113+
})
114+
)
114115
}
115116

116117
configurations {
117-
create("integrationImplementation") { extendsFrom(configurations.getByName("testImplementation")) }
118+
create("integrationImplementation") {
119+
extendsFrom(configurations.getByName("testImplementation"))
120+
}
118121
create("integrationRuntimeOnly") {
119122
if (isShadow) {
120123
extendsFrom(
@@ -127,25 +130,23 @@ configurations {
127130
}
128131
}
129132

130-
// configure sourceSets as extension since it's not available here as `sourceSets` is an extension on `Project`
133+
// configure sourceSets as extension since it's not available here as `sourceSets` is an extension
134+
// on `Project`
131135
// https://docs.gradle.org/current/userguide/kotlin_dsl.html#project_extensions_and_conventions
132136
configure<SourceSetContainer> {
133137
create("integration") {
134138
resources.srcDir("src/integration/resources")
135139
compileClasspath += main.get().compileClasspath + test.get().compileClasspath
136140
runtimeClasspath += main.get().runtimeClasspath + test.get().runtimeClasspath
137141
}
138-
main {
139-
resources {
140-
this.srcDir("build/generated/src/main/resources")
141-
}
142-
}
142+
main { resources { this.srcDir("build/generated/src/main/resources") } }
143143
}
144144

145-
val integrationTest: Test by tasks.creating(Test::class) {
146-
testClassesDirs = sourceSets["integration"].output.classesDirs
147-
classpath = sourceSets["integration"].runtimeClasspath
148-
}
145+
val integrationTest: Test by
146+
tasks.creating(Test::class) {
147+
testClassesDirs = sourceSets["integration"].output.classesDirs
148+
classpath = sourceSets["integration"].runtimeClasspath
149+
}
149150

150151
tasks.withType<Jar> {
151152
archiveBaseName.set(fullName)
@@ -161,7 +162,8 @@ tasks.withType<Jar> {
161162
* Cannot change dependencies of dependency configuration ':core:implementation' after it has been included in dependency resolution
162163
* if we try to add more dependencies in the module's build.gradle file directly
163164
*/
164-
// "Class-Path" to project.configurations.compileClasspath.get().joinToString(" ") { it.name }
165+
// "Class-Path" to project.configurations.compileClasspath.get().joinToString(" ") {
166+
// it.name }
165167
)
166168
)
167169
}
@@ -172,5 +174,12 @@ tasks.withType<DokkaTask>().configureEach {
172174
onlyIf("Release or snapshot") { isRelease.get() || isSnapshot.get() }
173175
}
174176

175-
apiValidation {
177+
apiValidation {}
178+
179+
spotless {
180+
kotlin { ktfmt().kotlinlangStyle().configure {} }
181+
kotlinGradle {
182+
target("*.gradle.kts")
183+
ktfmt().kotlinlangStyle().configure {}
184+
}
176185
}

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ plugin-semantic-versioning = "0.13.0"
1515
plugin-devpublish = "0.4.1"
1616
plugin-nexus-publish = "2.0.0"
1717
plugin-benmanes-versions = "0.51.0"
18+
plugin-spotless = "7.2.1"
1819
# test
1920
kotest = "5.9.1"
2021
junit = "5.11.0"
@@ -51,6 +52,7 @@ gradle-plugin-bcv = { module = "org.jetbrains.kotlinx.binary-compatibility-valid
5152
gradle-plugin-dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "plugin-dokka" }
5253
gradle-plugin-semantic-versioning = { module = "io.github.serpro69:semantic-versioning", version.ref = "plugin-semantic-versioning" }
5354
gradle-plugin-shadow = { module = "com.gradleup.shadow:shadow-gradle-plugin", version.ref = "plugin-shadow" }
55+
gradle-plugin-spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "plugin-spotless" }
5456
snakeyaml = { module = "org.yaml:snakeyaml", version.ref = "snakeyaml" }
5557
gradle-plugin-devpublish = { module = "dev.adamko.gradle:dev-publish-plugin", version.ref = "plugin-devpublish" }
5658

@@ -67,6 +69,7 @@ gradle-plugins = [
6769
"gradle-plugin-dokka",
6870
"gradle-plugin-semantic-versioning",
6971
"gradle-plugin-shadow",
72+
"gradle-plugin-spotless",
7073
]
7174
jackson = ["jackson-databind", "jackson-module-kotlin"]
7275
kotlin = ["kotlin-stdlib-common", "kotlin-stdlib-jdk8", "kotlin-reflect"]

0 commit comments

Comments
 (0)