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

tasks.register('androidSourcesJar', 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 = PUBLISH_GROUP_ID
version = PUBLISH_VERSION

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                tasks.named("generateMetadataFileForReleasePublication").configure { dependsOn("androidSourcesJar") }
                groupId PUBLISH_GROUP_ID
                artifactId PUBLISH_ARTIFACT_ID
                version PUBLISH_VERSION
                if (project.plugins.findPlugin("com.android.library")) {
                    from components.release
                } else {
                    from components.java
                }

                artifact javadocJar

                pom {
                    name = PUBLISH_ARTIFACT_ID
                    description = 'Stream Feeds official Android SDK'
                    url = 'https://github.com/getstream/stream-feeds-android'
                    licenses {
                        license {
                            name = 'Stream License'
                            url = 'https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE'
                        }
                    }
                    developers {
                        developer {
                            id = 'jcminarro'
                            name = 'Jc Miñarro'
                            email = 'josecarlos@getstream.io'
                        }
                        developer {
                            id = 'aleksandar-apostolov'
                            name = 'Aleksandar Apostolov'
                            email = 'aleksandar.apostolov@getstream.io'
                        }
                        developer {
                            id = 'VelikovPetar'
                            name = 'Petar Velikov'
                            email = 'petar.velikov@getstream.io'
                        }
                        developer {
                            id = 'andremion'
                            name = 'André Mion'
                            email = 'andre.rego@getstream.io'
                        }
                        developer {
                            id = 'rahul-lohra'
                            name = 'Rahul Kumar Lohra'
                            email = 'rahul.lohra@getstream.io'
                        }
                        developer {
                            id = 'gpunto'
                            name = 'Gianmarco'
                            email = 'gianmarco.david@getstream.io'
                        }
                    }
                    scm {
                        connection = 'scm:git:github.com/getstream/stream-feeds-android.git'
                        developerConnection = 'scm:git:ssh://github.com/getstream/stream-feeds-android.git'
                        url = 'https://github.com/getstream/stream-feeds-android/tree/main'
                    }
                }
            }
        }
    }
}

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