diff --git a/CHANGELOG.md b/CHANGELOG.md index 0445c54..9863863 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Version 0.1.2 +* make license header configurable via 'license-header.txt' (#48) * replace 'developer.properties' with generic Hiero entry (#38) * update 'io.github.gradle-nexus:publish-plugin' to 2.0.0 (#40) diff --git a/src/main/kotlin/org.hiero.gradle.check.spotless-java.gradle.kts b/src/main/kotlin/org.hiero.gradle.check.spotless-java.gradle.kts index 42cbf3f..54d87d9 100644 --- a/src/main/kotlin/org.hiero.gradle.check.spotless-java.gradle.kts +++ b/src/main/kotlin/org.hiero.gradle.check.spotless-java.gradle.kts @@ -1,4 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 +import org.hiero.gradle.spotless.LicenseHeader import org.hiero.gradle.spotless.RepairDashedCommentsFormatterStep import org.hiero.gradle.spotless.SortModuleInfoRequiresStep @@ -22,6 +23,7 @@ spotless { // through git history (see "license" section below). // The delimiter override below is required to support some // of our test classes which are in the default package. - licenseHeader("// SPDX-License-Identifier: Apache-2.0\n", "(package|import|module)") + licenseHeader(LicenseHeader.javaFormat(project), "(package|import|module)") + .updateYearWithLatest(true) } } diff --git a/src/main/kotlin/org.hiero.gradle.check.spotless-kotlin.gradle.kts b/src/main/kotlin/org.hiero.gradle.check.spotless-kotlin.gradle.kts index 600baf9..007cb7d 100644 --- a/src/main/kotlin/org.hiero.gradle.check.spotless-kotlin.gradle.kts +++ b/src/main/kotlin/org.hiero.gradle.check.spotless-kotlin.gradle.kts @@ -1,4 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 +import org.hiero.gradle.spotless.LicenseHeader + plugins { id("com.diffplug.spotless") } spotless { @@ -6,8 +8,9 @@ spotless { ktfmt().kotlinlangStyle() licenseHeader( - "// SPDX-License-Identifier: Apache-2.0\n", - "(import|plugins|pluginManagement|dependencyResolutionManagement|repositories|tasks|allprojects|subprojects|buildCache|version)" - ) + LicenseHeader.javaFormat(project), + "(import|plugins|pluginManagement|dependencyResolutionManagement|repositories|tasks|allprojects|subprojects|buildCache|version)" + ) + .updateYearWithLatest(true) } } diff --git a/src/main/kotlin/org.hiero.gradle.check.spotless-yaml.gradle.kts b/src/main/kotlin/org.hiero.gradle.check.spotless-yaml.gradle.kts index 0109312..d33b9d1 100644 --- a/src/main/kotlin/org.hiero.gradle.check.spotless-yaml.gradle.kts +++ b/src/main/kotlin/org.hiero.gradle.check.spotless-yaml.gradle.kts @@ -1,4 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 +import org.hiero.gradle.spotless.LicenseHeader + plugins { id("com.diffplug.spotless") } spotless { @@ -18,6 +20,6 @@ spotless { indentWithSpaces() endWithNewline() - licenseHeader("# SPDX-License-Identifier: Apache-2.0\n", "(name)") + licenseHeader(LicenseHeader.yamlFormat(project), "(name)").updateYearWithLatest(true) } } diff --git a/src/main/kotlin/org/hiero/gradle/spotless/LicenseHeader.kt b/src/main/kotlin/org/hiero/gradle/spotless/LicenseHeader.kt new file mode 100644 index 0000000..dc5a89d --- /dev/null +++ b/src/main/kotlin/org/hiero/gradle/spotless/LicenseHeader.kt @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: Apache-2.0 +package org.hiero.gradle.spotless + +import org.gradle.api.Project + +object LicenseHeader { + const val default = "SPDX-License-Identifier: Apache-2.0" + + fun javaFormat(project: Project): String { + val plainHeader = plainHeader(project).lines() + return if (plainHeader.size == 1) "// " + plainHeader.single() + else + (listOf("/*") + + plainHeader.map { line -> + when (line) { + "" -> " *" + else -> " * $line" + } + } + + listOf(" */", "", "")) + .joinToString("\n") + } + + fun yamlFormat(project: Project): String { + val plainHeader = plainHeader(project).lines() + return if (plainHeader.size == 1) "# " + plainHeader.single() + else + (listOf("##") + + plainHeader.map { line -> + when (line) { + "" -> "#" + else -> "# $line" + } + } + + listOf("##", "", "")) + .joinToString("\n") + } + + private fun plainHeader(project: Project): String { + @Suppress("UnstableApiUsage") val rootDir = project.isolated.rootProject.projectDirectory + val headerFile = rootDir.file("gradle/license-header.txt").asFile + val header = if (headerFile.exists()) headerFile.readText() else default + return header + } +}