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

android {
    compileSdkVersion 31
    buildToolsVersion '31.0.0'

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 31
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    new FileInputStream(secretPropsFile).withCloseable { is ->
        p.load(is)
    }
    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')
    ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
}

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
}

version = '1.6.1'

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId = 'com.github.anastr'
                artifactId = 'speedviewlib'
                version = this.version

                // Two artifacts, the `aar` (or `jar`) and the sources
                if (project.plugins.findPlugin("com.android.library")) {
                    artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
                } else {
                    artifact("$buildDir/libs/${project.getName()}-${version}.jar")
                }
                artifact androidSourcesJar

                pom {
                    name = 'SpeedView'
                    description = 'Dynamic Speedometer and Gauge for Android. amazing, powerful, and multi shape.'
                    url = 'https://github.com/anastr/SpeedView'
                    signing {
                        useGpgCmd()
                        sign publishing.publications.release
//                        sign configurations.archives
                    }
                    licenses {
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            id = 'anastr'
                            name = 'Anas Altair'
                            email = properties.getProperty("userEmail")
                        }
                    }
                    scm {
                        connection = 'scm:git:github.com/anastr/SpeedView.git'
                        developerConnection = 'scm:git:ssh://github.com/anastr/SpeedView.git'
                        url = 'https://github.com/anastr/SpeedView'
                    }
                    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)
                        }
                    }
                }
            }
        }
        repositories {
            maven {
                name = "sonatype"
                url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
                credentials {
                    username = properties.getProperty("ossrhUsername")
                    password = properties.getProperty("ossrhPassword")
                }
            }
        }
    }
}

//signing {
//    sign publishing.publications.release
//    sign configurations.archives
////    sign publishing.publications
//}

dependencies {
}

// Avoid Kotlin docs error
tasks.withType(Javadoc) {
    enabled = false
}

repositories {
    mavenCentral()
}
