<commit_before>package buildsrc.convention

import buildsrc.config.createMockKPom
import buildsrc.config.credentialsAction


plugins {
    `maven-publish`
    signing
}

val sonatypeRepositoryCredentials: Provider<Action<PasswordCredentials>> =
    providers.credentialsAction("ossrh")

val sonatypeRepositoryReleaseUrl: Provider<String> = provider {
    if (version.toString().endsWith("SNAPSHOT")) {
        "https://oss.sonatype.org/content/repositories/snapshots/"
    } else {
        "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
    }
}

val signingKeyId: Provider<String> =
    providers.gradleProperty("signing.keyId")
val signingKey: Provider<String> =
    providers.gradleProperty("signing.key")
val signingPassword: Provider<String> =
    providers.gradleProperty("signing.password")
val signingSecretKeyRingFile: Provider<String> =
    providers.gradleProperty("signing.secretKeyRingFile")
val ossrhUsername: Provider<String> =
    providers.gradleProperty("ossrhUsername")
val ossrhPassword: Provider<String> =
    providers.gradleProperty("ossrhPassword")


tasks.withType<AbstractPublishToMaven>().configureEach {
    // Gradle warns about some signing tasks using publishing task outputs without explicit
    // dependencies. Here's a quick fix.
    dependsOn(tasks.withType<Sign>())
    mustRunAfter(tasks.withType<Sign>())

    doLast {
        logger.lifecycle("[task: ${name}] ${publication.groupId}:${publication.artifactId}:${publication.version}")
    }
}

val mavenName: String by project.extra
val mavenDescription: String by project.extra
val localrepo: String by project

publishing {
    repositories {
        // publish to local dir, for testing
        maven(rootProject.layout.projectDirectory.dir(localrepo)) {
            name = "LocalRepo"
        }

        /*maven {
            url = uri(sonatypeRepositoryReleaseUrl)
            credentials {
                username = ossrhUsername.get()
                password = <PASSWORD>()
            }
        }*/
    }
    publications.withType<MavenPublication>().configureEach {
        createMockKPom {
            name.set(provider { mavenName })
            description.set(provider { mavenDescription })
        }

        artifact(tasks.provider<Jar>("javadocJar"))

        signing.sign(this)
    }
}

signing {
    if (signingKeyId.isPresent() && signingKey.isPresent() && signingPassword.isPresent()) {
        logger.debug("[${project.displayName}] Signing is enabled")
        useInMemoryPgpKeys(signingKeyId.get(), signingKey.get(), signingPassword.get())
    }<commit_msg>only sign if the signing properties are present<commit_after>package buildsrc.convention

import buildsrc.config.createMockKPom
import buildsrc.config.credentialsAction


plugins {
    `maven-publish`
    signing
}

val sonatypeRepositoryCredentials: Provider<Action<PasswordCredentials>> =
    providers.credentialsAction("ossrh")

val sonatypeRepositoryReleaseUrl: Provider<String> = provider {
    if (version.toString().endsWith("SNAPSHOT")) {
        "https://oss.sonatype.org/content/repositories/snapshots/"
    } else {
        "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
    }
}

val signingKeyId: Provider<String> =
    providers.gradleProperty("signing.keyId")
val signingKey: Provider<String> =
    providers.gradleProperty("signing.key")
val signingPassword: Provider<String> =
    providers.gradleProperty("signing.password")
val signingSecretKeyRingFile: Provider<String> =
    providers.gradleProperty("signing.secretKeyRingFile")
val ossrhUsername: Provider<String> =
    providers.gradleProperty("ossrhUsername")
val ossrhPassword: Provider<String> =
    providers.gradleProperty("ossrhPassword")


tasks.withType<AbstractPublishToMaven>().configureEach {
    // Gradle warns about some signing tasks using publishing task outputs without explicit
    // dependencies. Here's a quick fix.
    dependsOn(tasks.withType<Sign>())
    mustRunAfter(tasks.withType<Sign>())

    doLast {
        logger.lifecycle("[task: ${name}] ${publication.groupId}:${publication.artifactId}:${publication.version}")
    }
}

val mavenName: String by project.extra
val mavenDescription: String by project.extra
val localrepo: String by project

publishing {
    repositories {
        // publish to local dir, for testing
        maven(rootProject.layout.projectDirectory.dir(localrepo)) {
            name = "LocalRepo"
        }

        /*maven {
            url = uri(sonatypeRepositoryReleaseUrl)
            credentials {
                username = ossrhUsername.get()
                password = <PASSWORD>()
            }
        }*/
    }
    publications.withType<MavenPublication>().configureEach {
        createMockKPom {
            name.set(provider { mavenName })
            description.set(provider { mavenDescription })
        }

        artifact(tasks.provider<Jar>("javadocJar"))

        if (signingKeyId.isPresent() && signingKey.isPresent() && signingPassword.isPresent()) {
            signing.sign(this)
        }
    }
}

signing {
    if (signingKeyId.isPresent() && signingKey.isPresent() && signingPassword.isPresent()) {
        logger.debug("[${project.displayName}] Signing is enabled")
        useInMemoryPgpKeys(signingKeyId.get(), signingKey.get(), signingPassword.get())
    }
{'fraction_non_alphanumeric': 0.11599924797894341, 'fraction_numerical': 0.00037601052829479227, 'mean_word_length': 3.4743481917577794, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6649112', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1486, 'n_words': 259}