import org.gradle.util.GradleVersion

plugins {
	id 'java-library'
	id 'java-gradle-plugin'
	id 'checkstyle'
	id 'maven-publish'
}

group = 'net.fabricmc'
version = project.filament_version

def ENV = System.getenv()

// Needed to read the main projects properties
def properties = new Properties()
file('../gradle.properties').newInputStream().withCloseable {
	properties.load(it)
}

repositories {
	maven {
		name = "Fabric Repository"
		url = 'https://maven.fabricmc.net'
	}
	mavenCentral()
	mavenLocal()
}

dependencies {
	implementation "com.google.code.gson:gson:${properties.gson_version}"
	implementation "org.ow2.asm:asm:${properties.asm_version}"
	implementation "org.ow2.asm:asm-tree:${properties.asm_version}"
	implementation "org.ow2.asm:asm-analysis:${properties.asm_version}"
	implementation "cuchaz:enigma:$properties.enigma_version"
	implementation "cuchaz:enigma-cli:$properties.enigma_version"
	implementation "net.fabricmc.unpick:unpick:$properties.unpick_version"
	implementation "net.fabricmc.unpick:unpick-format-utils:$properties.unpick_version"
	implementation "net.fabricmc:tiny-remapper:$properties.tiny_remapper_version"
	implementation 'net.fabricmc:mapping-io:0.7.1'
	implementation 'net.fabricmc:javapoet:0.1.1'

	// Contains a number of useful utilities we can re-use.
	implementation ("net.fabricmc:fabric-loom:$properties.loom_version") {
		transitive = false
	}

	testImplementation platform("org.junit:junit-bom:$properties.junit_version")
	testImplementation 'org.junit.jupiter:junit-jupiter'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
	testImplementation "org.assertj:assertj-core:$properties.assertj_version"
}

java {
	withSourcesJar()
}

tasks.withType(JavaCompile) {
	options.encoding = "UTF-8"
	options.release = 21
}

test {
	useJUnitPlatform()
}

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

// Workaround https://github.com/gradle/gradle/issues/27035
configurations.checkstyle {
	resolutionStrategy.capabilitiesResolution.withCapability("com.google.collections:google-collections") {
		select("com.google.guava:guava:0")
	}
}

gradlePlugin {
	plugins {
		filament {
			id = 'net.fabricmc.filament'
			implementationClass = 'net.fabricmc.filament.FilamentGradlePlugin'
		}
	}
}

publishing {
	repositories {
		if (ENV.MAVEN_URL) {
			repositories.maven {
				name = "fabric"
				url = ENV.MAVEN_URL
				credentials {
					username ENV.MAVEN_USERNAME
					password ENV.MAVEN_PASSWORD
				}
			}
		}
	}
}

/**
 * Run this task to download the gradle sources next to the api jar, you may need to manually attach the sources jar
 */
tasks.register('downloadGradleSources') {
	doLast {
		// Awful hack to find the gradle api location
		def gradleApiFile = project.configurations.detachedConfiguration(dependencies.gradleApi()).files.stream()
			.filter {
				it.name.startsWith("gradle-api")
			}.findFirst().orElseThrow()

		def gradleApiSources = new File(gradleApiFile.absolutePath.replace(".jar", "-sources.jar"))
		def url = "https://services.gradle.org/distributions/gradle-${GradleVersion.current().getVersion()}-src.zip"

		gradleApiSources.delete()

		println("Downloading (${url}) to (${gradleApiSources})")
		gradleApiSources << new URL(url).newInputStream()
	}
}