/*
 * publishing.gradle
 *
 * This source file is part of the FoundationDB open source project
 *
 * Copyright 2015-2018 Apple Inc. and the FoundationDB project authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

apply plugin: 'signing'

// Add various details to the pom file to allow for publishing
def addPublishingInfo(publication) {
    publication.pom {
        name = project.name
        url = 'https://foundationdb.github.io/fdb-record-layer/'

        licenses {
            license {
                name = 'The Apache License, Version 2.0'
                url = 'https://www.apache.org/licenses/LICENSE-2.0'
            }
        }

        developers {
            developer {
                name = 'FoundationDB'
            }
        }

        scm {
            url = 'https://github.com/FoundationDB/fdb-record-layer/'
            connection = 'scm:git:git@github.com:FoundationDB/fdb-record-layer.git'
            developerConnection = 'scm:git:git@github.com:FoundationDB/fdb-record-layer.git'
        }
    }
}

if (ext.publishLibrary) {
    publishing {
        publications {
            library(MavenPublication) { publication ->
                from components.java

                // Test fixtures appear to be placing their dependencies in the wrong scope
                // for maven compilation, which means that their dependencies wind up in the
                // final project's pom.xml.
                // See: https://github.com/gradle/gradle/issues/14936
                // Until this is fixed, skip these configurations to avoid polluting the list of dependencies
                configurations.each { config ->
                    if (config.name == 'testFixturesApiElements' || config.name == 'testFixturesRuntimeElements') {
                        components.java.withVariantsFromConfiguration(config) {
                            skip()
                        }
                    }
                }

                artifact tasks.sourcesJar
                artifact tasks.javadocJar
                artifact tasks.testJar
                addPublishingInfo(publication)
            }
        }
    }
    createDistribution.configure {
        afterEvaluate {
            dependsOn generatePomFileForLibraryPublication
            from tasks.jar, tasks.sourcesJar, tasks.javadocJar, tasks.testJar, tasks.generatePomFileForLibraryPublication
            rename { filename ->
                GenerateMavenPom pomTask = tasks.generatePomFileForLibraryPublication
                Jar jarTask = tasks.jar
                if (filename == pomTask.destination.name) {
                    jarTask.archiveFileName.map { archive -> archive.replace("jar", "pom") }
                } else {
                    filename
                }
            }
        }
    }
    if (Boolean.parseBoolean(centralPublish)) {
        signing {
            sign publishing.publications.library
        }
    }
}

ext {
    // Make the "addPublishingInfo" method visible from outside this plugin
    addPublishingInfo = this.&addPublishingInfo
}

publishing {
    if (Boolean.parseBoolean(publishBuild)) {
        repositories {
            if (Boolean.parseBoolean(githubPublish)) {
                maven {
                    name = "GitHubPackages"
                    url = "https://maven.pkg.github.com/FoundationDB/fdb-record-layer"
                    credentials {
                        username = System.getenv("GITHUB_ACTOR")
                        password = System.getenv("GITHUB_TOKEN")
                    }
                }
            }
        }
    }
}
