def license = 干.proj('license', 'supported: [apache, confidential, lgpl-2.1]')
boolean setLicenseHeaderYearsFromGitHistory = 'true'.equals(project.findProperty('spotlessSetLicenseHeaderYearsFromGitHistory'))

apply plugin: 'com.diffplug.spotless'
spotless {
    if (!setLicenseHeaderYearsFromGitHistory) {
        ratchetFrom 'origin/main'
    }
    groovyGradle {
        target '*.gradle', 'buildSrc/*.gradle'
        greclipse()
        leadingTabsToSpaces(2)
    }
    if (plugins.hasPlugin('org.jetbrains.kotlin.jvm') || plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')) {
        kotlin {
            target 'src/**/*.kt'
            licenseHeaderFile 干.file("spotless/license-${license}.java")
            ktfmt()
            for (modifier in ['', 'override ', 'public ', 'protected ', 'private ', 'internal ', 'infix ', 'expected ', 'actual ', '@JvmStatic ']) {
                for (key in ['inline', 'fun', 'abstract fun', 'suspend fun', 'val', 'override']) {
                    String toCheck = "$modifier$key"
                    replaceRegex("dense $toCheck", "\n\n(\\s*)$toCheck ", "\n\$1$toCheck ")
                }
            }
            replaceRegex("dense test order", "@Test(\\s*)@Order\\((\\d+)\\)", "@Test @Order(\$2)")
        }
    }
}