plugins {
    id 'java-library'
    id 'org.dmfs.gver' version '0.18.0'
    id 'io.github.gradle-nexus.publish-plugin' version '1.1.0' apply false
}

gver {
    issueTracker GitHub {
        repo = "dmfs/oauth2-essentials"
        if (project.hasProperty("GITHUB_API_TOKEN")) {
            accessToken = GITHUB_API_TOKEN
        }
    }
    changes {
        are none when {
            affects only(matches(~/.*\.md/))
        }
        are major when {
            commitMessage contains(~/(?i)#(major|break(ing)?)\b/)
        }
        are minor when {
            commitMessage contains(~/(?i)#(?<issue>\d+)\b/) {
                where("issue") { isIssue { labeled "enhancement" } }
            }
        }
        are patch when {
            commitMessage contains(~/(?i)#(?<issue>\d+)\b/) {
                where("issue") { isIssue { labeled "bug" } }
            }
        }
        are minor when {
            commitMessage contains("#feature\\b")
        }
        otherwise patch
    }
    preReleases {
        on ~/main/ use { "beta" }
        on ~/(.*\/)?(?<name>.*)/ use { "alpha-${group('name')}.1" }
    }
    releaseBranchPattern ~/main$/
}

sourceSets {
    main {
        java {
            srcDirs 'src/main/java'
            srcDirs 'build/generated/java'
        }
    }
}

group 'org.dmfs'

tasks.withType(Jar) {
    eachFile {
        it.duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
}

sourceCompatibility = 1.8

configurations {
    pom
}

apply from: 'publish.gradle'
apply from: 'jacoco.gradle'

repositories {
    mavenCentral()
}

import org.apache.tools.ant.filters.ReplaceTokens

task generateSources(type: Copy) {
    from 'src/templates/java'
    into "${buildDir}/generated/java"
    filter(ReplaceTokens, tokens: [
            'NAME'   : "${project.name}".toString(),
            'VERSION': "${project.version}".toString()
    ])

}
compileJava.dependsOn generateSources

if (project.hasProperty('SONATYPE_USERNAME') && project.hasProperty('SONATYPE_PASSWORD')) {
    apply plugin: 'io.github.gradle-nexus.publish-plugin'

    nexusPublishing {
        repositories {
            sonatype {
                username = SONATYPE_USERNAME
                password = SONATYPE_PASSWORD
            }
        }
    }
}

dependencies {
    api 'org.dmfs:jems:1.44'
    api 'org.dmfs:rfc3986-uri:0.8.1'
    api 'org.dmfs:rfc5545-datetime:0.3'
    api 'org.dmfs:http-client-essentials:' + HTTP_CLIENT_ESSENTIALS_VERSION

    implementation 'net.iharder:base64:2.3.9'
    implementation 'org.dmfs:http-client-headers:' + HTTP_CLIENT_ESSENTIALS_VERSION
    implementation 'org.dmfs:http-client-types:' + HTTP_CLIENT_ESSENTIALS_VERSION
    implementation 'org.dmfs:http-client-basics:' + HTTP_CLIENT_ESSENTIALS_VERSION
    implementation 'org.dmfs:http-executor-decorators:' + HTTP_CLIENT_ESSENTIALS_VERSION
    implementation 'org.dmfs:express-json:0.2.0'
    implementation 'org.dmfs:jems2:' + JEMS2_VERSION
    implementation 'org.json:json:20230227'

    testImplementation 'org.saynotobugs:confidence-core:0.8.0'
    testImplementation 'org.saynotobugs:confidence-mockito4:0.8.0'
    testImplementation 'org.dmfs:jems2-testing:' + JEMS2_VERSION
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:4.8.0'
    testImplementation 'org.hamcrest:hamcrest-library:2.2'
    testImplementation 'org.dmfs:http-client-mockutils:' + HTTP_CLIENT_ESSENTIALS_VERSION
    testImplementation 'org.jmockit:jmockit:1.49'
}
