import org.cyclonedx.Version

plugins {
    id 'java'
    id 'distribution'
    id 'org.springframework.boot' version '3.5.6'
    id 'io.spring.dependency-management' version '1.1.7'
    id 'org.graalvm.buildtools.native' version '0.10.6'
    id 'org.cyclonedx.bom' version '3.0.0'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
        vendor = JvmVendorSpec.of("GRAALVM_COMMUNITY")
    }
}

repositories {
    mavenCentral()
}

ext {
    set('jSpecifyVersion', "1.0.0")
    set('springShellVersion', "3.4.1")
}

dependencies {
    implementation platform("org.springframework.shell:spring-shell-dependencies:${springShellVersion}")

    implementation "org.jspecify:jspecify:${jSpecifyVersion}"
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.shell:spring-shell-starter-jni'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.shell:spring-shell-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

springBoot {
    buildInfo()
}

graalvmNative {
    binaries {
        main {
            imageName = "arconia"
            // See: https://docs.spring.io/spring-shell/reference/building.html#_ffm
            buildArgs.add('--enable-native-access=ALL-UNNAMED')
        }
    }
}

tasks.withType(JavaExec).configureEach {
   // See: https://docs.spring.io/spring-shell/reference/building.html#_ffm
   jvmArgs += ["--enable-native-access=ALL-UNNAMED"]
}

tasks.withType(Test).configureEach {
   // See: https://docs.spring.io/spring-shell/reference/building.html#_ffm
   jvmArgs += ["--enable-native-access=ALL-UNNAMED"]
}

tasks.named('test') {
    useJUnitPlatform()
}

cyclonedxDirectBom {
    includeConfigs = ["runtimeClasspath"]
    projectType = "application"
    schemaVersion = Version.VERSION_16
}

distributions {
    main {
        contents {
            from("$buildDir/native/nativeCompile") {
                into 'bin'
                include 'arconia', 'arconia.exe'
            }
            from("$buildDir/reports/cyclonedx") {
                include 'bom.json'
                rename { 'sbom.cdx.json' }
            }
            from(rootDir) {
                include 'LICENSE', 'README.md'
            }
        }
    }
}

tasks.named('distZip') {
    dependsOn('cyclonedxBom')
    def archiveName = gradle.startParameter.projectProperties['archiveName'] ?: "arconia-${project.version}.zip"
    onlyIf {
        gradle.startParameter.taskNames.contains("distZip")
    }
    archiveFileName = "${archiveName}"
}

tasks.named('distTar') {
    enabled = false
}
