Skip to content

Commit

Permalink
Critical updates to make gradle plugin work with releases
Browse files Browse the repository at this point in the history
### What's done:
- kotlinx.serialization is updated to 1.5.0
- kotlin is updated to 1.8.21
- release script is converted into the plugin (as suggested in https://kotlinlang.org/docs/multiplatform-library.html#set-up-the-environment). This is needed because gradle became too strict and needs an explicit dependency on signing task now
  • Loading branch information
orchestr7 committed May 10, 2023
1 parent 5d109bb commit db04993
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 157 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ allprojects {
repositories {
mavenCentral()
}

configureDiktat()
configureDetekt()

Expand All @@ -22,4 +23,3 @@ allprojects {
createDetektTask()
installGitHooks()

configurePublishing()
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21")

implementation("org.cqfn.diktat:diktat-gradle-plugin:1.2.5")
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.15.0")
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.22.0")
implementation("io.github.gradle-nexus:publish-plugin:1.1.0")
implementation("org.ajoberstar.reckon:reckon-gradle:0.13.0")
implementation("org.ajoberstar.grgit:grgit-core:4.1.0")
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/com/akuleshov7/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"PACKAGE_NAME_INCORRECT_PATH")

object Versions {
const val KOTLIN = "1.8.0"
const val KOTLIN = "1.8.21"
const val JUNIT = "5.7.1"
const val OKIO = "3.1.0"
const val SERIALIZATION = "1.4.1"
const val SERIALIZATION = "1.5.0"
}

This file was deleted.

14 changes: 14 additions & 0 deletions gradle/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation("io.github.gradle-nexus:publish-plugin:1.1.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* Publishing configuration file.
*/

@file:Suppress(
"MISSING_KDOC_TOP_LEVEL",
"MISSING_KDOC_ON_FUNCTION",
)

package com.akuleshov7.buildutils

import io.github.gradlenexus.publishplugin.NexusPublishExtension
import org.gradle.api.Named
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.bundling.Jar
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutput.Style.Failure
import org.gradle.internal.logging.text.StyledTextOutput.Style.Success
import org.gradle.internal.logging.text.StyledTextOutputFactory
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.support.serviceOf
import org.gradle.kotlin.dsl.withType
import org.gradle.plugins.signing.SigningExtension

/**
* Enables signing of the artifacts if the `signingKey` project property is set.
*
* Should be explicitly called after each custom `publishing {}` section.
*/
fun Project.configureSigning() {
if (hasProperty("signingKey")) {
/*
* GitHub Actions.
*/
configureSigningCommon {
useInMemoryPgpKeys(property("signingKey") as String?, findProperty("signingPassword") as String?)
}
} else if (
hasProperties(
"signing.keyId",
"signing.password",
"signing.secretKeyRingFile",
)
) {
/*-
* Pure-Java signing mechanism via `org.bouncycastle.bcpg`.
*
* Requires an 8-digit (short form) PGP key id and a present `~/.gnupg/secring.gpg`
* (for gpg 2.1, run
* `gpg --keyring secring.gpg --export-secret-keys >~/.gnupg/secring.gpg`
* to generate one).
*/
configureSigningCommon()
} else if (hasProperty("signing.gnupg.keyName")) {
/*-
* Use an external `gpg` executable.
*
* On Windows, you may need to additionally specify the path to `gpg` via
* `signing.gnupg.executable`.
*/
configureSigningCommon {
useGpgCmd()
}
}
}

@Suppress("TOO_LONG_FUNCTION")
internal fun Project.configurePublications() {
val dokkaJar: Jar = tasks.create<Jar>("dokkaJar") {
group = "documentation"
archiveClassifier.set("javadoc")
from(tasks.findByName("dokkaHtml"))
}
configure<PublishingExtension> {
repositories {
mavenLocal()
}
publications.withType<MavenPublication>().forEach { publication ->
publication.artifact(dokkaJar)
publication.pom {
name.set(project.name)
description.set(project.description ?: project.name)
url.set("https://github.com/akuleshov7/ktoml")
licenses {
license {
name.set("MIT License")
url.set("https://github.com/akuleshov7/ktoml/blob/main/LICENSE")
distribution.set("repo")
}
}
developers {
developer {
id.set("akuleshov7")
name.set("Andrey Kuleshov")
email.set("andrewkuleshov7@gmail.com")
}
}
scm {
url.set("https://github.com/akuleshov7/ktoml")
connection.set("scm:git:git://github.com/akuleshov7/ktoml.git")
}
}
}
}
}

internal fun Project.configureNexusPublishing() {
configure<NexusPublishExtension> {
repositories {
sonatype {
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
username.set(property("sonatypeUsername") as String)
password.set(property("sonatypePassword") as String)
}
}
}
}

/**
* @param useKeys the block which configures the PGP keys. Use either
* [SigningExtension.useInMemoryPgpKeys], [SigningExtension.useGpgCmd], or an
* empty lambda.
* @see SigningExtension.useInMemoryPgpKeys
* @see SigningExtension.useGpgCmd
*/
private fun Project.configureSigningCommon(useKeys: SigningExtension.() -> Unit = {}) {
configure<SigningExtension> {
useKeys()
val publications = extensions.getByType<PublishingExtension>().publications
val publicationCount = publications.size
val message = "The following $publicationCount publication(s) are getting signed: ${publications.map(Named::getName)}"
val style = when (publicationCount) {
0 -> Failure
else -> Success
}
styledOut(logCategory = "signing").style(style).println(message)
sign(*publications.toTypedArray())
}
}

private fun Project.styledOut(logCategory: String): StyledTextOutput =
serviceOf<StyledTextOutputFactory>().create(logCategory)

/**
* Determines if this project has all the given properties.
*
* @param propertyNames the names of the properties to locate.
* @return `true` if this project has all the given properties, `false` otherwise.
* @see Project.hasProperty
*/
private fun Project.hasProperties(vararg propertyNames: String): Boolean =
propertyNames.asSequence().all(this::hasProperty)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.akuleshov7.buildutils

import io.github.gradlenexus.publishplugin.NexusPublishPlugin
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.extra

plugins {
`maven-publish`
signing
}

run {
// If present, set properties from env variables. If any are absent, release will fail.
System.getenv("OSSRH_USERNAME")?.let {
extra.set("sonatypeUsername", it)
}
System.getenv("OSSRH_PASSWORD")?.let {
extra.set("sonatypePassword", it)
}
System.getenv("GPG_SEC")?.let {
extra.set("signingKey", it)
}
System.getenv("GPG_PASSWORD")?.let {
extra.set("signingPassword", it)
}

if (project.path == rootProject.path) {
apply<NexusPublishPlugin>()
if (hasProperty("sonatypeUsername")) {
configureNexusPublishing()
}
}
}

run {
configurePublications()
}
Loading

0 comments on commit db04993

Please sign in to comment.