import net.fabricmc.loom.task.RemapJarTask
import net.fabricmc.loom.task.RemapSourcesJarTask

apply plugin: "dev.architectury.loom"

architectury {
	common(rootProject.enabled_platforms.split(","))
}

loom {
	accessWidenerPath = file("src/main/resources/emi.accesswidener")
}

dependencies {
	minecraft "com.mojang:minecraft:${rootProject.minecraft_version}"
	mappings "net.fabricmc:yarn:${rootProject.yarn_mappings}:v2"

	modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"

	modCompileOnly("mezz.jei:${rootProject.jei_version}") {
		transitive = false
	}
}

processResources {
	inputs.property "version", project(":fabric").version

	filesMatching("fabric.mod.json") {
		expand "version": project(":fabric").version
	}
}

afterEvaluate {
	configurations.default.extendsFrom = [configurations.namedElements]
}

def filteredSourcesJar = tasks.register('filteredSourcesJar', Jar, {
	archiveClassifier = 'filtered-sources'
	def remapSourcesJar = tasks.named('remapSourcesJar', RemapSourcesJarTask)
	dependsOn remapSourcesJar
	from zipTree(remapSourcesJar.flatMap { it.archiveFile })
	exclude 'dev/emi/emi/jemi/**'
})

def apiJar = tasks.register('apiJar', Jar, {
	archiveClassifier = "api"
	def remapJar = tasks.named('remapJar', RemapJarTask)
	dependsOn remapJar
	from zipTree(remapJar.flatMap { it.archiveFile })
	include 'fabric.mod.json'
	include 'emi.mixins.json'
	include 'emi.accesswidener'
	include 'dev/emi/emi/api/**'
	exclude 'dev/emi/emi/api/EmiRecipeHandler**'
	exclude 'dev/emi/emi/api/stack/FluidEmiStack**'
	exclude 'dev/emi/emi/api/stack/ItemEmiStack**'
	exclude 'dev/emi/emi/api/stack/EmptyEmiStack**'
	exclude 'dev/emi/emi/api/stack/TagEmiIngredient**'
	exclude 'dev/emi/emi/api/stack/ListEmiIngredient**'
})

tasks.named("build").configure {
	dependsOn(filteredSourcesJar, apiJar)
}

publishing {
	publications {
		register('maven', MavenPublication) {
			artifactId = "${rootProject.name}-xplat-intermediary"
			version = project.ext.mavenVersion
			artifact(remapJar) {
				builtBy remapJar
				classifier = null
			}
			artifact(filteredSourcesJar) {
				builtBy filteredSourcesJar
				classifier = 'sources'
			}
			artifact(apiJar) {
				builtBy apiJar
				classifier = 'api'
			}
		}
	}
	setupRepositories(repositories)
}

void setupRepositories(RepositoryHandler repositories) {
	if (project.hasProperty("mavenUrl")) {
		repositories.maven {
			url project.mavenUrl
			credentials {
				username project.mavenUsername
				password project.mavenPassword
			}
		}
	}
}