import java.nio.file.Files
import java.nio.file.StandardCopyOption

description = "Apereo CAS Core Configuration Model"
def skipConfigurationMetadata = providers.systemProperty("skipNestedConfigMetadataGen").present

sourceSets.test.resources.srcDirs += sourceSets.main.resources.srcDirs

sourceSets {
    configurationMetadata {
        resources {
            srcDir 'src/config'
        }
    }
}

dependencies {
    implementation libraries.jasypt
    implementation libraries.bouncycastle
    implementation libraries.springbootconfigmetadata
    implementation libraries.jose4j
    implementation libraries.classgraph
    
    testImplementation libraries.springbootconfigmetadata

    testImplementation project(":api:cas-server-core-api-audit")

    compileOnly libraries.javaparser
    compileOnly libraries.log4j

    if (!skipConfigurationMetadata) {
        annotationProcessor libraries.springbootconfigmetadata
    }

    testImplementation project(path: ":core:cas-server-core-util-api", configuration: "tests")
}

abstract class ConfigurationMetadataLocationProvider implements CommandLineArgumentProvider {
    @InputFile
    @PathSensitive(PathSensitivity.RELATIVE)
    abstract RegularFileProperty getInputSpringConfigurationMetadata()

    @OutputFile
    abstract RegularFileProperty getOutputSpringConfigurationMetadata()
    
    @Override
    Iterable<String> asArguments() {
        [ inputSpringConfigurationMetadata.get().asFile.canonicalPath, outputSpringConfigurationMetadata.get().asFile.canonicalPath ]
    }
}

if (!skipConfigurationMetadata) {
    def outputFile= project.layout.buildDirectory.file("generated/spring-configuration-metadata/META-INF/spring-configuration-metadata.json")
    def inputFile= project.layout.buildDirectory.file("classes/java/main/META-INF/spring-configuration-metadata.json")

    tasks.register('generateConfigurationMetadata', JavaExec) {
        dependsOn("processResources", "compileConfigurationMetadataJava", "processConfigurationMetadataResources")

        def sortedClasspath = sourceSets.main.compileClasspath + sourceSets.main.runtimeClasspath + sourceSets.configurationMetadata.runtimeClasspath
        classpath = files(sortedClasspath.sort())
        mainClass = "org.apereo.cas.configuration.metadata.ConfigurationMetadataGenerator"
        argumentProviders.add(
            objects.newInstance(ConfigurationMetadataLocationProvider).tap {
                inputSpringConfigurationMetadata = inputFile
                outputSpringConfigurationMetadata = outputFile
            }
        )

        inputs.files(inputFile)
                .withPropertyName('configurationMetadataGeneration')
                .withPathSensitivity(PathSensitivity.RELATIVE)

        outputs.cacheIf { true }
        outputs.file(outputFile)

        finalizedBy("copyConfigurationMetadata")
    }

    tasks.register('copyConfigurationMetadata') {
        dependsOn tasks.named("generateConfigurationMetadata")
        doLast {
            Files.copy(
                    outputFile.get().asFile.toPath(),
                    inputFile.get().asFile.toPath(),
                    StandardCopyOption.REPLACE_EXISTING
            )
        }
        outputs.doNotCacheIf("Caching disabled for copying, as well as build correctness.") { true }
    }

    tasks.named('compileJava') {
        inputs.files(tasks.named('processResources'))
                .withPropertyName('configurationMetadataResources')
                .withPathSensitivity(PathSensitivity.RELATIVE)
        finalizedBy("generateConfigurationMetadata")
    }

    jar {
        dependsOn tasks.named("copyConfigurationMetadata")
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
    }
}
