import de.undercouch.gradle.tasks.download.Download
import org.gradle.crypto.checksum.Checksum

plugins {
    id 'org.springframework.boot' version '3.2.2'
    id 'io.spring.dependency-management' version '1.1.0'
    id "com.gorylenko.gradle-git-properties" version "2.3.2"
    id "checkstyle"
    id 'java'
    id 'jacoco'
    id "de.undercouch.download" version "5.3.1"
    id "io.freefair.lombok" version "8.4"
    id 'org.gradle.crypto.checksum' version '1.4.0'
}

group = 'run.halo.app'
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

checkstyle {
    toolVersion = "9.3"
    showViolations = false
    ignoreFailures = false
}

repositories {
    mavenCentral()

    maven { url 'https://repo.spring.io/milestone' }
    maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
}


configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

springBoot {
    buildInfo {
        properties {
            artifact = 'halo'
            name = 'halo'
        }
    }
}

bootJar {
    archiveBaseName = 'halo'
    manifest {
        attributes 'Implementation-Title': 'Halo Application',
                'Implementation-Vendor': 'Halo OSS Team'
    }
}

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

dependencies {
    implementation project(':api')

    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    annotationProcessor "org.springframework:spring-context-indexer"

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'io.projectreactor:reactor-test'
}

tasks.register('createChecksums', Checksum) {
    dependsOn tasks.named('bootJar')
    inputFiles.setFrom(layout.buildDirectory.files('libs'))
    outputDirectory = layout.buildDirectory.dir("libs")
    checksumAlgorithm = Checksum.Algorithm.SHA256
}

tasks.named('build') {
    dependsOn tasks.named('createChecksums')
}

tasks.named('test', Test) {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

tasks.named('jacocoTestReport', JacocoReport) {
    reports {
        xml.required = true
        html.required = false
    }
}

tasks.register('downloadPluginPresets', Download) {
    doFirst {
        delete 'src/main/resources/presets/plugins'
    }
    src([
            'https://github.com/halo-dev/plugin-comment-widget/releases/download/v1.8.0/plugin-comment-widget-1.8.0.jar',
            'https://github.com/halo-dev/plugin-search-widget/releases/download/v1.2.0/plugin-search-widget-1.2.0.jar',
            'https://github.com/halo-dev/plugin-sitemap/releases/download/v1.1.1/plugin-sitemap-1.1.1.jar',
            'https://github.com/halo-dev/plugin-feed/releases/download/v1.2.0/plugin-feed-1.2.0.jar',
            'https://github.com/halo-dev/plugin-app-store/releases/download/v1.0.0-beta.2/plugin-app-store-1.0.0-beta.2.jar'
    ])
    dest 'src/main/resources/presets/plugins'
}
