apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'org.jetbrains.dokka'

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
    }
}

tasks.withType(dokkaHtmlPartial.getClass()).configureEach {
    pluginsMapConfiguration.set(
            ["org.jetbrains.dokka.base.DokkaBase": """{ "separateInheritedMembers": true}"""]
    )
}

task javadocJar(type: Jar, dependsOn: dokkaJavadoc) {
    archiveClassifier.set('javadoc')
    from dokkaJavadoc.outputDirectory
}

artifacts {
    archives androidSourcesJar
    archives javadocJar
}

group = publishedGroupId
version = libraryVersion

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId publishedGroupId
                artifactId artifactId
                version libraryVersion
                if (project.plugins.findPlugin("com.android.library")) {
                    from components.release
                } else {
                    from components.java
                }

                artifact androidSourcesJar
                artifact javadocJar

                pom {
                    name = artifactId
                    description = libraryDescription
                    url = siteUrl
                    licenses {
                        license {
                            name = licenseName
                            url = licenseUrl
                        }
                    }
                    developers {
                        developer {
                            id = developerId
                            name = developerName
                            email = developerEmail
                        }
                    }
                    scm {
                        connection = gitConnection
                        developerConnection = gitDeveloperConnection
                        url = gitConnection
                    }
                }
            }
        }
    }
}

signing {
    useInMemoryPgpKeys(
            SIGNING_KEY_ID,
            SIGNING_KEY,
            SIGNING_PASSWORD,
    )
    sign publishing.publications
}
