xref: /aosp_15_r20/external/robolectric/buildSrc/src/main/java/org/robolectric/gradle/SpotlessPlugin.kt (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1 package org.robolectric.gradle
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 class SpotlessPlugin : Plugin<Project> {
applynull9   override fun apply(project: Project) {
10     project.pluginManager.apply("com.diffplug.spotless")
11 
12     project.extensions.configure<SpotlessExtension> {
13       // Add configurations for Kotlin files
14       kotlin {
15         target("**/*.kt")
16         ktfmt("0.49").googleStyle()
17       }
18 
19       // Add configurations for Kotlin Gradle files
20       kotlinGradle {
21         target("**/*.kts")
22         ktfmt("0.49").googleStyle()
23       }
24 
25       // Only apply yaml and json formatting for root project
26       // to avoid some files are added into multiple project's spotless targets.
27       if (project.rootProject == project) {
28         // Add configurations for JSON files
29         json {
30           target("**/*.json")
31           gson()
32             .indentWithSpaces(2) // Follow code's indent.
33             .sortByKeys()
34             .escapeHtml()
35         }
36       }
37     }
38   }
39 }
40