import java.text.SimpleDateFormat

plugins {
    id("com.github.johnrengelman.shadow") version "8.1.1"
    id 'java'
}

version = '2.1.25'
java {
    sourceCompatibility = JavaVersion.VERSION_1_8
}

jar {
    archiveBaseName = 'rulekit'
    manifest {
        attributes 'Implementation-Version': version
        attributes 'Main-Class': 'adaa.analytics.rules.consoles.ExperimentalConsole'
    }
}

repositories {
    mavenCentral()
}


sourceSets {
    test {
        java {
            srcDir 'test'
        }
    }
    main {
        java {
            srcDir 'src'
        }
    }
}

dependencies {
    implementation group: 'commons-cli', name: 'commons-cli', version: '1.6.0'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.16.1'
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0'
    implementation group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
    implementation group: 'com.intellij', name: 'annotations', version: '12.0'

    implementation group: 'tech.tablesaw', name: 'tablesaw-core', version: '0.43.1'
    implementation group: 'com.google.guava', name: 'guava', version: '33.0.0-jre'

    implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'

    testImplementation group: 'junit', name: 'junit', version: '4.+'
}

shadowJar {
    zip64 true
}


task rjar(type: Jar) {
    archiveBaseName = 'rulekit'
    archiveClassifier = 'all'
    manifest {
        attributes 'Implementation-Version': version
        attributes 'Main-Class': 'adaa.analytics.rules.consoles.ExperimentalConsole'
    }

    from {
        configurations.runtimeClasspath.filter({ !(it.name =~ /.*\.pom/) }).collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude('**/*.SF')
        exclude('**/*.DSA')
        exclude('**/*.RSA')
    }
    duplicatesStrategy(DuplicatesStrategy.INCLUDE)
    with jar
    zip64 true
}


task generateVersionProperties {
    doLast {
        def propertiesFile = file "$buildDir/resources/main/version.properties"
        propertiesFile.parentFile.mkdirs()
        def properties = new Properties()
        properties.setProperty("version", rootProject.version.toString())
        properties.setProperty("date", new SimpleDateFormat("dd.MM.yyyy").format(new Date()))
        properties.setProperty("commitHash", ['git', 'rev-parse', 'HEAD'].execute().text.trim())
        properties.setProperty("commitDate", ['git', 'log', '-1', '--format=%ct', '--date=local'].execute().text.trim())
        propertiesFile.withWriter { properties.store(it, null) }
    }
}
processResources.dependsOn generateVersionProperties

classes {
    dependsOn generateVersionProperties
}
