plugins {
    id "pl.allegro.tech.build.axion-release" version "1.20.1"
    id "org.springframework.boot" version "3.5.5"
    id "org.sonarqube" version "6.3.1.5724"
    id 'com.google.cloud.tools.jib' version '3.4.5'
    id "com.github.node-gradle.node" version "7.1.0"
    id "com.github.ben-manes.versions" version "0.52.0"
}

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "io.spring.dependency-management"
apply plugin: "jacoco"
apply plugin: "org.sonarqube"

scmVersion {
    tag {
        prefix = ""
    }
}

group = "de.gessnerfl"
description = """Fake SMTP Server :: Simple SMTP Server which stores emails in memory and lists email in a web ui"""
project.version = project.findProperty("projectVersion") ?: scmVersion.version
project.archivesBaseName = "fake-smtp-server"

ext.scmUrl = "git@github.com:gessnerfl/fake-smtp-server.git"
ext.projectUrl = "https://github.com/gessnerfl/fake-smtp-server"

sourceCompatibility = 21
targetCompatibility = 21

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-mail")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("commons-io:commons-io:2.20.0")
    implementation("org.yaml:snakeyaml:2.5")
    implementation('org.flywaydb:flyway-core:11.11.2')
    implementation("org.glassfish.jaxb:jaxb-runtime:4.0.5")
    implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.20.0")
    implementation('org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.11')

    runtimeOnly("com.h2database:h2:2.3.232")
    runtimeOnly("io.micrometer:micrometer-registry-prometheus:1.15.3")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.ow2.asm:asm:9.8")
    testImplementation("org.apache.commons:commons-lang3:3.18.0")
    testImplementation("org.hamcrest:hamcrest:3.0")
}

java {
    withSourcesJar()
    toolchain { languageVersion = JavaLanguageVersion.of(21) }
}

bootRun {
    // für Spring Boot Gradle Plugin >=2.5
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:deprecation"
        options.compilerArgs << "-Xlint:unchecked"
    }
}

node {
    nodeProjectDir = file("${project.projectDir}/webapp")
    npmInstallCommand = System.getenv("CI") ? 'ci' : 'install'
    download = true
    version = "24.7.0"
    npmVersion = "11.5.1"
}

tasks.register('webappClean', Delete) {
    delete file("${project.projectDir}/webapp/build")
    delete file("${project.projectDir}/webapp/dist")
    delete file("${project.projectDir}/webapp/coverage")
}
clean.dependsOn webappClean

tasks.register('webappNpmBuild', NpmTask) {
    dependsOn(npmInstall)
    args = ["run", "build"]
}

tasks.register('webappNpmTest', NpmTask) {
    dependsOn(webappNpmBuild)
    args = ["run", "test", "--", "--coverage", "--watchAll=false"]
}

tasks.register('copyStaticAssets', Copy) {
    dependsOn(compileJava, compileTestJava, webappNpmTest, resolveMainClassName)
    from(file('webapp/dist'))
    into(file('build/resources/main/static/'))
    exclude("index.html")
}

tasks.register('copyTemplateAssets', Copy) {
    dependsOn(compileJava, compileTestJava, webappNpmTest, resolveMainClassName)
    from(file('webapp/dist/index.html'))
    into(file('build/resources/main/templates/'))
    filter { line -> line.replaceAll(/(href|src)(=\")([^\"]+)(\")/, 'th:$1$2@{$3}$4').replaceAll(/(name="base-path"\s+)(content=")([^"]+)(")/, '$1th:$2@{$3}$4') }
}

jar {
    dependsOn(copyStaticAssets, copyTemplateAssets)
}

bootJar {
    dependsOn(copyStaticAssets, copyTemplateAssets)
}

springBoot {
    buildInfo {
        properties {}
    }
}

sonar {
    properties {
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.organization", "gessnerfl-github"
        property "sonar.projectName", "Fake SMTP Server"
        property "sonar.projectKey", "de.gessnerfl.fake-smtp-server"
        property "sonar.sources", "src/main,webapp/src"
        property "sonar.test.inclusions", "src/test/**/*.*, webapp/src/**/*.spec.ts, webapp/src/**/*.spec.tsx"
        property "sonar.javascript.lcov.reportPaths", "./webapp/coverage/lcov.info"
        property "sonar.coverage.exclusions", "webapp/src/setupTests.ts"
    }
}

test {
    dependsOn(copyStaticAssets, copyTemplateAssets)
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

tasks.register('copyJacocoReports', Copy) {
    from(file('build/reports/jacoco/test/jacocoTestReport.xml'))
    into(file('.qodana/code-coverage/'))
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.required = true
    }
    finalizedBy copyJacocoReports
}

jib {
    from {
        image = 'amazoncorretto:21.0.8-al2023-headless'
        container {
            ports = ["8080", "8081", "8025"]
            creationTime = 'USE_CURRENT_TIMESTAMP'
            jvmFlags = ['-Djava.security.egd=file:/dev/./urandom', '-Dfile.encoding=utf-8']
        }
        extraDirectories {
            paths {
                path {
                    from = 'webapp/dist'
                    into = '/app/resources/static'
                }
            }
        }
        platforms {
            platform {
                architecture = 'amd64'
                os = 'linux'
            }
            platform {
                architecture = 'arm64'
                os = 'linux'
            }
        }
    }
    to {
        image = 'gessnerfl/fake-smtp-server'
        tags = [scmVersion.version, 'latest']
        auth {
            username = DOCKERHUB_USERNAME
            password = DOCKERHUB_TOKEN
        }
    }
}
