<commit_before>package nebula.plugin.publishing.sign

import nebula.core.AlternativeArchiveTask
import nebula.plugin.publishing.component.CustomComponentPlugin
import nebula.plugin.publishing.maven.NebulaBaseMavenPublishingPlugin
import org.apache.commons.lang3.text.WordUtils
import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.maven.MavenArtifact
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.tasks.GenerateMavenPom
import org.gradle.plugins.signing.Sign
import org.gradle.plugins.signing.Signature
import org.gradle.plugins.signing.SigningPlugin

/**
 * From: http://mike-neck.github.io/blog/2013/06/21/how-to-publish-artifacts-with-gradle-maven-publish-plugin-version-1-dot-6/
 *
 * Mike perfectly captured the non-intuitiveness that is needed to use the signing plugin, with the maven-publish plugin.
 * This plugin captures that logic, while adding some static typing and some laziness to the publication bits. Follow the
 * <a href="http://www.gradle.org/docs/current/userguide/signing_plugin.html"></a>
 * <p>
 *     signing.keyId=24875D73
 *     signing.password=<PASSWORD>
 *     signing.secretKeyRingFile=/Users/me/.gnupg/secring.gpg
 * </p>
 *
 * Instructions on how to create a key can be found at https://docs.sonatype.org/display/Repository/How+To+Generate+PGP+Signatures+With+Maven
 */
class NebulaSignPlugin implements Plugin<Project> {

    Sign signJarsTask

