Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add license header to 'module-info' and 'package-info' #71

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/main/kotlin/org.hiero.gradle.check.spotless-java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ spotless {

// fix errors due to dashed comment blocks (eg: /*-, /*--, etc)
addStep(RepairDashedCommentsFormatterStep.create())
// Sort the 'requires' entries in Module Info files
addStep(SortModuleInfoRequiresStep.create())
// enable toggle comment support
toggleOffOn()
// don't need to set target, it is inferred from java
// apply a specific flavor of google-java-format
// apply flavor of google-java-format
palantirJavaFormat()
// make sure every file has the following copyright header.
// optionally, Spotless can set copyright years by digging
// 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(LicenseHeader.javaFormat(project), "(package|import|module)")

licenseHeader(LicenseHeader.javaFormat(project), "(package|import)")
.updateYearWithLatest(true)
}
format("javaInfoFiles") {
// separate extension due to https://github.com/diffplug/spotless/issues/532
target("src/**/module-info.java", "src/**/package-info.java")

// sort the 'requires' entries in 'module-info' files
addStep(SortModuleInfoRequiresStep.create())

licenseHeader(LicenseHeader.javaFormat(project), "(package|import|module|@|/\\*\\*)")
.updateYearWithLatest(true)
}
}

tasks.named("spotlessJavaInfoFiles") { dependsOn(tasks.named("spotlessJava")) }
4 changes: 2 additions & 2 deletions src/main/kotlin/org/hiero/gradle/spotless/LicenseHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package org.hiero.gradle.spotless
import org.gradle.api.Project

object LicenseHeader {
const val default = "SPDX-License-Identifier: Apache-2.0"
private const val DEFAULT = "SPDX-License-Identifier: Apache-2.0"

fun javaFormat(project: Project): String {
val plainHeader = plainHeader(project).lines()
Expand Down Expand Up @@ -39,7 +39,7 @@ object LicenseHeader {
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
val header = if (headerFile.exists()) headerFile.readText() else DEFAULT
return header
}
}
99 changes: 99 additions & 0 deletions src/test/kotlin/org/hiero/gradle/test/QualityGateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,103 @@

assertThat(result.task(":qualityGate")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun `qualityGate fully formats module-info and package-info`() {

Check warning on line 51 in src/test/kotlin/org/hiero/gradle/test/QualityGateTest.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/kotlin/org/hiero/gradle/test/QualityGateTest.kt#L51

The function qualityGate fully formats module-info and package-info is missing documentation.

Check warning on line 51 in src/test/kotlin/org/hiero/gradle/test/QualityGateTest.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/kotlin/org/hiero/gradle/test/QualityGateTest.kt#L51

The function qualityGate fully formats module-info and package-info is too long (93). The maximum length is 60.
val p = GradleProject().withMinimalStructure()
p.moduleBuildFile("""plugins { id("org.hiero.gradle.module.library") }""")
p.dependencyVersionsFile(
"""
plugins {
id("org.hiero.gradle.base.lifecycle")
id("org.hiero.gradle.base.jpms-modules")
}
dependencies.constraints {
api("com.fasterxml.jackson.core:jackson-databind:2.16.0") { because("com.fasterxml.jackson.databind") }
api("org.apache.commons:commons-lang3:3.14.0") { because("org.apache.commons.lang3") }
}"""
.trimIndent()
)
p.javaSourceFile(
"""
package org.hiero.product.module.a;
public class ModuleA {
private com.fasterxml.jackson.databind.ObjectMapper om;
private org.apache.commons.lang3.CharUtils cu;
}"""
.trimIndent()
)

val moduleInfo =
p.file(
"product/module-a/src/main/java/module-info.java",
"""
module org.hiero.product.module.a {
requires org.apache.commons.lang3;
requires com.fasterxml.jackson.databind;

exports org.hiero.product.module.a;
} """
.trimIndent()
)
val packageInfoA =
p.file(
"product/module-a/src/main/java/org/hiero/product/module/a/package-info.java",
"package org.hiero.product.module.a; "
)
val packageInfoB =
p.file(
"product/module-a/src/main/java/org/hiero/product/module/b/package-info.java",
"/** some comment */ package org.hiero.product.module.b; "
)
val packageInfoC =
p.file(
"product/module-a/src/main/java/org/hiero/product/module/c/package-info.java",
"@Deprecated package org.hiero.product.module.c; "
)

val result = p.qualityGate()

assertThat(moduleInfo)
.hasContent(
"""
// SPDX-License-Identifier: Apache-2.0
module org.hiero.product.module.a {
requires com.fasterxml.jackson.databind;
requires org.apache.commons.lang3;

exports org.hiero.product.module.a;
}
"""
.trimIndent()
)
assertThat(packageInfoA)
.hasContent(
"""
// SPDX-License-Identifier: Apache-2.0
package org.hiero.product.module.a;
"""
.trimIndent()
)
assertThat(packageInfoB)
.hasContent(
"""
// SPDX-License-Identifier: Apache-2.0
/** some comment */
package org.hiero.product.module.b;
"""
.trimIndent()
)
assertThat(packageInfoC)
.hasContent(
"""
// SPDX-License-Identifier: Apache-2.0
@Deprecated
package org.hiero.product.module.c;
"""
.trimIndent()
)

assertThat(result.task(":qualityGate")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}