
plugins {
  id 'java-library'
  id 'checkstyle'
  id 'distribution'
  id 'com.github.spotbugs'  version '6.1.7'
  id 'com.diffplug.spotless' version '7.0.3'
  id 'com.github.ben-manes.versions' version '0.52.0'
  id 'com.formkiq.gradle.graalvm-native-plugin' version '1.6.0'
  id 'de.undercouch.download' version '5.6.0'
  id 'org.openapi.generator' version '7.12.0'
}

def moduleName = "formkiq-core"

def getCmd() {
    String os = System.getProperty("os.name").toLowerCase()
    return os.contains("win") ? "cmd" : "bash"
}

def getCmdParam() {
    String os = System.getProperty("os.name").toLowerCase()
    return os.contains("win") ? "/c" : "-c"
}

repositories { mavenCentral() }

allprojects {

  version = '1.17.2'
  
  ext.awsCognitoVersion = '1.6.0'
  group = 'com.formkiq.stacks'
  
	apply plugin: 'com.diffplug.spotless'
    apply plugin: 'java-library'
    apply plugin: 'checkstyle'
    apply plugin: 'com.github.spotbugs'
    apply plugin: 'com.github.ben-manes.versions'
    apply plugin: 'com.formkiq.gradle.graalvm-native-plugin'
	
	java {
		toolchain {
			languageVersion.set(JavaLanguageVersion.of(17))
		}
	}
  
	spotless { 
	  java { 
	    eclipse().configFile project.rootProject.file("spotless.eclipseformat.xml")
	    licenseHeaderFile project.rootProject.file("LICENSE")  
	  } 
	}

    checkstyle {
        toolVersion = '10.12.1'
        configFile file("config/checkstyle/checkstyle.xml")
        configProperties = [project_loc: "${projectDir}"]
        maxWarnings = 0
        maxErrors = 0
    }

	spotlessJavaCheck.dependsOn 'spotlessJavaApply'
}

subprojects {

  repositories {
  	mavenLocal()
    mavenCentral()
    maven {
      url "https://central.sonatype.com/repository/maven-snapshots/"
    }
  }
  
  compileJava.dependsOn 'spotlessCheck'

  tasks.withType(Test) {
    systemProperties['testregion'] = project.getProperty('testregion')
    systemProperties['testprofile'] = project.getProperty('testprofile')
    systemProperties['testappenvironment'] = project.getProperty('testappenvironment')
    systemProperties['testchatgptapikey'] = project.getProperty('testchatgptapikey')
  }

  spotbugs {
    excludeFilter = file("$rootDir/config/gradle/spotbugs-exclude.xml")
  }
     
  spotbugsMain {
    reports {
        html {
            enabled = true
        }
    }
  } 
  
  spotbugsTest {
    reports {
        html {
            enabled = true
        }
    }
  }
}

description = "FormKiQ Core"

apply plugin: 'distribution'

task buildDistribution(type: Copy) {
	dependsOn subprojects.build
	duplicatesStrategy = DuplicatesStrategy.EXCLUDE
	outputs.upToDateWhen {false}

    from 'console/build/distributions'
    from 'lambda-api-graalvm/build/distributions'
    from 'lambda-s3-graalvm/build/distributions'
    from 'lambda-typesense/build/distributions'
    from 'lambda-ocr-tesseract/build/distributions'
    from 'module-email-notify/build/distributions'
    from 'websocket-api/build/distributions'
    from 'lambda-apikey-authorizer/build/distributions'
    into "${buildDir}/modules"
}

task downloadAwsCognito(type: Download) {
    src "https://github.com/formkiq/aws-cognito/releases/download/v${awsCognitoVersion}/aws-cognito-v${awsCognitoVersion}.zip"
    dest buildDir
    overwrite false
}

/*
task downloadAwsCognito(type: Copy) {
    dependsOn jar
    from file("$buildDir/../../../github/aws-cognito/build/aws-cognito-v${awsCognitoVersion}.zip")
    into file("${buildDir}")
}*/

task unzipAwsCognito(type: Copy) {
  dependsOn downloadAwsCognito
  def zipFile = file("${buildDir}/aws-cognito-v${awsCognitoVersion}.zip")
  def outputDir = file("${buildDir}/modules/cognito")

  from zipTree(zipFile)
  into outputDir
}

task buildTemplate {
    dependsOn buildDistribution, unzipAwsCognito
    
	outputs.upToDateWhen { false }
    
	doLast {	
		copy {
		  from layout.buildDirectory.dir("${buildDir}/../src/main/resources/cloudformation")
		  include "*"
	      into "${buildDir}/modules"
	    }
		exec {
			commandLine getCmd(), getCmdParam(), "ytt --data-value version=${project.version} -f ${buildDir}/modules/template.yaml --output-files ${buildDir}/modules"
		}	
	}
}

distZip.dependsOn buildTemplate

tasks.distTar.enabled = false

distributions {
    main {
        contents {
            from(".") {
            	include "INSTALL.md"
            	include "LICENSE"
            }
            from("${buildDir}/modules") {
            	include "**/**"
            }
            into '/'
        }
    }
}

task validateOpenApiJwtSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
    inputSpec = "$rootDir/docs/openapi/openapi-jwt.yaml".toString()
    recommend = true
}

task validateOpenApiIamSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
    inputSpec = "$rootDir/docs/openapi/openapi-iam.yaml".toString()
    recommend = true
}

task validateOpenApiKeySpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
    inputSpec = "$rootDir/docs/openapi/openapi-key.yaml".toString()
    recommend = true
}

task validateOpenApiSpecs(dependsOn: ['validateOpenApiJwtSpec', 'validateOpenApiIamSpec', 'validateOpenApiKeySpec'])

openApiGenerate {
    generatorName.set("spring")
    inputSpec.set("$rootDir/docs/openapi/openapi-jwt.yaml")
    outputDir.set("$buildDir/generated")
    apiPackage.set("com.formkiq.springboot.api")
    invokerPackage.set("com.formkiq.springboot.invoker")
    modelPackage.set("com.formkiq.springboot.model")
}
