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


task androidJavadocJar(type: Jar) {
    archiveClassifier.set('javadoc')
    from("$buildDir/javadoc")
}

task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    if (project.plugins.findPlugin("com.android.library")) {
        from android.sourceSets.main.java.srcDirs
        from android.sourceSets.main.kotlin.srcDirs
    } else {
        from sourceSets.main.java.srcDirs
        from sourceSets.main.kotlin.srcDirs
    }
}

group = project.groupId
version = project.versionName

ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''
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["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
    ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
    ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
}

publishing {
    publications {
        release(MavenPublication) {
            groupId project.groupId
            artifactId project.artifactId
            version project.versionName

            if (project.plugins.findPlugin("com.android.library")) {
                artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
            } else {
                artifact("$buildDir/libs/${project.getName()}-${version}.jar")
            }

            artifact androidJavadocJar
            artifact androidSourcesJar

            pom {
                name = project.libraryName
                description = project.libraryDescription
                url = project.siteUrl
                licenses {
                    license {
                        name = project.licenseName
                        url = project.licenseUrl
                    }
                }
                developers {
                    developer {
                        id = project.developerId
                        name = project.developName
                        email = project.developerEmail
                    }
                }
                scm {
                    connection = project.gitUrl
                    developerConnection = project.gitUrl
                    url = project.siteUrl
                }
                withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    project.configurations.implementation.allDependencies.each {
                        if (it.name != 'unspecified') {
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
        }
    }
    repositories {
        maven {
            name = "sonatype"
            url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
            credentials {
                username ossrhUsername
                password ossrhPassword
            }
        }
    }
}

signing {
    sign publishing.publications
}
