plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.4.10'
    id 'application'
    id "com.palantir.graal" version "0.7.1-20-g113a84d"
}

def mainCommandClass = "picocli.examples.kotlin.Checksum"
group 'picocli.examples.kotlin'
version '1.0-SNAPSHOT'

//com.palantir.graal (graalVm native image) supports only jdk 8 or 11
sourceCompatibility = 8

application{
    mainClass.set(mainCommandClass)
}


repositories {
    mavenCentral()
}

run{
    standardInput = System.in
}

test {
    useJUnitPlatform()
}

jar{
    manifest {
        attributes "Main-Class": mainCommandClass
    }
}

task uberJar(type: Jar) {
    manifest {
        attributes "Main-Class": mainCommandClass
    }
    archiveClassifier = 'uberJar'
    from sourceSets.main.output
    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testImplementation  'org.jetbrains.kotlin:kotlin-test-junit5:1.4.10'

    testImplementation "org.junit.jupiter:junit-jupiter:5.7.0"
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
}


compileKotlin {
    kotlinOptions.jvmTarget = "$sourceCompatibility"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "$sourceCompatibility"
}

graal {
    //see https://github.com/palantir/gradle-graal
    mainClass mainCommandClass
    outputName 'checksum'
    javaVersion "$sourceCompatibility".replace("1.", "")
    //    option "--no-fallback"
}


configurations {
    generateConfig
}
dependencies {
    implementation 'info.picocli:picocli:4.6.2'
    generateConfig 'info.picocli:picocli-codegen:4.6.2'
}

task(generateGraalReflectionConfig, dependsOn: 'classes', type: JavaExec) {
    main = 'picocli.codegen.aot.graalvm.ReflectionConfigGenerator'
    classpath = configurations.generateConfig + sourceSets.main.runtimeClasspath
    def outputFile = "${buildDir}/resources/main/META-INF/native-image/${project.group}/${project.name}/reflect-config.json"
    args = ["--output=$outputFile", mainCommandClass]
}
tasks.nativeImage.dependsOn(generateGraalReflectionConfig)

task(generateGraalDynamicProxyConfig, dependsOn: 'classes', type: JavaExec) {
    main = 'picocli.codegen.aot.graalvm.DynamicProxyConfigGenerator'
    classpath = configurations.generateConfig + sourceSets.main.runtimeClasspath
    def outputFile = "${buildDir}/resources/main/META-INF/native-image/${project.group}/${project.name}/proxy-config.json"
    args = ["--output=$outputFile", mainCommandClass]
}
tasks.nativeImage.dependsOn(generateGraalDynamicProxyConfig)