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


apply from: rootProject.file('gradle/versioning.gradle')

task javadocJar(type: Jar, dependsOn: dokkaHtml) {
    archiveClassifier = "javadoc"
    from dokkaHtml.outputDirectory
}

task sourcesJar(type: Jar) {
    archiveClassifier = 'sources'
    from android.sourceSets.main.java.source
}


publishing {
    publications {
        release(MavenPublication) {
            groupId = GROUP
            artifactId = POM_ARTIFACT_ID
            version = getVersionName()

            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
            artifact sourcesJar
            artifact javadocJar

            pom {
                name = POM_NAME
                packaging = POM_PACKAGING
                description = POM_DESCRIPTION
                url = POM_URL

                licenses {
                    license {
                        name = POM_LICENCE_NAME
                        url = POM_LICENCE_URL
                        distribution = POM_LICENCE_DIST
                    }
                }

                developers {
                    developer {
                        id = POM_DEVELOPER_ID
                        name = POM_DEVELOPER_NAME
                        email = POM_DEVELOPER_EMAIL
                    }
                }

                scm {
                    url = POM_SCM_URL
                    connection = POM_SCM_CONNECTION
                    developerConnection = POM_SCM_DEV_CONNECTION
                }

                //Replace this with components.release after we updated the gradle version
                withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')

                    project.configurations.implementation.allDependencies.each {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
}


signing {
    def signingKey = System.getenv("SIGNING_KEY")
    def signingPassword = System.getenv("SIGNING_PASSWORD")
    useInMemoryPgpKeys(signingKey, signingPassword)
    sign publishing.publications
}


publish.dependsOn build
tasks.named('signReleasePublication').configure {
    dependsOn 'bundleReleaseAar'
}

