Skip to content
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: 25 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
`kotlin-dsl`
}

val versionCatalog = versionCatalogs.named("libs")

fun plugin(alias: String): String {
val p = versionCatalog.findPlugin(alias).get().get()
return "${p.pluginId}:${p.pluginId}.gradle.plugin:${p.version}"
}

dependencies {
implementation(plugin("spotless"))
implementation(plugin("spotless-changelog"))
implementation("com.diffplug.spotless-changelog:spotless-changelog-lib:3.1.2")
implementation(plugin("spotbugs"))
implementation(plugin("errorprone"))
implementation(plugin("rewrite"))
implementation(plugin("test-logger"))
implementation(plugin("nexus-publish"))
implementation(plugin("develocity"))

// TODO: https://github.com/gradle/gradle/issues/15383
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}
13 changes: 13 additions & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dependencyResolutionManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}

rootProject.name = "build-logic"
35 changes: 35 additions & 0 deletions build-logic/src/main/kotlin/Ext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import com.diffplug.spotless.changelog.gradle.ChangelogExtension
import java.util.Base64
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByType

internal val Project.libs: LibrariesForLibs
get() = extensions.findByType() ?: rootProject.extensions.getByType()

val Project.spotlessChangelog: ChangelogExtension
get() = extensions.findByType() ?: rootProject.extensions.getByType()

val Project.rootSpotlessChangelog: ChangelogExtension
get() = rootProject.extensions.getByType()

fun Project.requiredProperty(name: String): String =
findProperty(name)?.toString()
?: error("$path is missing the '$name' property, which is required")

// getMimeDecoder, not getDecoder: the basic decoder rejects the newlines that `base64`/`openssl
// base64` leave in a wrapped secret, whereas Groovy's String.decodeBase64 (used before the Kotlin
// DSL migration) skipped whitespace.
fun decode64(varName: String): String {
val envValue = System.getenv(varName) ?: return ""
return String(Base64.getMimeDecoder().decode(envValue), Charsets.UTF_8)
}

// Groovy's String.toBoolean() -- what this used before the Kotlin DSL migration -- accepts "true",
// "y" and "1", ignoring case. Kotlin's toBoolean() only accepts "true", so the short forms would
// silently become no-ops.
fun String.toBooleanGroovy(): Boolean = trim().lowercase() in setOf("true", "y", "1")

val isCiServer: Boolean
get() = System.getenv().containsKey("CI")
59 changes: 59 additions & 0 deletions build-logic/src/main/kotlin/spotless.changelog.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id("com.diffplug.spotless-changelog")
}

val (kind, releaseTitle) =
when (project.name) {
"plugin-gradle" -> "gradle" to "Gradle Plugin"
"plugin-maven" -> "maven" to "Maven Plugin"
else -> {
check(project == rootProject)
"lib" to "Lib"
}
}

// the root project and plugins have their own changelogs
spotlessChangelog {
changelogFile("CHANGES.md")
// need -Prelease=true in order to do a publish
setAppendDashSnapshotUnless_dashPrelease(true)
branch("release")
tagPrefix("$kind/")
commitMessage("Published $kind/{{version}}") // {{version}} will be replaced
tagMessage("{{changes}}")
runAfterPush(
"gh release create $kind/{{version}} --title '$releaseTitle v{{version}}' --notes-from-tag"
)
}

