import net.fabricmc.loom.api.mappings.layered.MappingsNamespace
import net.fabricmc.loom.task.RemapJarTask
import net.fabricmc.loom.task.RemapSourcesJarTask

apply plugin: "dev.architectury.loom"

evaluationDependsOn ':xplat'

dependencies {
	minecraft "com.mojang:minecraft:${rootProject.minecraft_version}"
	mappings loom.officialMojangMappings()
}

def mojmapJar = tasks.register('mojmapJar', RemapJarTask, {
	classpath.from loom.getMinecraftJarsCollection(MappingsNamespace.INTERMEDIARY)
	def remapJar = project(':xplat').tasks.named('remapJar', RemapJarTask)
	dependsOn remapJar

	inputFile.convention(remapJar.flatMap {it.archiveFile })
	sourceNamespace = 'intermediary'
	targetNamespace = 'named'
	
	remapperIsolation = true
})

def mojmapSourcesJar = tasks.register('mojmapSourcesJar', RemapSourcesJarTask, {
	classpath.from loom.getMinecraftJarsCollection(MappingsNamespace.INTERMEDIARY)
	def filteredSourcesJar = project(':xplat').tasks.named('filteredSourcesJar', Jar)
	dependsOn filteredSourcesJar
	
	archiveClassifier = 'sources'

	inputFile.convention(filteredSourcesJar.flatMap {it.archiveFile })
	sourceNamespace = 'intermediary'
	targetNamespace = 'named'
	
	remapperIsolation = true
})

def mojmapApiJar = tasks.register('mojmapApiJar', RemapJarTask, {
	classpath.from loom.getMinecraftJarsCollection(MappingsNamespace.INTERMEDIARY)
	def apiJar = project(':xplat').tasks.named('apiJar', Jar)
	dependsOn apiJar
	
	archiveClassifier = 'api'

	inputFile.convention(apiJar.flatMap {it.archiveFile })
	sourceNamespace = 'intermediary'
	targetNamespace = 'named'
	
	remapperIsolation = true
})

tasks.named("build").configure {
	dependsOn(mojmapJar, mojmapSourcesJar, mojmapApiJar)
}

publishing {
	publications {
		register('maven', MavenPublication) {
			artifactId = "${rootProject.name}-xplat-mojmap"
			version = project.ext.mavenVersion
			artifact(mojmapJar) {
				builtBy mojmapJar
				classifier = ''
			}
			artifact(mojmapSourcesJar) {
				builtBy mojmapSourcesJar
				classifier = 'sources'
			}
			artifact(mojmapApiJar) {
				builtBy mojmapApiJar
				classifier = 'api'
			}
		}
	}
	setupRepositories(repositories)
}

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