//Gradle file library ev3dev-lang-java
//Author: Juan Antonio Breña Moral

plugins {
    id 'io.franzbecker.gradle-lombok' version '1.14'
    id 'java'
    id 'java-library'
    id 'eclipse'
    id 'idea'
    id "jacoco"
    id 'checkstyle'
    id 'maven-publish'
    id 'org.hidetake.ssh' version '2.10.1'
    id "net.ossindex.audit" version "0.4.5-beta"
    id "com.github.johnrengelman.shadow" version "4.0.3"
}

version = '2.7.0-SNAPSHOT'

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

lombok {
    version = '1.18.2'
    sha256 = ""
}

dependencies {
    checkstyle 'com.puppycrawl.tools:checkstyle:8.2'

    annotationProcessor("org.projectlombok:lombok:1.16.20")
    compileOnly("org.projectlombok:lombok:1.16.20")
    testCompileOnly("org.projectlombok:lombok:1.16.20")

    api("org.slf4j:slf4j-api:1.7.25")
    api("com.github.ev3dev-lang-java:lejos-commons:0.7.3")
    api("net.java.dev.jna:jna:4.5.2")

    testImplementation("ch.qos.logback:logback-classic:1.2.3")
    testImplementation("commons-io:commons-io:2.5")

    //TODO: Upgrade to JUnit 5
    testImplementation("junit:junit:4.12")
    //TODO: Review to add Mockito support
    testImplementation("org.hamcrest:hamcrest-all:1.3")
    testImplementation("org.assertj:assertj-core:3.18.1")
}

compileJava.options.encoding = 'UTF-8'

//Compile
compileJava {
    sourceCompatibility = 11
    targetCompatibility = 11
}

def checkstyleConfigDir = file("${rootDir}/config/checkstyle")

checkstyle {
    toolVersion = "8.20"
    configFile = new File(checkstyleConfigDir, "checkstyle.xml")
    configProperties.checkstyleConfigDir = checkstyleConfigDir
    checkstyleTest.enabled = false
}

tasks.withType(Checkstyle) {
    ignoreFailures false
    maxWarnings = 0
    source 'src/main/java'
    reports {
        xml.enabled false
        html.enabled true
        html.stylesheet resources.text.fromFile('config/xsl/checkstyle-noframes-sorted.xsl')
    }
}

test.testLogging {
    exceptionFormat "full"
}

//Coverage
jacoco {
    toolVersion = "0.8.2"
    reportsDirectory = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("${buildDir}/jacocoHtml")
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                //minimum = 0.75 // implement more tests
                minimum = 0.2
            }
        }
    }
}

test {
    jacoco {
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpDir = file("$buildDir/jacoco/classpathdumps")
    }
}

test.finalizedBy jacocoTestReport
test.finalizedBy jacocoTestCoverageVerification

//Jar
jar {
    archiveBaseName = "${rootProject.name}"
    manifest {
        from file("${projectDir}/src/main/resources/META-INF/MANIFEST.MF")
    }
}

task fatJar {
    doLast {
    }
}
fatJar.dependsOn shadowJar

apply from: './gradle/config.gradle'
apply from: './gradle/deploy.gradle'
apply from: './gradle/docker.gradle'

/* global settings */
configure(allprojects) { project ->

    compileJava.options*.compilerArgs = [
            "-Xlint:unchecked", "-Xlint:rawtypes",
    ]

    compileTestJava.options*.compilerArgs = [
            "-Xlint:deprecation", "-Xlint:unchecked", "-Xlint:rawtypes",
    ]
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier 'sources'
    from sourceSets.main.allSource
}

//TODO Review failing classes in the next release
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'com.github.ev3dev-lang-java'
            artifactId = 'ev3dev-lang-java'
            version = "${version}"

            from components.java
            artifact (sourcesJar) {
                classifier = 'sources'
            }
            //artifact (javadocJar) {
            //    classifier = 'javadoc'
            //}
        }
    }
}
