|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-build-conventions-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.getstream.android |
| 17 | + |
| 18 | +import com.diffplug.gradle.spotless.SpotlessExtension |
| 19 | +import org.gradle.api.Project |
| 20 | +import org.gradle.api.Task |
| 21 | +import org.gradle.api.tasks.TaskContainer |
| 22 | +import org.gradle.kotlin.dsl.configure |
| 23 | +import org.gradle.kotlin.dsl.findByType |
| 24 | +import org.gradle.kotlin.dsl.named |
| 25 | +import org.gradle.kotlin.dsl.register |
| 26 | + |
| 27 | +internal fun Project.configureSpotless() { |
| 28 | + pluginManager.apply("com.diffplug.spotless") |
| 29 | + |
| 30 | + afterEvaluate { |
| 31 | + // Get configuration from root project |
| 32 | + val projectExtension = requireStreamProjectExtension() |
| 33 | + |
| 34 | + // Get module-specific configuration |
| 35 | + val moduleExtension = extensions.findByType<StreamModuleExtension>() |
| 36 | + |
| 37 | + // Check if Spotless should be disabled for this module |
| 38 | + if (moduleExtension?.disableSpotless?.getOrElse(false) == true) { |
| 39 | + return@afterEvaluate |
| 40 | + } |
| 41 | + |
| 42 | + val repositoryName = |
| 43 | + projectExtension.repositoryName.orNull |
| 44 | + ?: error("streamProject.repositoryName must be configured in the root project") |
| 45 | + |
| 46 | + val useKtfmt = projectExtension.useKtfmt.getOrElse(false) |
| 47 | + |
| 48 | + val generateKotlinLicenseTask = |
| 49 | + registerLicenseGenerationTask( |
| 50 | + taskName = "generateKotlinLicenseHeader", |
| 51 | + repositoryName = repositoryName, |
| 52 | + templateName = "license-header.txt", |
| 53 | + fileName = "license.kt", |
| 54 | + ) |
| 55 | + |
| 56 | + val generateXmlLicenseTask = |
| 57 | + registerLicenseGenerationTask( |
| 58 | + taskName = "generateXmlLicenseHeader", |
| 59 | + repositoryName = repositoryName, |
| 60 | + templateName = "license-header.xml", |
| 61 | + fileName = "license.xml", |
| 62 | + ) |
| 63 | + |
| 64 | + extensions.configure<SpotlessExtension> { |
| 65 | + val kotlinLicenseFile = generateKotlinLicenseTask.get().outputFile.get().asFile |
| 66 | + val xmlLicenseFile = generateXmlLicenseTask.get().outputFile.get().asFile |
| 67 | + |
| 68 | + encoding = Charsets.UTF_8 |
| 69 | + kotlin { |
| 70 | + target("**/*.kt") |
| 71 | + targetExclude("**/build/**/*.kt") |
| 72 | + |
| 73 | + if (useKtfmt) { |
| 74 | + ktfmt().kotlinlangStyle() |
| 75 | + } else { |
| 76 | + // For now, we are fixing the ktlint version to the one currently used by Chat & |
| 77 | + // Video to dodge this issue: https://github.com/diffplug/spotless/issues/1913 |
| 78 | + ktlint("0.50.0") |
| 79 | + .editorConfigOverride( |
| 80 | + mapOf("ktlint_standard_max-line-length" to "disabled") |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + trimTrailingWhitespace() |
| 85 | + endWithNewline() |
| 86 | + licenseHeaderFile(kotlinLicenseFile) |
| 87 | + } |
| 88 | + java { |
| 89 | + importOrder() |
| 90 | + removeUnusedImports() |
| 91 | + googleJavaFormat() |
| 92 | + trimTrailingWhitespace() |
| 93 | + endWithNewline() |
| 94 | + licenseHeaderFile(kotlinLicenseFile) |
| 95 | + } |
| 96 | + kotlinGradle { |
| 97 | + target("*.gradle.kts") |
| 98 | + ktlint() |
| 99 | + trimTrailingWhitespace() |
| 100 | + endWithNewline() |
| 101 | + } |
| 102 | + format("xml") { |
| 103 | + target("**/*.xml") |
| 104 | + targetExclude("**/build/**/*.xml", "**/detekt-baseline.xml") |
| 105 | + licenseHeaderFile(xmlLicenseFile, "(<[^!?])") |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Make Spotless tasks depend on license generation tasks |
| 110 | + tasks |
| 111 | + .matching { it.name.startsWith("spotless") } |
| 112 | + .configureEach { dependsOn(generateKotlinLicenseTask, generateXmlLicenseTask) } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Registers a task to generate a license file from a bundled template. Safe to call from multiple |
| 118 | + * subprojects, as it will reuse the task if it already exists. |
| 119 | + */ |
| 120 | +private fun Project.registerLicenseGenerationTask( |
| 121 | + taskName: String, |
| 122 | + repositoryName: String, |
| 123 | + templateName: String, |
| 124 | + fileName: String, |
| 125 | +) = |
| 126 | + rootProject.tasks.findOrRegister<GenerateLicenseFileTask>(taskName) { |
| 127 | + this.repositoryName.set(repositoryName) |
| 128 | + this.templateName.set(templateName) |
| 129 | + |
| 130 | + // Set the plugin's classpath as an input so Gradle tracks when the plugin JAR changes |
| 131 | + pluginClasspath.from( |
| 132 | + GenerateLicenseFileTask::class.java.protectionDomain.codeSource.location |
| 133 | + ) |
| 134 | + |
| 135 | + // Set the output file location |
| 136 | + val outputDir = rootProject.layout.buildDirectory.dir("stream-spotless-config") |
| 137 | + outputFile.set(outputDir.map { it.file(fileName) }) |
| 138 | + } |
| 139 | + |
| 140 | +private inline fun <reified T : Task> TaskContainer.findOrRegister( |
| 141 | + name: String, |
| 142 | + noinline configuration: T.() -> Unit, |
| 143 | +) = findByName(name)?.let { named<T>(name) } ?: register<T>(name, configuration) |
0 commit comments