    void apply(Project project) {

        // Per http://www.gradle.org/docs/current/userguide/signing_plugin.html, it's well established that
        // signing.keyId, signing.password, signing.secretKeyRingFile need to be provided. While SigningExtension
        // can be configured with other values, it's best to stick with these well known names.

        // Conditionalize the actual signing on having the property settings. Otherwise leave the
        boolean hasSigningProps = project.hasProperty('signing.keyId') &&
                project.hasProperty('signing.password') &&
                project.hasProperty('signing.secretKeyRingFile')

        // adding 'signing' plugin
        project.plugins.apply(SigningPlugin)

        // sign all artifacts
        signJarsTask = project.tasks.create('signJars', Sign)

        // Conditionally sign.
        if(hasSigningProps) {
            // Only try to sign a configuration if we've got keys, otherwise we'll get artifacts into the archives
            // configurations with no bodies.
            signJarsTask.sign(project.configurations.getByName('archives'))
        } else {
            // Otherwise it'll complain that it has no signatories, even though it doesn't need any
            signJarsTask.required = false
        }

        // call signJar task before publish task, aka bintrayUpload
        project.task('preparePublish').dependsOn(signJarsTask)

        project.plugins.withType(NebulaBaseMavenPublishingPlugin) { NebulaBaseMavenPublishingPlugin basePlugin ->
            basePlugin.withMavenPublication { MavenPublication mavenJava ->
                // give signature files to artifact method
                signJarsTask.signatures.all { Signature signature ->
                    mavenJava.artifact(signature, new Action<MavenArtifact>() {
                        @Override
                        void execute(MavenArtifact t) {
                            t.classifier = signature.toSignArtifact.classifier
                            t.extension = signature.getType() // 'jar.asc'
                        }
                    })
                }
            }
        }

        // Support POM
        // Create configuration which pom can be added to, all artifacts will automatically get added to archives, which is already
        // being signed
        project.configurations.create('pom')

        // Immediately create a wrapper task when the generator appears
        project.tasks.matching { it instanceof GenerateMavenPom }.all { GenerateMavenPom pomGenerator ->
            // 'wrapGeneratePomFileForMavenJavaPublication'
            def taskName = "wrap${pomGenerator.name.capitalize()}"

            // Have GenerateMavenPom have a destination that we can sign, with a nice name
            def pubName = (pomGenerator.name =~ /For(.*)Publication/)[0][1]
            pomGenerator.destination = project.file("${project.buildDir}/poms/${WordUtils.uncapitalize(pubName)}.pom")<commit_msg>Additional cases for skipping signing. Still don’t know how to adapt to publishMavenJavaPublicationToMavenRepository tasks, since those are just going to be broken.<commit_after>package nebula.plugin.publishing.sign

import nebula.core.AlternativeArchiveTask
import nebula.plugin.publishing.component.CustomComponentPlugin
import nebula.plugin.publishing.maven.NebulaBaseMavenPublishingPlugin
import org.apache.commons.lang3.text.WordUtils
import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.maven.MavenArtifact
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.tasks.GenerateMavenPom
import org.gradle.plugins.signing.Sign
import org.gradle.plugins.signing.Signature
import org.gradle.plugins.signing.SigningPlugin

/**
 * From: http://mike-neck.github.io/blog/2013/06/21/how-to-publish-artifacts-with-gradle-maven-publish-plugin-version-1-dot-6/
 *
 * Mike perfectly captured the non-intuitiveness that is needed to use the signing plugin, with the maven-publish plugin.
 * This plugin captures that logic, while adding some static typing and some laziness to the publication bits. Follow the
 * <a href="http://www.gradle.org/docs/current/userguide/signing_plugin.html"></a>
 * <p>
 *     signing.keyId=24875D73
 *     signing.password=<PASSWORD>
 *     signing.secretKeyRingFile=/Users/me/.gnupg/secring.gpg
 * </p>
 *
 * Instructions on how to create a key can be found at https://docs.sonatype.org/display/Repository/How+To+Generate+PGP+Signatures+With+Maven
 *
 * Signing can falter in two way: it can fail to create a Signatory because of lack of keys or AbstractAntTaskBackedMavenPublisher.determineMainArtifact
 * can't differentiate the primary .jar, it's signature or the pom file. AbstractAntTaskBackedMavenPublisher is only used in a few situations, but it is
 * not used via the Bintray or Artifactory plugin. Though it is used for "install" which is unavoidable. To avoid these situations, there are three way
 * which skip actual signing:
 *
 * <ul>
 *     <li>Missing signing.keyId, signing.password, signing.secretKeyRingFile
 *     <li>A property called signing.skip has a true value
 *     <li>install task is present on the command line
 * </ul>
 */
class NebulaSignPlugin implements Plugin<Project> {

    Sign signJarsTask

    void apply(Project project) {

        // Per http://www.gradle.org/docs/current/userguide/signing_plugin.html, it's well established that
        // signing.keyId, signing.password, signing.secretKeyRingFile need to be provided. While SigningExtension
        // can be configured with other values, it's best to stick with these well known names.

        // Conditionalize the actual signing on having the property settings. Otherwise leave the
        boolean hasSigningProps = project.hasProperty('signing.keyId') &&
                project.hasProperty('signing.password') &&
                project.hasProperty('signing.secretKeyRingFile')

        // Provide two mechanism for avoiding signing.
        // The first is just a backdoor-hatch incase it's just problematric for some.
        boolean skipSigning = !hasSigningProps || (project.hasProperty('signing.skip') && Boolean.valueOf(project.skip)) || project.gradle.startParameter.taskNames.contains('install')

        // adding 'signing' plugin
        project.plugins.apply(SigningPlugin)

        // sign all artifacts
        signJarsTask = project.tasks.create('signJars', Sign)

        // Conditionally sign.
        if(!skipSigning) {
            // Only try to sign a configuration if we've got keys, otherwise we'll get artifacts into the archives
            // configurations with no bodies.
            signJarsTask.sign(project.configurations.getByName('archives'))
        } else {
            // Otherwise it'll complain that it has no signatories, even though it doesn't need any
            signJarsTask.required = false
        }

        // call signJar task before publish task, aka bintrayUpload
        project.task('preparePublish').dependsOn(signJarsTask)

        project.plugins.withType(NebulaBaseMavenPublishingPlugin) { NebulaBaseMavenPublishingPlugin basePlugin ->
            basePlugin.withMavenPublication { MavenPublication mavenJava ->
                // give signature files to artifact method. If the task's sign method is never called, then this callback
                // will never be called.
                signJarsTask.signatures.all { Signature signature ->
                    mavenJava.artifact(signature, new Action<MavenArtifact>() {
                        @Override
                        void execute(MavenArtifact t) {
                            t.classifier = signature.toSignArtifact.classifier != ""?signature.toSignArtifact.classifier:null
                            t.extension = signature.getType() // 'jar.asc'
                        }
                    })
                }
            }
        }

        // Support POM
        // Create configuration which pom can be added to, all artifacts will automatically get added to archives, which is already
        // being signed
        project.configurations.create('pom')

        // Immediately create a wrapper task when the generator appears
        project.tasks.matching { it instanceof GenerateMavenPom }.all { GenerateMavenPom pomGenerator ->
            // 'wrapGeneratePomFileForMavenJavaPublication'
            def taskName = "wrap${pomGenerator.name.capitalize()}"

            // Have GenerateMavenPom have a destination that we can sign, with a nice name
            def pubName = (pomGenerator.name =~ /For(.*)Publication/)[0][1]
            pomGenerator.destination = project.file("${project.buildDir}/poms/${WordUtils.uncapitalize(pubName)}.pom")
{'fraction_non_alphanumeric': 0.08828109791707223, 'fraction_numerical': 0.003893322951138797, 'mean_word_length': 2.8584303417198647, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '1793708', 'n_tokens_mistral': 2852, 'n_tokens_neox': 2677, 'n_words': 737}