apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

def groupIdString = "org.matrix.android"
def artifactIdString = "matrix-sdk-android-rustCrypto"
def versionName = "1.5.30.35"
def artifactPath = "$buildDir/outputs/aar/$artifactIdString-release.aar"
def publicationName = "release"
def projectId = "16"

tasks.register('sourceJar', Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = "sources"
}

publishing {
    publications {
        "$publicationName"(MavenPublication) {
            groupId groupIdString
            artifactId artifactIdString
            version versionName
            artifact artifactPath
            artifact sourceJar

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                ext.addDependency = { Dependency dep, String scope ->
                    if (dep.group == null || dep.name == null || dep.name == "unspecified")
                        return
                    final dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dep.group)
                    dependencyNode.appendNode('artifactId', dep.name)
                    if (dep.version != null)
                        dependencyNode.appendNode('version', dep.version)
                    dependencyNode.appendNode('scope', scope)
                }
                configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
                configurations.rustCryptoImplementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
            }
        }
    }
    repositories {
        Properties properties = new Properties()
        if (rootProject.file("local.properties").exists()) {
            properties.load(rootProject.file("local.properties").newDataInputStream())
        }
        maven {
            url "https://gitlab.futo.org/api/v4/projects/$projectId/packages/maven"
            name "GitLab"
            credentials(HttpHeaderCredentials) {
                name = "Private-Token"
                value = properties.getProperty("PUBLISH_TOKEN")
            }
            authentication {
                header(HttpHeaderAuthentication)
            }
        }
    }
}