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

Create plugin for Scala publishing conventions #169

Merged
merged 5 commits into from
Jun 19, 2023
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
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ plugins {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
22 changes: 22 additions & 0 deletions buildSrc/src/main/kotlin/Predef.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.gradle.api.Project

internal fun Project.configValue(propertyName: String, environmentVariableName: String): String? {
val property: String? = project.properties[propertyName]?.toString()
val environmentVariable: String? = System.getenv(environmentVariableName)

val configValue = property ?: environmentVariable

return configValue.also {
if (configValue.isNullOrBlank()) {
errorMessage(
"$propertyName Gradle property and " +
"$environmentVariableName environment variable are missing",
)
}
}
}

internal fun Project.errorMessage(message: String) = logger.lifecycle("$YELLOW$message$RESET")

private const val RESET = "\u001B[0m"
private const val YELLOW = "\u001B[0;33m"
15 changes: 0 additions & 15 deletions buildSrc/src/main/kotlin/ScalaDocumentationPlugin.kt

This file was deleted.

86 changes: 86 additions & 0 deletions buildSrc/src/main/kotlin/ScalaPublishingConventionsPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.BasePluginExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.register
import org.gradle.plugins.signing.SigningExtension

class ScalaPublishingConventionsPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run {
val scaladocJarTask: TaskProvider<Jar> = tasks.register<Jar>("scaladocJar") {
group = BasePlugin.BUILD_GROUP
tasks.findByName("scaladoc")?.let { dependsOn(it) }
franciscodr marked this conversation as resolved.
Show resolved Hide resolved
?: errorMessage("The scaladoc task was not found. The Javadoc jar file won't contain any documentation")
archiveClassifier.set("javadoc")
from("$buildDir/docs/scaladoc")
}

val publishingExtension: PublishingExtension =
extensions.findByType<PublishingExtension>()
?: throw IllegalStateException("The Maven Publish plugin is required to publish the build artifacts")

val signingExtension: SigningExtension =
extensions.findByType<SigningExtension>()
?: throw IllegalStateException("The Signing plugin is required to digitally sign the built artifacts")

val basePluginExtension: BasePluginExtension =
extensions.findByType<BasePluginExtension>()
?: throw IllegalStateException("The Base plugin is required to configure the name of artifacts")

publishingExtension.run {
publications {
register<MavenPublication>("maven") {
val scala3Suffix = "_3"

artifactId = basePluginExtension.archivesName.get() + scala3Suffix
from(components["java"])
artifact(scaladocJarTask)

pom {
name.set(project.properties["pom.name"]?.toString())
description.set(project.properties["pom.description"]?.toString())
url.set(project.properties["pom.url"]?.toString())

licenses {
license {
name.set(project.properties["pom.license.name"]?.toString())
url.set(project.properties["pom.license.url"]?.toString())
}
}

developers {
developer {
id.set(project.properties["pom.developer.id"].toString())
name.set(project.properties["pom.developer.name"].toString())
}
}

scm {
url.set(project.properties["pom.smc.url"].toString())
connection.set(project.properties["pom.smc.connection"].toString())
developerConnection.set(project.properties["pom.smc.developerConnection"].toString())
}
}
}
}
}

signingExtension.run {
val isLocal = gradle.startParameter.taskNames.any { it.contains("publishToMavenLocal", ignoreCase = true) }
val signingKeyId: String? = configValue("signing.keyId", "SIGNING_KEY_ID")
val signingKey: String? = configValue("signing.key", "SIGNING_KEY")
val signingPassphrase: String? = configValue("signing.passphrase", "SIGNING_KEY_PASSPHRASE")

isRequired = !isLocal
useGpgCmd()
useInMemoryPgpKeys(signingKeyId, signingKey, signingPassphrase)
sign(publishingExtension.publications)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apply<ScalaPublishingConventionsPlugin>()
58 changes: 3 additions & 55 deletions scala-cats/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
signing
alias(libs.plugins.semver.gradle)
alias(libs.plugins.spotless)
`xef-scala-documentation`
`xef-scala-publishing-conventions`
}

dependencies {
Expand All @@ -18,10 +18,8 @@ dependencies {
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
toolchain {
languageVersion = JavaLanguageVersion.of(11)
languageVersion.set(JavaLanguageVersion.of(11))
}
withSourcesJar()
}
Expand All @@ -34,58 +32,8 @@ tasks.withType<ScalaCompile> {
scalaCompileOptions.additionalParameters = listOf("-Wunused:all", "-Wvalue-discard")
}

publishing {
publications {
register<MavenPublication>("maven") {
val scala3Suffix = "_3"
from(components["java"])
artifact(tasks.named("scaladocJar"))

artifactId = base.archivesName.get() + scala3Suffix

pom {
name.set(project.properties["pom.name"]?.toString())
description.set(project.properties["pom.description"]?.toString())
url.set(project.properties["pom.url"]?.toString())

licenses {
license {
name.set(project.properties["pom.license.name"]?.toString())
url.set(project.properties["pom.license.url"]?.toString())
}
}

developers {
developer {
id.set(project.properties["pom.developer.id"].toString())
name.set(project.properties["pom.developer.name"].toString())
}
}

scm {
url.set(project.properties["pom.smc.url"].toString())
connection.set(project.properties["pom.smc.connection"].toString())
developerConnection.set(project.properties["pom.smc.developerConnection"].toString())
}
}
}
}
}

signing {
val isLocal = gradle.startParameter.taskNames.any { it.contains("publishToMavenLocal", ignoreCase = true) }
val signingKeyId: String? = System.getenv("SIGNING_KEY_ID")
val signingKey: String? = System.getenv("SIGNING_KEY")
val signingPassphrase: String? = System.getenv("SIGNING_KEY_PASSPHRASE")

isRequired = !isLocal
useGpgCmd()
useInMemoryPgpKeys(signingKeyId, signingKey, signingPassphrase)
sign(publishing.publications)
}

spotless {
scala {
scalafmt("3.7.3").configFile(".scalafmt.conf").scalaMajorVersion("2.13")
}
}
}
54 changes: 2 additions & 52 deletions scala/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
signing
alias(libs.plugins.semver.gradle)
alias(libs.plugins.spotless)
`xef-scala-documentation`
`xef-scala-publishing-conventions`
}

dependencies {
Expand Down Expand Up @@ -40,58 +40,8 @@ tasks.withType<ScalaCompile> {
scalaCompileOptions.additionalParameters = listOf("-Wunused:all", "-Wvalue-discard")
}

publishing {
publications {
register<MavenPublication>("maven") {
val scala3Suffix = "_3"
from(components["java"])
artifact(tasks.named("scaladocJar"))

artifactId = base.archivesName.get() + scala3Suffix

pom {
name.set(project.properties["pom.name"]?.toString())
description.set(project.properties["pom.description"]?.toString())
url.set(project.properties["pom.url"]?.toString())

licenses {
license {
name.set(project.properties["pom.license.name"]?.toString())
url.set(project.properties["pom.license.url"]?.toString())
}
}

developers {
developer {
id.set(project.properties["pom.developer.id"].toString())
name.set(project.properties["pom.developer.name"].toString())
}
}

scm {
url.set(project.properties["pom.smc.url"].toString())
connection.set(project.properties["pom.smc.connection"].toString())
developerConnection.set(project.properties["pom.smc.developerConnection"].toString())
}
}
}
}
}

signing {
val isLocal = gradle.startParameter.taskNames.any { it.contains("publishToMavenLocal", ignoreCase = true) }
val signingKeyId: String? = System.getenv("SIGNING_KEY_ID")
val signingKey: String? = System.getenv("SIGNING_KEY")
val signingPassphrase: String? = System.getenv("SIGNING_KEY_PASSPHRASE")

isRequired = !isLocal
useGpgCmd()
useInMemoryPgpKeys(signingKeyId, signingKey, signingPassphrase)
sign(publishing.publications)
}

spotless {
scala {
scalafmt("3.7.3").configFile(".scalafmt.conf").scalaMajorVersion("2.13")
}
}
}