plugins {
	id 'java-library'
	id 'maven-publish'
	id 'checkstyle'
	id 'com.diffplug.spotless' version '6.22.0'
	id 'com.github.johnrengelman.shadow' version '8.1.1'
}

version = '0.12.0'

def ENV = System.getenv()
version = version + (ENV.GITHUB_ACTIONS ? "" : "+local")

group = 'net.fabricmc'

base {
	archivesName = 'tiny-remapper'
}

repositories {
	mavenCentral()
}

dependencies {
	api 'org.ow2.asm:asm:9.7.1'
	api 'org.ow2.asm:asm-commons:9.7.1'
	implementation 'org.ow2.asm:asm-tree:9.7.1'
	implementation 'org.ow2.asm:asm-util:9.7.1'
	implementation "net.fabricmc:mapping-io:0.7.1"

	testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
	testCompileOnly ('net.fabricmc:sponge-mixin:0.16.2+mixin.0.8.7') {
		transitive = false
	}
}

java {
	withSourcesJar()
	sourceCompatibility = JavaVersion.VERSION_1_8
	targetCompatibility = JavaVersion.VERSION_1_8
}

tasks.withType(JavaCompile).configureEach {
	it.options.encoding = "UTF-8"

	if (JavaVersion.current().isJava9Compatible()) {
		it.options.release = 8
	}
}

shadowJar {
	archiveClassifier = 'fat'
}

test {
	useJUnitPlatform()
	testLogging {
		showStandardStreams = true
		exceptionFormat = 'full'
		events = ['passed', 'failed', 'skipped']
	}
}

jar {
	manifest {
		attributes	'Implementation-Title': 'TinyRemapper',
					'Implementation-Version': archiveVersion,
					'Main-Class': "net.fabricmc.tinyremapper.Main"
	}
}

checkstyle {
	configFile = file("checkstyle.xml")
	toolVersion = '8.31'
}

spotless {
	java {
		licenseHeaderFile(rootProject.file('HEADER')).yearSeparator(', ')
		removeUnusedImports()
		importOrder('java', 'javax', '', 'net.minecraft', 'net.fabricmc')
		indentWithTabs()
		trimTrailingWhitespace()
	}
}

publishing {
	publications {
		mavenJava(MavenPublication) {
			from components.java
		}
	}

	repositories {
		if (ENV.MAVEN_URL) {
			maven {
				url ENV.MAVEN_URL
				credentials {
					username ENV.MAVEN_USERNAME
					password ENV.MAVEN_PASSWORD
				}
			}
		}
	}
}

// A task to ensure that the version being released has not already been released.
task checkVersion {
	doFirst {
		def xml = new URL("https://maven.fabricmc.net/net/fabricmc/tiny-remapper/maven-metadata.xml").text
		def metadata = new XmlSlurper().parseText(xml)
		def versions = metadata.versioning.versions.version*.text();
		if (versions.contains(version)) {
			throw new RuntimeException("${version} has already been released!")
		}
	}
}

publish.mustRunAfter checkVersion
