plugins {
    id "com.github.johnrengelman.shadow"
    id "modernfix.mod-common-conventions"
    id "modernfix.platform-conventions"
}

architectury {
    platformSetupLoomIde()
    forge()
}

loom {
    forge {
        convertAccessWideners = true
        extraAccessWideners.add loom.accessWidenerPath.get().asFile.name

        mixinConfig "modernfix-common.mixins.json"
        mixinConfig "modernfix-forge.mixins.json"
    }
}

configurations {
    common
    shadowCommon // Don't use shadow from the shadow plugin since it *excludes* files.
    compileClasspath.extendsFrom common
    runtimeClasspath.extendsFrom common
}

def extraModsDir = "extra-mods"

repositories {
    exclusiveContent {
        forRepository {
            flatDir {
                name "extra-mods"
                dir file(extraModsDir)
            }
        }
        filter {
            includeGroup "extra-mods"
        }
    }
}

dependencies {
    forge "net.minecraftforge:forge:${rootProject.forge_version}"
    implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:${rootProject.mixinextras_version}"))
    implementation(include("io.github.llamalad7:mixinextras-forge:${rootProject.mixinextras_version}"))
    // Remove the next line if you don't want to depend on the API
    // modApi "me.shedaniel:architectury-forge:${rootProject.architectury_version}"

    modCompileOnly("curse.maven:refinedstorage-243076:${refined_storage_version}")

    modCompileOnly("curse.maven:jeresources-240630:3951643")
    modCompileOnly("me.shedaniel:RoughlyEnoughItems-forge:${rei_version}") { transitive false }
    modCompileOnly("dev.latvian.mods:kubejs-forge:${kubejs_version}")
    //modRuntimeOnly("curse.maven:ferritecore-429235:4441949")
    modCompileOnly("curse.maven:ctm-267602:${ctm_version}")
    modCompileOnly("curse.maven:ldlib-626676:${ldlib_version}")

    modCompileOnly("curse.maven:supermartijncore-454372:4455391")
    modCompileOnly("vazkii.patchouli:Patchouli:1.19.2-77")
    modCompileOnly("curse.maven:cofhcore-69162:5374122")
    modCompileOnly("curse.maven:resourcefullib-570073:5659871")

    // runtime remapping at home
    for (extraModJar in fileTree(dir: extraModsDir, include: '*.jar')) {
        def basename = extraModJar.name.substring(0, extraModJar.name.length() - ".jar".length())
        def versionSep = basename.lastIndexOf('-')
        assert versionSep != -1
        def artifactId = basename.substring(0, versionSep)
        def version = basename.substring(versionSep + 1)
        modRuntimeOnly("extra-mods:$artifactId:$version")
    }

    common(project(path: ":common", configuration: "namedElements")) { transitive false }
    shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive = false }
    shadowCommon(project(path: ":annotations"))
    forgeRuntimeLibrary(project(path: ":annotations"))
}

processResources {
    inputs.property "version", project.version

    filesMatching("META-INF/mods.toml") {
        expand "version": project.version
    }
}

shadowJar {
    exclude "fabric.mod.json"
    exclude "architectury.common.json"

    configurations = [project.configurations.shadowCommon]
    archiveClassifier.set("dev-shadow")
}

remapJar {
    input.set shadowJar.archiveFile
    dependsOn shadowJar
    archiveClassifier.set(null)
}

jar {
    archiveClassifier.set("dev")
    manifest {
        attributes([
                "Specification-Title"     : "modernfix",
                "Operative-Class"         : "org.embeddedt.modernfix.agent.Agent",
                //"Specification-Vendor": "modernfix authors",
                "Specification-Version"   : "1", // We are version 1 of ourselves
                "Implementation-Title"    : project.name,
                "Implementation-Version"  : project.jar.archiveVersion
                //"Implementation-Vendor": "modernfix authors",
        ])
    }
}

components.java {
    withVariantsFromConfiguration(project.configurations.shadowRuntimeElements) {
        skip()
    }
}

publishing {
    publications {
        mavenForge(MavenPublication) {
            artifactId = rootProject.archives_base_name + "-" + project.name
            from components.java
        }
    }

    // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
    repositories {
        // Add repositories to publish to here.
    }
}
