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
    } else {
        // For pure Kotlin libraries, in case you have them
        from sourceSets.main.java.srcDirs
    }
}

task androidJavadocJar(type: Jar) {
    archiveClassifier.set('javadoc')
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocJar
}

group = PUBLISH_GROUP_ID
version = PUBLISH_VERSION
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                // The coordinates of the library, being set from variables that
                // we'll set up later
                groupId PUBLISH_GROUP_ID
                artifactId PUBLISH_ARTIFACT_ID
                version PUBLISH_VERSION

                // Two artifacts, the `aar` (or `jar`) and the sources
                if (project.plugins.findPlugin("com.android.library")) {
                    from components.release
                    // Note: AGP 8.5+ automatically includes sources and javadoc from components.release
                    // so we don't need to add them manually
                } else {
                    from components.java
                    artifact androidSourcesJar
                    artifact androidJavadocJar
                }

                pom {
                    name = PUBLISH_ARTIFACT_ID
                    description = PUBLISH_DESCRIPTION
                    url = PUBLISH_URL
                    licenses {
                        license {
                            name = PUBLISH_LICENSE_NAME
                            url = PUBLISH_LICENSE_URL
                        }
                    }
                    developers {
                        developer {
                            id = PUBLISH_DEVELOPER_ID
                            name = PUBLISH_DEVELOPER_NAME
                            email = PUBLISH_DEVELOPER_EMAIL
                        }
                    }

                    // Version control info - if you're using GitHub, follow the
                    // format as seen here
                    scm {
                        connection = PUBLISH_SCM_CONNECTION
                        developerConnection = PUBLISH_SCM_DEVELOPER_CONNECTION
                        url = PUBLISH_SCM_URL
                    }
                }
            }
        }
    }

    signing {
        def signingKeyId = rootProject.ext["signing.keyId"]
        def signingKey = rootProject.ext["signing.key"]
        def signingPassword = rootProject.ext["signing.password"]

        if (signingKeyId && signingKey && signingPassword) {
            useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
            sign(publishing.publications)
        }
    }

    // Ensure proper task ordering
    tasks.withType(AbstractPublishToMaven).configureEach {
        dependsOn(tasks.withType(Sign))
    }

    tasks.withType(GenerateModuleMetadata).configureEach {
        dependsOn(androidSourcesJar)
        dependsOn(androidJavadocJar)
    }
}

publish.dependsOn build
publishToMavenLocal.dependsOn build
publishToSonatype.dependsOn publish
