Skip to content

Commit

Permalink
build: make license header configurable via 'license-header.txt' (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: Jendrik Johannes <[email protected]>
  • Loading branch information
jjohannes authored Dec 19, 2024
1 parent d5f774c commit 34ed2ba
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
import org.hiero.gradle.spotless.LicenseHeader

plugins { id("com.diffplug.spotless") }

spotless {
kotlinGradle {
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
import org.hiero.gradle.spotless.LicenseHeader

plugins { id("com.diffplug.spotless") }

spotless {
Expand All @@ -18,6 +20,6 @@ spotless {
indentWithSpaces()
endWithNewline()

licenseHeader("# SPDX-License-Identifier: Apache-2.0\n", "(name)")
licenseHeader(LicenseHeader.yamlFormat(project), "(name)").updateYearWithLatest(true)
}
}
45 changes: 45 additions & 0 deletions src/main/kotlin/org/hiero/gradle/spotless/LicenseHeader.kt
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 34ed2ba

Please sign in to comment.