import org.ajoberstar.grgit.*

buildscript {
    repositories {
        mavenCentral()
    }
    
    dependencies {
        classpath 'org.ajoberstar:gradle-git:0.11.2'
    }
}

apply plugin: 'signing'

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}
task androidJavadocsJar(type: Jar) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}
artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

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

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { deployment -> signing.signPom(deployment) }
            
            if (project.hasProperty('sonatype.username') && project.hasProperty('sonatype.password')) {
                def username = project.property('sonatype.username')
                def password = project.property('sonatype.password')
                
                repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                    authentication(userName: username, password: password)
                }
                snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
                    authentication(userName: username, password: password)
                }
            }
        }
    }
}

[uploadArchives.repositories.mavenDeployer, install.repositories.mavenInstaller]*.pom*.project {
    name githubname
    packaging 'aar'
    description githubdescription
    url "https://github.com/evant/$githubname"
    scm {
        url "git@github.com:evant/${githubname}.git"
        connection "scm:git:git@github.com:evant/${githubname}.git"
        developerConnection "scm:git:git@github.com:evant/${githubname}.git"
    }
    licenses {
        license {
            name 'The Apache Software License, Version 2.0'
            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
            distribution 'repo'
        }
    }
    developers {
        developer {
            id 'evant'
            name 'Evan Tatarka'
        }
    }
}

def grgit = Grgit.open(project.rootDir)
def branchName = grgit.branch.current.name

task tagRelease << {
    grgit.tag.add {
        name = "v$version"
        message = "Release of $version"
    }
}

if (branchName == 'master' && !version.endsWith('SNAPSHOT')) {
    uploadArchives.dependsOn(tagRelease)
}
