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

task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    if (project.plugins.findPlugin('com.android.library')) {
        // For Android libraries
        from android.sourceSets.main.java.srcDirs
        from android.sourceSets.main.kotlin.srcDirs
    } else {
        // For pure Kotlin libraries, in case you have them
        from sourceSets.main.java.srcDirs
        from sourceSets.main.kotlin.srcDirs
    }
}

artifacts {
    archives androidSourcesJar
}

group = PUBLISH_GROUP_ID
version = PUBLISH_VERSION

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                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 androidSourcesJar

                pom {
                    name = PUBLISH_ARTIFACT_ID
                    description = 'A modern contacts Android API.'
                    url = 'https://github.com/alexstyl/contactstore'
                    licenses {
                        license {
                            name = 'ContactStore License'
                            url = 'https://github.com/alexstyl/contactstore/blob/main/LICENSE'
                        }
                    }
                    developers {
                        developer {
                            id = 'alexstyl'
                            name = 'Alex Styl'
                            email = 'alex@alexstyl.com'
                        }
                    }

                    scm {
                        connection = 'scm:git:github.com/alexstyl/contactstore.git'
                        developerConnection = 'scm:git:ssh://github.com/alexstyl/contactstore.git'
                        url = 'https://github.com/alexstyl/contactstore/tree/main'
                    }
                }
            }
        }
    }
}

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