apply plugin: 'com.github.kt3k.coveralls' //version '2.6.3'

buildscript {
    repositories {
        jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
        classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.2'
    }
}

description = "g419 - Top-level project"
version = "2.5-SNAPSHOT"

apply plugin: 'wrapper'

group = "pl.pwr.nlp"

allprojects {
    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'idea'
    apply plugin: 'eclipse'
    apply plugin: 'groovy'
    apply plugin: 'jacoco'

    compileJava {
        options.compilerArgs << "-Xlint:all"
        options.debug = true
    }

    group = "pl.pwr.nlp"
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    version = '2.5-SNAPSHOT'

    repositories {
        mavenCentral()
    }

    dependencies {
        compile(
                'org.slf4j:slf4j-api:1.7.2',
                'io.vavr:vavr:0.9.0'
        )
        runtime(
                'org.slf4j:slf4j-log4j12:1.7.2',
                'com.google.code.gson:gson:2.7',
                'commons-lang:commons-lang:2.6',
                'org.apache.commons:commons-csv:1.4'
        )
        testCompile(
                'junit:junit:4.12',
                'org.codehaus.groovy:groovy-all:2.4.4',
                'org.spockframework:spock-core:1.0-groovy-2.4'
        )
    }

    test {
        useJUnit()
    }

    task sourcesJar(type: Jar, dependsOn: classes) {
        description "Makes JAR with sources of this module."
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    task javadocJar(type: Jar, dependsOn: javadoc) {
        description "Makes JAR with javadocs of this module."
        classifier = 'javadoc'
        from javadoc.destinationDir
    }

    def artifactsTasks = [
            jar,
            sourcesJar,
//        javadocJar //uncomment this when javadoc will work
    ]

    plugins.withType(ApplicationPlugin) {
        it.project.apply plugin: 'com.github.johnrengelman.shadow'

        shadowJar {
            classifier = 'all'
        }

        it.project.tasks.find { it.name == "jar" }.dependsOn it.project.tasks.find { it.name == "shadowJar" }
    }

    artifacts {
        artifactsTasks.each {
            archives it
        }
    }

    jacoco {
        toolVersion = '0.7.1.201405082137'
    }
//    jacocoTestReport {
//        reports {
//            xml.enabled true
//            html.enabled true
//            //csv.enabled false
//            //html.destination "${buildDir}/reports/coverage"
//        }
//    }
}

subprojects {
    dependencies {
        testCompile 'junit:junit:4.11'
    }

    jacocoTestReport {
        additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)
        sourceDirectories = files(sourceSets.main.allSource.srcDirs)
        classDirectories = files(sourceSets.main.output)
        reports {
            html.enabled = true
            xml.enabled = true
            csv.enabled = false
        }
    }
}

task libZip(type: Zip, dependsOn: build) {
    description "Makes ZIP with all JARs and some additional stuff, like README and samples."
    classifier = "libs"
    into("liner2-pack") {
        from new File(project.rootDir, "stuff/README")
        from new File(project.rootDir, "stuff/log4j.properties")
        into("libs") {
            from subprojects.collect {
                it.jar.archivePath
            }
        }
        into("wrapper") {
            from new File(project.rootDir, "stuff/wrapper")
        }
    }
}

task appZip(type: Zip, dependsOn: build) {
    description "Makes ZIP with all FAT-JARs."
    classifier = "apps"
    def files = subprojects.collect {
        File f = it.jar.archivePath
        String p = f.absolutePath
        def parts = p.split("[.]") as List
        def last = parts.pop()
        def archiveName = parts.pop() + "-all"
        def appPath = (parts + [archiveName, last]).join(".")
        File appFile = new File(appPath)
        if (appFile.exists())
            return appFile
        return null
    }
    into("liner2-executable-jars") {
        from files.findAll()
    }
}

task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
    dependsOn = subprojects.test
    additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(subprojects.sourceSets.main.output)
    executionData = files(subprojects.jacocoTestReport.executionData)
    reports {
        html.enabled = true
        xml.enabled = true
        csv.enabled = false
        xml.destination "build/reports/jacoco/test/jacocoTestReport.xml"
    }
    onlyIf = {
        true
    }
    doFirst {
        executionData = files(executionData.findAll {
            it.exists()
        })
    }
}

coveralls {
    sourceDirs = subprojects.sourceSets.main.allSource.srcDirs.flatten()
    jacocoReportPath = "build/reports/jacoco/test/jacocoTestReport.xml"
}

tasks.coveralls {
    dependsOn jacocoRootReport
    onlyIf { System.env.'CI' }
}