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 = publishInfo.groupId
version = publishInfo.version

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {

                groupId publishInfo.groupId
                artifactId publishInfo.artifactId
                version publishInfo.version

                if (project.plugins.findPlugin("com.android.library")) {
                    from components.release
                } else {
                    from components.java
                }

                artifact androidSourcesJar
                artifact javadocJar

                pom {
                    name = publishInfo.artifactId
                    description = publishInfo.description
                    url = "https://github.com/aminography/${publishInfo.githubRepo}"

                    licenses {
                        license {
                            name = 'License'
                            url = "https://github.com/aminography/${publishInfo.githubRepo}/blob/main/LICENSE.md"
                        }
                    }

                    developers {
                        developer {
                            id = 'aminography'
                            name = 'Amin Hassani'
                            email = 'aminography@gmail.com'
                        }
                    }

                    scm {
                        connection = "scm:git:github.com/aminography/${publishInfo.githubRepo}.git"
                        developerConnection = "scm:git:ssh://github.com/aminography/${publishInfo.githubRepo}.git"
                        url = "https://github.com/aminography/${publishInfo.githubRepo}/tree/main"
                    }
                }
            }
        }
    }
}

signing {
    useInMemoryPgpKeys(
            rootProject.ext["signing.keyId"],
            rootProject.ext["signing.key"],
            rootProject.ext["signing.password"],
    )
    sign publishing.publications
}