if (project == rootProject) {
gradle.taskGraph.whenReady {
val changelogPushTasks = allTasks.filter { it.name == "changelogPush" }.map { it.path }
if (changelogPushTasks.size > 1) {
// make sure only one changelog gets published per tag/commit
throw IllegalArgumentException(
"Run changelogPush one at a time:\n" + changelogPushTasks.joinToString("\n")
)
}
if (changelogPushTasks.size == 1) {
// if the one thing being published is a plugin, make sure there aren't any unreleased
// changes in lib
val isPlugin =
changelogPushTasks[0] == ":plugin-gradle:changelogPush" ||
changelogPushTasks[0] == ":plugin-maven:changelogPush"
if (
isPlugin &&
!rootSpotlessChangelog.parsedChangelog.noUnreleasedChanges() &&
rootProject.findProperty("ignoreUnreleasedLib")?.toString() != "true"
) {
throw IllegalArgumentException(
"You're going to publish ${changelogPushTasks[0]}, but there are unreleased features in lib!\n" +
"You should run :changelogPush first! Else you'll be missing out on:\n" +
"${rootSpotlessChangelog.parsedChangelog.unreleasedChanges()}\n" +
"If it's okay to miss those and link against the old ${rootSpotlessChangelog.versionLast} then " +
"add -PignoreUnreleasedLib=true"
)
}
}
}
}
38 changes: 38 additions & 0 deletions build-logic/src/main/kotlin/spotless.error-prone.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import net.ltgt.gradle.errorprone.errorprone

plugins {
id("net.ltgt.errorprone")
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone {
enabled = System.getenv("error-prone")?.toBooleanGroovy() == true
// https://github.com/diffplug/spotless/issues/2745
// https://github.com/google/error-prone/issues/5365
disableAllWarnings = true
disable(
"AnnotateFormatMethod", // We don't want to use ErrorProne's annotations.
"FunctionalInterfaceMethodChanged",
"ImmutableEnumChecker", // We don't want to use ErrorProne's annotations.
"InlineMeSuggester", // We don't want to use ErrorProne's annotations.
"JavaxInjectOnAbstractMethod",
"OverridesJavaxInjectableMethod",
"ReturnValueIgnored", // We don't want to use ErrorProne's annotations.
)
error(
"ReturnValueIgnored",
"SelfAssignment",
"StringJoin",
"UnnecessarilyFullyQualified",
"UnnecessaryLambda",
)
// bug: this only happens when the file is dirty.
// might be an up2date (caching) issue, as file is currently in corrupt state.
// ForbidGradleInternal(import
excludedPaths = ".*/GradleIntegrationHarness.java"
}
}

dependencies {
errorprone(libs.errorprone.core)
}
181 changes: 181 additions & 0 deletions build-logic/src/main/kotlin/spotless.java-publish.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
plugins {
`java-library`
`maven-publish`
signing
}

java {
withJavadocJar()
withSourcesJar()
}

tasks.named("sourcesJar") {
dependsOn(tasks.jar)
}

tasks.withType<Javadoc>().configureEach {
options {
(this as? StandardJavadocDocletOptions)?.apply {
encoding = Charsets.UTF_8.name()
// Where it's possible to name parameters and methods clearly enough that javadoc is not
// necessary, why make the code bigger? Thus, no javadoc warnings.
addStringOption("Xdoclint:none", "-quiet")
addStringOption("Xwerror", "-quiet")
addStringOption("source", "17")
val artifactId = requiredProperty("artifactId")
val org = requiredProperty("org")
val version = project.version.toString()
val group = project.group.toString()
val javadocInfo =
"<h2><a href=\"https://github.com/$org/${rootProject.name}\" style=\"text-transform: none;\">$group:$artifactId:$version</a> by <a href=\"https://www.diffplug.com\" style=\"text-transform: none;\">DiffPlug</a></h2>"
header = javadocInfo

val dotdotGradle = if (project.name.startsWith("eclipse-")) "../../gradle" else "../gradle"
linksOffline("https://docs.gradle.org/6.1.1/javadoc/", "$dotdotGradle/javadoc/gradle")
val versionLast = rootSpotlessChangelog.versionLast
linksOffline(
"https://javadoc.io/static/com.diffplug.spotless/spotless-lib/$versionLast",
"$dotdotGradle/javadoc/spotless-lib",
)
linksOffline(
"https://javadoc.io/static/com.diffplug.spotless/spotless-lib-extra/$versionLast",
"$dotdotGradle/javadoc/spotless-lib-extra",
)
}
}
}

// make sure bad javadoc breaks the build
tasks.check {
dependsOn(tasks.javadoc)
}

