configurations {
	proGuard
}

dependencies {
	implementation 'org.ow2.asm:asm:9.7.1'
	implementation '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 'org.bitbucket.mstrobel:procyon-compilertools:0.6.0'
	implementation 'net.fabricmc:cfr:0.2.2'
	implementation 'org.vineflower:vineflower:1.10.1'

	proGuard 'com.guardsquare:proguard-base:7.4.0-beta02'

	testImplementation 'com.google.jimfs:jimfs:1.2'
}

// Generate "version.txt" file

def genOutputDir = layout.buildDirectory.dir('generated-resources').get().asFile

tasks.register('generateResources') {
	def langDir = file('src/main/resources/lang')
	inputs.dir(langDir)
	inputs.property 'version', project.version

	def versionFile = file("$genOutputDir/version.txt")
	def langsFile = file("$genOutputDir/lang/index.txt")
	outputs.files(versionFile, langsFile)

	doLast {
		versionFile.text = inputs.properties.version
		langsFile.text = langDir
				.listFiles()
				.collect { it.name }
				.findAll { it.endsWith(".json") }
				.collect { it.substring(0, it.length() - 5) }
				.join("\n")
	}
}

sourceSets.main.output.dir genOutputDir, builtBy: generateResources

// Generate obfuscated JARs for tests

def libraryJarsArg = "<java.home>/jmods"

// If your test fails for class file version problem with proguard, run gradle with -Dorg.gradle.java.home="<older jdk>" flag
file('src/test/java/cuchaz/enigma/inputs').listFiles().each { theFile ->
	if (theFile.directory) {
		tasks.register("${theFile.name}TestJar", Jar) {
			from(sourceSets.test.output) {
				include "cuchaz/enigma/inputs/$theFile.name/**/*.class"
				include 'cuchaz/enigma/inputs/Keep.class'
			}

			archiveFileName = theFile.name + '.jar'
			destinationDirectory = file('build/test-inputs')
		}

		tasks.register("${theFile.name}TestObf", JavaExec) {
			dependsOn "${theFile.name}TestJar"
			mainClass = 'proguard.ProGuard'
			classpath configurations.proGuard

			args '@src/test/resources/proguard-test.conf', '-injars', file('build/test-inputs/' +
					"${theFile.name}.jar"), '-libraryjars', libraryJarsArg,
					'-outjars', file('build/test-obf/' + "${theFile.name}.jar")
		}

		test.dependsOn "${theFile.name}TestObf"
	}
}

test.dependsOn 'translationTestObf'
