/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.
 */
include ':agera'
include ':extensions:content'
include ':extensions:rvadapter'
include ':extensions:rvdatabinding'
include ':extensions:database'
include ':extensions:net'
include ':testapp'

gradle.ext.versionCode = 10400
gradle.ext.versionName = '1.4.0'

gradle.ext.bintrayRepo = 'Agera'
gradle.ext.bintrayName = 'agera'
gradle.ext.bintrayUser = 'ernstsson'

gradle.ext.description = 'Reactive Programming for Android'
gradle.ext.group = 'com.google.android.agera'
gradle.ext.url = 'https://github.com/google/agera'
gradle.ext.gitUrl = 'https://github.com/google/agera.git'
gradle.ext.licenseName = 'The Apache Software License, Version 2.0'
gradle.ext.licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'

gradle.ext.minSdkVersion = 9
gradle.ext.compileSdkVersion = 25
gradle.ext.buildToolsVersion = '25.0.1'
gradle.ext.supportLibraryVersion = '25.2.0'
gradle.ext.javaVersion = JavaVersion.VERSION_1_7

gradle.allprojects {
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'me.tatarka:gradle-retrolambda:3.4.0'
            classpath 'com.android.tools.build:gradle:2.2.3'
            classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
            classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2'
            classpath 'com.novoda:bintray-release:0.3.4'
        }
    }
    repositories {
        jcenter()
    }
    tasks.withType(JavaForkOptions) {
        jvmArgs '-Djava.awt.headless=true'
    }
    tasks.withType(Test) {
        testLogging {
            exceptionFormat "full"
            events "passed", "failed", "skipped"
            showCauses true
            showExceptions true
            showStackTraces true
            showStandardStreams true
        }
    }
    afterEvaluate { project ->
        if (project.hasProperty("android")) {
            android {
                defaultConfig {
                    versionCode gradle.versionCode
                    versionName gradle.versionName
                }
            }
            if (android.hasProperty('libraryVariants')) {
                version = gradle.versionName
                group = gradle.group
                android {
                    lintOptions {
                        textReport true
                        textOutput 'stdout'
                        checkAllWarnings true
                    }
                    buildTypes {
                        debug {
                            testCoverageEnabled true
                        }
                    }
                    testOptions {
                        unitTests.all {
                            jacoco {
                                includeNoLocationClasses = true
                            }
                        }
                    }
                }
                dependencies {
                    compile 'com.android.support:support-annotations:' +
                            gradle.supportLibraryVersion
                    testCompile 'org.hamcrest:hamcrest-library:1.3'
                    testCompile 'junit:junit:4.12'
                    testCompile 'org.robolectric:robolectric:3.1.4'
                    testCompile 'org.mockito:mockito-core:2.3.5'
                    testCompile 'nl.jqno.equalsverifier:equalsverifier:2.1.8'
                }
                task sourcesJar(type: Jar) {
                    from android.sourceSets.main.java.srcDirs
                    classifier = 'sources'
                }
                task jacocoTestReport(type:JacocoReport, dependsOn: "testDebugUnitTest") {
                    group = "Reporting"
                    description = "Generate Jacoco coverage reports"
                    def fileFilter = ['**/R.class', '**/R$*.class', '**/BR.class',
                                      '**/BuildConfig.*', '**/Manifest*.*', 'android/**/*.*']
                    def debugTree = fileTree(dir:
                            "${project.buildDir}/intermediates/classes/debug",
                            excludes: fileFilter)
                    def mainSrc = "${project.projectDir}/src/main/java"

                    sourceDirectories = files([mainSrc])
                    classDirectories = files([debugTree])
                    additionalSourceDirs = files([
                            "${buildDir}/generated/source/buildConfig/debug",
                            "${buildDir}/generated/source/r/debug"
                    ])
                    executionData = fileTree(dir: project.projectDir, includes:
                            ['**/*.exec', '**/*.ec'])

                    reports {
                        xml.enabled = true
                        xml.destination = "${buildDir}/jacocoTestReport.xml"
                        csv.enabled = false
                        html.enabled = true
                        html.destination = "${buildDir}/reports/jacoco"
                    }
                }
                android.libraryVariants.all { variant ->
                    task("${variant.name}Javadoc", type: Javadoc) {
                        description "Generates Javadoc for $variant.name."
                        source = variant.javaCompile.source
                        classpath = files(variant.javaCompile.classpath.files,
                                project.android.getBootClasspath())
                        exclude '**/BuildConfig.java'
                        exclude '**/R.java'
                    }
                }
                task javadocJar(type: Jar, dependsOn: 'releaseJavadoc') {
                    classifier = 'javadoc'
                    from {
                        releaseJavadoc.destinationDir
                    }
                }
                artifacts {
                    archives javadocJar
                    archives sourcesJar
                }
                android {
                    compileSdkVersion gradle.compileSdkVersion
                    buildToolsVersion gradle.buildToolsVersion
                }
            }
        }
    }
}
