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

final RELEASE_REPO_URL = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
final SNAPSHOT_REPO_URL = 'https://oss.sonatype.org/content/repositories/snapshots/'

final REPO_USERNAME = findProperty('NEXUS_USERNAME')
final REPO_PASSWORD = findProperty('NEXUS_PASSWORD')

final SIGN_KEY_ID = findProperty('signing.alexvasilkov.keyId')
final SIGN_PASSWORD = findProperty('signing.alexvasilkov.password')

final IS_RELEASE = !VERSION_NAME.contains('SNAPSHOT')

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

                pom.groupId = POM_GROUP
                pom.artifactId = POM_ARTIFACT_ID
                pom.version = VERSION_NAME

                repository(url: RELEASE_REPO_URL) {
                    authentication(userName: REPO_USERNAME, password: REPO_PASSWORD)
                }
                snapshotRepository(url: SNAPSHOT_REPO_URL) {
                    authentication(userName: REPO_USERNAME, password: REPO_PASSWORD)
                }

                pom.project {
                    name POM_NAME
                    packaging POM_PACKAGING
                    description POM_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
                        }
                    }
                }
            }
        }
    }

    signing {
        required { IS_RELEASE && gradle.taskGraph.hasTask('uploadArchives') }
        sign configurations.archives

        // We need to re-map singing properties to be able to mix properties for different projects
        project.ext.'signing.keyId' = SIGN_KEY_ID
        project.ext.'signing.password' = SIGN_PASSWORD
    }

    task jar(type: Jar, dependsOn: compileReleaseJavaWithJavac) {
        from file(project.compileReleaseJavaWithJavac.destinationDir)
        exclude '**/R.class', '**/BuildConfig.class'
    }

    task javadocs(type: Javadoc) {
        source = android.sourceSets.main.java.sourceFiles
        classpath = files(android.bootClasspath)
        exclude "**/internal/**"
        title "${POM_NAME} ${VERSION_NAME} API"
        options {
            windowTitle "${POM_NAME} ${VERSION_NAME} API"
        }

        if (JavaVersion.current().isJava8Compatible()) {
            allprojects {
                tasks.withType(Javadoc) {
                    options.addStringOption('Xdoclint:none', '-quiet')
                }
            }
        }
    }

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

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

    artifacts {
        archives jar
        archives sourcesJar
        archives javadocsJar
    }

    // Excluding aar artifacts from uploading
    configurations.archives.artifacts.with { archives ->
        for (def iter = archives.iterator(); iter.hasNext();) {
            if (iter.next().file =~ 'aar') iter.remove();
        }
    }
}
