apply plugin: 'maven-publish'
apply plugin: 'signing'

def isReleaseBuild() {
    return VERSION_NAME.contains("dev") == false
}

def getReleaseRepositoryUrl() {
    return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
            : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}

def getSnapshotRepositoryUrl() {
    return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
            : "https://oss.sonatype.org/content/repositories/snapshots/"
}

def getRepositoryUsername() {
    return System.getenv("NEXUS_USERNAME")
}

def getRepositoryPassword() {
    return System.getenv("NEXUS_PASSWORD")
}

afterEvaluate { project ->
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.release
                groupId = GROUP
                artifactId = POM_ARTIFACT_ID

                VERSION_NAME = libVersion
                version = VERSION_NAME

                repositories {
                    maven {
                        def releasesRepoUrl = getReleaseRepositoryUrl()
                        def snapshotsRepoUrl = getSnapshotRepositoryUrl()
                        url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                        credentials {
                            username getRepositoryUsername()
                            password getRepositoryPassword()
                        }
                    }
                }
                pom {
                    name = POM_NAME
                    packaging = POM_PACKAGING
                    description = POM_DESCRIPTION
                    url = POM_URL

                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            name = POM_DEVELOPER_NAME
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                }
            }
        }
    }

    signing {
        allprojects { ext."signing.keyId" = System.getenv("MAVEN_KEYID") }
        allprojects { ext."signing.password" = System.getenv("MAVEN_PASSWORD") }
        allprojects { ext."signing.secretKeyRingFile" = System.getenv("MAVEN_SECRING_PATH") }

        required { isReleaseBuild() && gradle.taskGraph.hasTask("publish") }
        sign publishing.publications.mavenJava
    }

    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
        exclude '**/*.kt'
        include '**/BuildConfig.java' 
    }

    afterEvaluate {
        androidJavadocs.classpath += files(android.libraryVariants.collect { variant ->
            variant.javaCompileProvider.get().classpath.files
        })
    }

    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier 'javadoc'
        from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier 'sources'
        from android.sourceSets.main.java.sourceFiles
    }

    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}
