/*
 * Copyright (c) 2019 Adyen N.V.
 *
 * This file is open source and available under the MIT license. See the LICENSE file for more info.
 *
 * Created by ran on 6/2/2019.
 */
// cannot reference the plugins declared in the version catalogs here because it requires the use of the `plugins` block instead of the `apply plugin` command
// the `plugins` block cannot be used in gradle files such as this one, which are imported into other gradle files with the `apply from` command
apply plugin: "maven-publish"
apply plugin: "signing"

ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["sonatypeCentralPortalUsername"] = ''
ext["sonatypeCentralPortalPassword"] = ''
ext["sonatypeStagingProfileId"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        ext[name] = value
    }
} else {
    ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
    ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
    ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
    ext["sonatypeCentralPortalUsername"] = System.getenv('SONATYPE_CENTRAL_PORTAL_USERNAME')
    ext["sonatypeCentralPortalPassword"] = System.getenv('SONATYPE_CENTRAL_PORTAL_PASSWORD')
    ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
}

final theGroupId = "com.adyen.checkout"
final theArtifactId = project.mavenArtifactId
final theVersion = rootProject.ext.versionName

final theName = project.mavenArtifactName
final theDescription = project.mavenArtifactDescription
final theUrl = "https://github.com/Adyen/adyen-android"

final theLicenseName = "MIT License"
final theLicenseUrl = "https://opensource.org/licenses/MIT"

final theOrganizationName = "Adyen N.V."
final theOrganizationUrl = "https://www.adyen.com/"

final theTeamName = "Checkout"

final theScmConnection = "scm:git:git://github.com/Adyen/adyen-android.git"
final theScmUrl = "https://github.com/Adyen/adyen-android"

group theGroupId

publishing {
    publications {
        release(MavenPublication) {

            groupId theGroupId
            artifactId theArtifactId
            version theVersion

            artifact tasks.dokkaJavadocJar

            afterEvaluate {
                from(components.release)
            }

            pom {
                name = theName
                description = theDescription
                url = theUrl
                licenses {
                    license {
                        name = theLicenseName
                        url = theLicenseUrl
                    }
                }
                organization {
                    name = theOrganizationName
                    url = theOrganizationUrl
                }
                developers {
                    developer {
                        name = theTeamName
                        organization = theOrganizationName
                        organizationUrl = theOrganizationUrl
                    }
                }
                scm {
                    connection = theScmConnection
                    developerConnection = theScmConnection
                    url = theScmUrl
                }
            }
        }
    }

    repositories {
        maven {
            name = "sonatype"
            url = "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/"
            credentials {
                username sonatypeCentralPortalUsername
                password sonatypeCentralPortalPassword
            }
        }
    }
}

signing {
    sign publishing.publications
}
