buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
    }
}

plugins {
    id "maven"
    id "signing"
}

apply plugin: 'com.android.library'

apply from: '../versions.gradle'

repositories {
    google()
    jcenter()
    mavenCentral()
}

android {
    compileSdkVersion 29
    buildToolsVersion = '29.0.2'
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 29
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

def checkSigning() {
    ext."signing.secretKeyRingFile" = System.properties['user.home'] + '/.gnupg/secring.gpg'
    if (!hasProperty("signing.keyId")) {
        ext."signing.keyId" = System.getenv("signingKeyId")
    }
    if (!hasProperty("signing.password")) {
        ext."signing.password" = System.getenv("signingPassword")
    }
}

def getReleaseRepositoryUrl() {
    return hasProperty('NEXUS_RELEASE_REPOSITORY_URL') ? NEXUS_RELEASE_REPOSITORY_URL : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}

def getSnapshotRepositoryUrl() {
    return hasProperty('NEXUS_SNAPSHOT_REPOSITORY_URL') ? NEXUS_SNAPSHOT_REPOSITORY_URL : "https://oss.sonatype.org/content/repositories/snapshots/"
}

def getRepositoryUsername() {
    return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : System.getenv("NEXUS_USERNAME")
}

def getRepositoryPassword() {
    return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : System.getenv("NEXUS_PASSWORD")
}

signing {
    required { gradle.taskGraph.hasTask(":uploadArchives") }
    checkSigning()
    sign configurations.archives
}

tasks.withType(Sign) {
    onlyIf {gradle.taskGraph.hasTask(":uploadArchives") }
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    destinationDir = file("../javadoc/")
    failOnError false
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.getDestinationDir()
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives androidSourcesJar
    archives javadocJar
}

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

            repository(url: getReleaseRepositoryUrl()) {
                authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
            }
            snapshotRepository(url: getSnapshotRepositoryUrl()) {
                authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
            }
            pom.version = VERSION_NAME
            pom.artifactId = POM_INJECTOR_ANDROID_NAME
            pom.groupId = GROUP

            pom.project {
                name POM_INJECTOR_ANDROID_NAME
                packaging POM_AAR_PACKAGING
                description POM_INJECTOR_ANDROID_DESCRIPTION
                url POM_URL

                scm {
                    url POM_SCM_URL
                    connection POM_SCM_CONNECTION
                    developerConnection POM_SCM_DEV_CONNECTION
                }

                licenses {
                    license {
                        name POM_LICENCE_NAME
                        url POM_LICENCE_URL
                        distribution POM_LICENCE_DIST
                    }
                }

                developers {
                    developer {
                        id POM_DEVELOPER_ID
                        name POM_DEVELOPER_NAME
                    }
                }
            }
        }
    }
}
