description = "FormKiQ Core - Lambda TypeSense"

apply plugin: 'distribution'

def moduleName = "formkiq-module-lambda-typesense"
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"
}

dependencies {

 	annotationProcessor group: 'com.formkiq', name: 'graalvm-annotations-processor', version: '1.4.2'
 
	implementation project(':aws-dynamodb')
	implementation project(':fkq-lambda-services')
	implementation project(':dynamodb-documents')
	implementation project(':typesense')
	implementation project(':http')
	
	implementation group: 'com.amazonaws', name: 'aws-lambda-java-core', version: '1.2.3'
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.13.0'
    
	implementation group: 'com.formkiq', name: 'lambda-runtime-graalvm', version:'2.5.0'
    implementation group: 'com.formkiq', name: 'graalvm-annotations', version: '1.2.0'
    
	implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.17'
	testImplementation project(':fkq-test-utils')

	testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version:'5.11.4'
    testImplementation group: 'org.testcontainers', name: 'testcontainers', version: '1.20.6'
	testImplementation group: 'org.testcontainers', name: 'junit-jupiter', version: '1.20.6'
    testImplementation group: 'org.testcontainers', name: 'localstack', version: '1.20.6'
}

compileJava {
    options.annotationProcessorPath += configurations.runtimeClasspath
}

nativeImage { 
	outputFileName = "server"
	dockerImage = "ghcr.io/graalvm/graalvm-ce:ol9-java17-22.3.3"
	mainClassName = "com.formkiq.lambda.runtime.graalvm.LambdaRuntime"
    enableHttp = true
    enableHttps = true
    enableStatic = true
	systemProperty = ["java.net.preferIPv4Stack=true"]
}

test {
  failFast = true
  useJUnitPlatform()
}

task buildTemplate {
	inputs.files("src/main/resources/cloudformation/typesense/template.yaml")
	outputs.files("${buildDir}/distributions/typesense/template.yaml")
  
	def randomtext = ('0'..'z').shuffled().take(10).join();
	def sha256 = randomtext.digest('SHA-256')
	
  	doLast {
		exec {
			commandLine getCmd(), getCmdParam(), "ytt --data-value hash=${sha256} -f src/main/resources/cloudformation/typesense/template.yaml > ${buildDir}/distributions/typesense/template.yaml"
		}
	}
}

distZip.dependsOn buildTemplate, graalvmNativeImage

tasks.distTar.enabled = false

distZip {
    archiveFileName = "typesense/${moduleName}-graalvm.zip"
}

distributions {
    main {
        contents {
            from("runtime/") {
            	include "bootstrap"
            }
            from ("${buildDir}/graalvm") {
                include("server")
            }
            into '/'
        }
    }
}
