apply plugin: "com.android.library"
apply plugin: "com.jfrog.bintray"
apply plugin: "maven-publish"

apply from: "coverage.gradle"

android {
    compileSdkVersion project.ext.compileSdk

    defaultConfig {
        minSdkVersion project.ext.minSdk
        targetSdkVersion project.ext.compileSdk
        versionCode 1
        versionName project.ext.version
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }

    defaultPublishConfig "release"
    publishNonDefault true

    buildTypes {
        debug {
            testCoverageEnabled true
        }

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    }

    sourceSets {
        // A set of testing helpers that are shared across test types
        testLib     { java.srcDir("src/main") }
        test        { java.srcDir("src/testLib") } // Robolectric tests
        androidTest { java.srcDir("src/testLib") } // Android (e2e) tests
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    // The main library only depends on the Android support lib
    implementation "com.android.support:support-compat:${project.ext.supportLibraryVersion}"

    def junit = 'junit:junit:4.12'
    def robolectric = 'org.robolectric:robolectric:3.6.1'
    def guava = 'com.google.guava:guava:23.6-android'
    def truth = 'com.google.truth:truth:0.39'

    // The common test library uses JUnit
    testLibImplementation junit

    // Unit tests (using Robolectric)
    testImplementation junit
    testImplementation robolectric
    testImplementation guava
    testImplementation truth
    testImplementation 'org.mockito:mockito-core:2.13.0'

    // Android (e2e) tests
    androidTestImplementation junit
    androidTestImplementation guava
    androidTestImplementation truth
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
}

task javadocs(type: Javadoc) {
    description "Generate Javadocs"
    source = android.sourceSets.main.java.sourceFiles
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    classpath += configurations.compile
    failOnError false
}

task javadocsJar(type: Jar, dependsOn: javadocs) {
    description "Package Javadocs into a jar"
    classifier = "javadoc"
    from javadocs.destinationDir
}

task sourcesJar(type: Jar) {
    description "Package sources into a jar"
    classifier = "sources"
    from android.sourceSets.main.java.sourceFiles
}

task aar(dependsOn: "assembleRelease") {
    group "artifact"
    description "Builds the library AARs"
}

// Artifact descriptions (surfaced in JCenter / Maven)
def artifactDesc = "The Firebase JobDispatcher is a library that provides a common" +
    " interface to scheduling engines on Android."

publishing {
    repositories {
        // build repo for quick validation
        maven {
            name "buildLocal"
            url "${buildDir}/repo"
        }
    }

    publications {
        // The initial FJD releases had two artifacts; one for users that
        // already had a dependency on the gcm client library and one for users
        // that didn't. There's no longer a need for these two variants, but we
        // continue to produce the -with-gcm-dep version (identical to the
        // standard artifact) to prevent breaking clients.
        //
        // TODO(ciarandowney): add a deprecation notice pointing users to the
        //                     regular version
        [
          standardArtifact: "firebase-jobdispatcher",
          legacyArtifact: "firebase-jobdispatcher-with-gcm-dep",
        ].each { key, value ->
            "dispatcherLibrary${key.capitalize()}"(MavenPublication) {
                artifactId value
                groupId project.ext.group
                version project.ext.version

                // Add the AAR artifact
                artifact("${buildDir}/outputs/aar/jobdispatcher-release.aar") {
                    // wrap in a closure because this task isn't defined 'till
                    // the android plugin finishes
                    builtBy { tasks["bundleRelease"] }
                }
                // Add the sources and javadoc JARs
                artifact sourcesJar
                artifact javadocsJar

                // Define the POM file programmatically
                pom.withXml {
                    def n = asNode()

                    n.appendNode("description", artifactDesc)
                    n.appendNode("inceptionYear", 2016)

                    def licensesNode = n.appendNode("licenses")
                    def licenseNode = licensesNode.appendNode("license")
                    licenseNode.appendNode("name", project.ext.licenseName)
                    licenseNode.appendNode("url", project.ext.licenseUrl)
                    licenseNode.appendNode("distribution", project.ext.licenseDist)

                    def developersNode = n.appendNode("developers")
                    def devNode = developersNode.appendNode("developer")
                    devNode.appendNode("id", "firebase")
                    devNode.appendNode("name", "Firebase")
                    devNode.appendNode("email", "support@firebase.com")
                    devNode.appendNode("url", "https://firebase.com/")
                    devNode.appendNode("organization", "Firebase")
                    devNode.appendNode("organizationUrl", "https://firebase.com/")
                    def rolesNode = devNode.appendNode("roles")
                    rolesNode.appendNode("role", "architect")
                    rolesNode.appendNode("role", "developer")

                    def scmNode = n.appendNode("scm")
                    scmNode.appendNode("url", project.ext.scmUrl)
                    scmNode.appendNode("connection", project.ext.scmConnection)
                    scmNode.appendNode("developerConnection", project.ext.scmDevConnection)

                    def depsNode = n.getAt("dependencies")[0]
                    if (depsNode == null) {
                        depsNode = n.appendNode("dependencies")
                    }

                    configurations.api.allDependencies.each {
                        def dependencyNode = depsNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                        dependencyNode.appendNode('type', 'aar')
                        dependencyNode.appendNode('scope', 'compile')
                    }
                    configurations.implementation.allDependencies.each {
                        def dependencyNode = depsNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                        dependencyNode.appendNode('type', 'aar')
                        dependencyNode.appendNode('scope', 'runtime')
                    }
                }
            }
        }
    }
}

afterEvaluate {
    bintray {
        user = findProperty("BINTRAY_USER") ?: System.getenv("BINTRAY_USER")
        key = findProperty("BINTRAY_KEY") ?: System.getenv("BINTRAY_KEY")

        publications = [
            "dispatcherLibraryStandardArtifact",
            "dispatcherLibraryLegacyArtifact"]

        pkg {
            userOrg = "firebase"
            repo = "firebase-jobdispatcher"
            name = "jobdispatcher"
            vcsUrl = "https://github.com/firebase/firebase-jobdispatcher-android.git"
            licenses = ["Apache-2.0"]

            version.name = project.ext.version
        }
    }
}
