/*
 * Copyright 2013 Chris Banes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Based on: https://github.com/mcxiaoke/gradle-mvn-push/blob/master/gradle-mvn-push.gradle.
 *
 * To install in a local maven repo:
 * 1. In the project you want to test (not Glide), add mavenLocal() to the repositories list.
 * 2. In Glide, run: ./gradlew uploadArchives -PLOCAL
 *
 * For faster runs add: -x check when building Glide.
 */

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

version = VERSION_NAME
group = GROUP

static def localMavenRepo() {
    'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
}

@SuppressWarnings("GrMethodMayBeStatic")
def isReleaseBuild() {
    return !VERSION_NAME.contains("SNAPSHOT")
}

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

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

def getRepositoryUsername() {
    return hasProperty('USERNAME') ? USERNAME : (hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : '')
}

def getRepositoryPassword() {
    return hasProperty('PASSWORD') ? PASSWORD : (hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : '')
}

afterEvaluate { project ->
    def isAndroidProject = project.plugins.hasPlugin('com.android.application') || project.plugins.hasPlugin('com.android.library')
    // To avoid uploading the default empty jar artifact in the project root directory, we use a custom
    // configuration to specify which artifacts we want to upload.
    uploadArchives {
        repositories {
            mavenDeployer {
                // allow uploading through FTP protocol with the following command:
                // gradle uploadArchives -PSNAPSHOT_REPOSITORY_URL=ftp://host/repo/path -PUSERNAME=uname -PPASSWORD=passwd
                configuration = configurations.create('deployerJars')
                configuration.dependencies.add dependencies.create('org.apache.maven.wagon:wagon-ftp:2.2')

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

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

                repository(url: getReleaseRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
                snapshotRepository(url: getSnapshotRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }

                pom.whenConfigured { pom ->
                    pom.packaging = POM_PACKAGING
                }

                // Dependencies are only automatically included by the release plugin if the release
                // variant is built. Since we've disabled the release variant to improve build
                // times, we need to add the dependencies to the pom file explicitly.
                if (isAndroidProject) {
                    pom.withXml {
                        def dependenciesNode = asNode().appendNode('dependencies')

                        project.configurations.implementation.allDependencies.each {
                            def groupId = it.group
                            def artifactId = it.name
                            // If we specify an artifact id that differs from the project name, it won't
                            // match. To avoid that, we look up the artifact id (and group) by property
                            // for any project dependencies.
                            // TODO: there must be a neater way to do this.
                            if (it instanceof ProjectDependency) {
                                def properties = it.getDependencyProject().getProperties()
                                groupId = properties.get("GROUP")
                                artifactId = properties.get("POM_ARTIFACT_ID")
                            }
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', groupId)
                            dependencyNode.appendNode('artifactId', artifactId)
                            dependencyNode.appendNode('version', it.version)
                            dependencyNode.appendNode('scope', 'compile')
                        }
                    }
                }

                pom.project {
                    name = POM_NAME
                    description = POM_DESCRIPTION
                    url = POM_URL

                    scm {
                        url POM_SCM_URL
                        connection POM_SCM_CONNECTION
                        developerConnection POM_SCM_DEV_CONNECTION
                    }

                    licenses {
                        license {
                            name = 'Simplified BSD License'
                            url = 'http://www.opensource.org/licenses/bsd-license'
                            distribution = 'repo'
                        }
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            distribution = 'repo'
                        }
                    }

                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            name = POM_DEVELOPER_NAME
                            email = POM_DEVELOPER_EMAIL
                        }
                    }
                }
            }
        }
    }

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


    if (isAndroidProject) {
        def variants = project.android.libraryVariants.findAll {
            it.buildType.name.equalsIgnoreCase('debug')
        }

        def getAndroidSdkDirectory = project.android.sdkDirectory

        def getAndroidJar = "${getAndroidSdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar"

        task androidJavadocs(type: Javadoc, dependsOn: assembleDebug) {
            source = variants.collect { it.javaCompile.source }
            classpath = files(
                    getAndroidJar,
                    project.file("build/intermediates/classes/debug")
            )
            doFirst {
                classpath += files(variants.collect { it.javaCompile.classpath.files })
            }
            options {
                links("http://docs.oracle.com/javase/7/docs/api/")
                linksOffline("http://d.android.com/reference",
                        "${getAndroidSdkDirectory}/docs/reference")
            }

            exclude '**/BuildConfig.java'
            exclude '**/R.java'
        }

        def cleanJavadocTask = task("cleanJavadocTask", type: Delete) {
            delete androidJavadocs.destinationDir
        } as Task
        project.clean.dependsOn(cleanJavadocTask)

        task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
            classifier = 'javadoc'
            from androidJavadocs.destinationDir
            baseName "${JAR_PREFIX}${project.name}${JAR_POSTFIX}"
        }

        task androidSourcesJar(type: Jar) {
            classifier = 'sources'
            from project.android.sourceSets.main.java.source
            baseName "${JAR_PREFIX}${project.name}${JAR_POSTFIX}"
        }

        task androidLibraryJar(type: Jar, dependsOn: compileDebugJavaWithJavac /* == variant.javaCompile */) {
            from compileDebugJavaWithJavac.destinationDir
            exclude '**/R.class'
            exclude '**/BuildConfig.class'
            exclude '**/R$*.class'
            baseName "${JAR_PREFIX}${project.name}${JAR_POSTFIX}"
        }

        artifacts {
            archives androidLibraryJar
            archives androidSourcesJar
            archives androidJavadocsJar
            // This is unnecessary with a release variant because by default the release variant
            // includes the release aar in archives. Since we've disabled our release variants and
            // want to include an aar, we need to manually specify the task that produces the aar
            // here.
            archives project.tasks.bundleDebugAar
        }
    } else if (project.plugins.hasPlugin('java')) {
        task sourcesJar(type: Jar, dependsOn: classes) {
            classifier = 'sources'
            from sourceSets.main.allSource
        }

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

        artifacts {
            archives sourcesJar
            archives javadocsJar
        }
    }
    logger.info("Published artifacts in ${configurations.archives}:")
    configurations.archives.artifacts.files.files.each { logger.info("\t$it") }
}
