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

group = PUBLISH_GROUP_ID
version = PUBLISH_VERSION

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}"""]
    )
}

tasks.register('javadocJar', Jar) {
    dependsOn dokkaJavadoc
    archiveClassifier.set('javadoc')
    from dokkaJavadoc.outputDirectory
}

artifacts {
    archives androidSourcesJar
    archives javadocJar
}

afterEvaluate { project ->
    publishing {
        publications {
            release(MavenPublication) {
                groupId "io.writeopia"
                artifactId PUBLISH_ARTIFACT_ID
                version PUBLISH_VERSION

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

                artifact androidSourcesJar
                artifact javadocJar

                pom {
                    name = PUBLISH_ARTIFACT_ID
                    description = "A library to create complex text editors inside apps"
                    url = 'https://github.com/leandroBorgesFerreira/Writeopia'
                    licenses {
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            id = 'leandroBorgesFerreira'
                            name = 'Leandro Borges Ferreira'
                            email = "lehen01@gmail.com"
                            url = "https://github.com/leandroBorgesFerreira"
                        }
                    }
                    scm {
                        connection = "scm:git@github.com:leandroBorgesFerreira/Writeopia.git"
                        developerConnection = "scm:git:ssh://github.com/leandroBorgesFerreira/Writeopia.git"
                        url = "https://github.com/leandroBorgesFerreira/Writeopia"
                    }
                }
            }
        }
    }
}

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