apply plugin: 'maven'
apply plugin: 'signing'

def isReleaseBuild() {
    return VERSION_NAME.contains("SNAPSHOT") == false
}

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

def getRepositoryUsername() {
    return hasProperty('repositoryUsername') ? property('repositoryUsername') : ""
}

def getRepositoryPassword() {
    return hasProperty('repositoryPassword') ? property('repositoryPassword') : ""
}

def configureKeyframesPom(def pom) {
    pom.whenConfigured {
        applyOptionalDeps it, getOptionalDeps()
    }
    pom.project {
        name POM_NAME
        artifactId POM_ARTIFACT_ID
        packaging POM_PACKAGING
        description 'Keyframes'
        url 'https://github.com/facebookincubator/Keyframes'

        scm {
            url 'https://github.com/facebookincubator/Keyframes.git'
            connection 'scm:git:https://github.com/facebookincubator/Keyframes.git'
            developerConnection 'scm:git:git@github.com:facebookincubator/Keyframes.git'
        }

        licenses {
            license {
                name 'BSD License'
                url 'https://github.com/facebookIncubator/Keyframes/blob/master/LICENSE'
                distribution 'repo'
            }
        }

        developers {
            developer {
                id 'facebook'
                name 'Facebook'
            }
        }
    }
}

// Hack to modify the resulting pom's dependencies to use
// <optional>true</optional> where appropriate.
def applyOptionalDeps(def pom, def optionalDeps) {
    pom.dependencies.each { dep ->
        def artifactLabel = dep.groupId + ':' + dep.artifactId
        if (optionalDeps.contains(artifactLabel)) {
            dep.optional = true
        }
    }
}

def getOptionalDeps() {
    if (hasProperty('POM_OPTIONAL_DEPS')) {
        return property('POM_OPTIONAL_DEPS').split(',') as Set
    } else {
        return []
    }
}

afterEvaluate { project ->
    task androidJavadoc(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += files(android.bootClasspath)
        if (JavaVersion.current().isJava8Compatible()) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }

    task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
        classifier = 'javadoc'
        from androidJavadoc.destinationDir
    }

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

    android.libraryVariants.all { variant ->
        def name = variant.name.capitalize()
        task "jar${name}"(type: Jar, dependsOn: variant.javaCompile) {
            from variant.javaCompile.destinationDir
        }
    }

    artifacts {
        archives androidJavadocJar
        archives androidSourcesJar
    }

    version = VERSION_NAME
    group = GROUP

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    uploadArchives {
        configuration = configurations.archives
        repositories.mavenDeployer {
            beforeDeployment {
                MavenDeployment deployment -> signing.signPom(deployment)
            }

            repository(url: getRepositoryUrl()) {
                authentication(
                        userName: getRepositoryUsername(),
                        password: getRepositoryPassword())

            }

            configureKeyframesPom pom
        }
    }

    task installArchives(type: Upload) {
        configuration = configurations.archives
        repositories {
            mavenDeployer {
                repository url: "file://${System.properties['user.home']}/.m2/repository"
                configureKeyframesPom pom
            }
        }
    }
}