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

/**
 * publish:
 * ./gradlew build
 * ./gradlew publishReleasePublicationToSonatypeRepository
 */

ext {
    PUBLISH_GROUP_ID = 'com.github.aartikov'
    PUBLISH_VERSION = '1.5.0'
    DESCRIPTION = 'Sesame is a set of architecture components for Android development'

    GITHUB_USER = 'aartikov'
    GITHUB_PROJECT = 'Sesame'
    LICENSE_NAME = 'The MIT License'
    LICENSE_URL = "https://github.com/${GITHUB_USER}/${GITHUB_PROJECT}/blob/master/LICENSE"

    DEVELOPER_ID = 'aartikov'
    DEVELOPER_NAME = 'Artur Artikov'
    DEVELOPER_EMAIL = 'a.artikov@gmail.com'
}

task androidSourcesJar(type: 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
    }
}

task javadocJar(type: Jar, dependsOn: dokkaJavadoc) {
    archiveClassifier.set('javadoc')
    from dokkaJavadoc.outputDirectory
}

artifacts {
    archives androidSourcesJar
    archives javadocJar
}

group = PUBLISH_GROUP_ID
version = PUBLISH_VERSION

ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        ext[name] = value
    }
} else {
    ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
    ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
    ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
    ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
    ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
}

publishing {
    publications {
        release(MavenPublication) {
            groupId PUBLISH_GROUP_ID
            artifactId project.name
            version PUBLISH_VERSION
            if (project.plugins.findPlugin("com.android.library")) {
                artifact("$buildDir/outputs/aar/${project.name}-release.aar")
            } else {
                artifact("$buildDir/libs/${project.name}-${version}.jar")
            }

            artifact androidSourcesJar
            artifact javadocJar

            pom {
                name = project.name
                description = DESCRIPTION
                url = "https://github.com/${GITHUB_USER}/${GITHUB_PROJECT}"
                licenses {
                    license {
                        name = LICENSE_NAME
                        url = LICENSE_URL
                    }
                }
                developers {
                    developer {
                        id = DEVELOPER_ID
                        name = DEVELOPER_NAME
                        email = DEVELOPER_EMAIL
                    }
                }
                scm {
                    connection = "scm:git:github.com/${GITHUB_USER}/${GITHUB_PROJECT}.git"
                    developerConnection = "scm:git:ssh://github.com/${GITHUB_USER}/${GITHUB_PROJECT}.git"
                    url = "https://github.com/${GITHUB_USER}/${GITHUB_PROJECT}/tree/main"
                }
                withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')

                    project.configurations.implementation.allDependencies.each {
                        if (it.name != 'unspecified') {
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
        }
    }
    repositories {
        maven {
            name = "sonatype"
            def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            credentials {
                username ossrhUsername
                password ossrhPassword
            }
        }
    }
}

signing {
    sign publishing.publications
}