afterEvaluate {
val isExt = project.name.startsWith("eclipse-")
val artifactId = requiredProperty("artifactId")
val org = requiredProperty("org")
val isPluginMaven = artifactId == "spotless-maven-plugin"
val isPluginGradle = pluginManager.hasPlugin("java-gradle-plugin")

publishing {
publications {
// java-gradle-plugin creates 'pluginMaven' from its own afterEvaluate, with the java
// component already attached. Ours has to run after that one, which it only does because
// java-gradle-plugin is applied earlier in the consumer's plugins block. If that ever
// inverts we would create an empty publication here and ship a POM with no jar, so check
// rather than silently degrade.
val existing = findByName("pluginMaven") as? MavenPublication
check(!isPluginGradle || existing != null) {
"${project.path} applies java-gradle-plugin, which must be applied before " +
"spotless.java-publish so that it can create the 'pluginMaven' publication"
}
val pluginMaven =
existing ?: create<MavenPublication>("pluginMaven") { from(components["java"]) }

pluginMaven.apply {
groupId = project.group.toString()
this.artifactId = artifactId
version = project.version.toString()

pom {
name = artifactId
description = project.description
url = "https://github.com/$org/${rootProject.name}"
scm {
url = "https://github.com/$org/${rootProject.name}"
connection = "scm:git:https://github.com/$org/${rootProject.name}.git"
developerConnection = "scm:git:ssh:git@github.com/$org/${rootProject.name}.git"
}
licenses {
license {
if (isExt) {
name = "Eclipse Public License - v 1.0"
url = "https://www.eclipse.org/legal/epl-v10.html"
} else {
name = "The Apache Software License, Version 2.0"
url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
}
distribution = "repo"
}
}
developers {
if (isExt) {
@Suppress("UNCHECKED_CAST")
val devs = project.findProperty("developers") as? Map<String, Map<String, String>>
devs?.forEach { (extId, extValues) ->
developer {
id = extId
name = extValues["name"]
email = extValues["email"]
}
}
} else {
if (isPluginMaven) {
developer {
id = "lutovich"
name = "Konstantin Lutovich"
email = "konstantin.lutovich@neotechnology.com"
}
}
developer {
id = "nedtwigg"
name = "Ned Twigg"
email = "ned.twigg@diffplug.com"
}
}
}
if (isPluginMaven) {
// Maven plugin requires Maven 3.1.0+ to run
withXml {
val rootNode = asNode()
val prerequisites = rootNode.appendNode("prerequisites")
prerequisites.appendNode("maven", "3.1.0")
}
}
}
}
}
if (System.getenv("JITPACK") == "true" || project.version.toString().endsWith("-SNAPSHOT")) {
signing {
isRequired = false
}
} else {
signing {
val gpgKey = decode64("ORG_GRADLE_PROJECT_gpg_key64")
useInMemoryPgpKeys(
"0x4272C851",
gpgKey,
System.getenv("ORG_GRADLE_PROJECT_gpg_passphrase"),
)
sign(publishing.publications)
}

// find the project with the changelog (this project for plugins, root project for libs)
val changelogProject = if (tasks.names.contains("changelogBump")) project else rootProject
val changelogTasks = changelogProject.tasks

// ensures that nothing will be built if changelogPush will end up failing
tasks.jar {
dependsOn(changelogTasks.named("changelogCheck"))
}

// ensures that changelog bump and push only happens if the publish was successful
val thisProj = project
changelogTasks.named("changelogBump") {
dependsOn(
":${thisProj.path.removePrefix(":")}:publishPluginMavenPublicationToSonatypeRepository"
)
dependsOn(":closeAndReleaseSonatypeStagingRepository")
// if we have a Gradle plugin, we need to push it up to the plugin portal too
if (thisProj.tasks.names.contains("publishPlugins")) {
dependsOn(thisProj.tasks.named("publishPlugins"))
}
}
}
}
}

tasks.withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}
Loading
Loading