plugins {
    id "architectury-plugin" version "3.4-SNAPSHOT"
    id "dev.architectury.loom" version "1.9-SNAPSHOT" apply false
    id "maven-publish"
    id 'com.matthewprenger.cursegradle' version '1.4.0' apply false
    id 'com.palantir.git-version' version '1.0.0'
    id 'org.ajoberstar.grgit' version '5.2.0'
    id 'se.bjurr.gitchangelog.git-changelog-gradle-plugin' version '1.79.0'
    id "com.modrinth.minotaur" version "2.+" apply false
    id("com.diffplug.spotless") version "6.25.0" apply false
    id 'modernfix.common-conventions' apply false
}

architectury {
    minecraft = rootProject.minecraft_version
}

ext.archives_base_name = 'modernfix'

apply plugin: 'modernfix.common-conventions'

tasks.withType(JavaCompile).configureEach {
    // ensure that the encoding is set to UTF-8, no matter what the system default is
    // this fixes some edge cases with special characters not displaying correctly
    // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
    // If Javadoc is generated, this must be specified in that task too.
    options.encoding = "UTF-8"

    // The Minecraft launcher currently installs Java 8 for users, so your mod probably wants to target Java 8 too
    // JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used.
    // We'll use that if it's available, but otherwise we'll use the older option.
    def targetVersion = 8
    /*
    if (JavaVersion.current().isJava9Compatible()) {
        options.release = targetVersion
    }
     */
}

tasks.register('generateChangelog', se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
    def details = versionDetails();
    def theVersionRef
    if (details.commitDistance > 0) {
        theVersionRef = details.lastTag;
    } else {
        def secondLastTagCmd = "git describe --abbrev=0 " + details.lastTag + "^"
        def secondLastTag = secondLastTagCmd.execute().text.trim()
        theVersionRef = secondLastTag;
    }

    fromRef = theVersionRef

    file = new File("${rootDir}/CHANGELOG.md");
    templateContent = new File("${rootDir}/gradle/changelog.mustache").getText('UTF-8').replace("[[modernFixVersionRef]]", theVersionRef);
    toCommit = "HEAD";
}

tasks.register('checkCleanTag') {
    doLast {
        def details = versionDetails()
        if (!details.isCleanTag || versionDetails().commitDistance != 0) {
            throw new GradleException('Not a clean tree.')
        }
    }
}

println "ModernFix: " + version
