plugins {
	id 'application'
}

group = 'com.mgrm'
version = '1.2.0'
sourceCompatibility = '8'

repositories {
	mavenCentral()
}

dependencies {
	implementation group: 'org.bouncycastle', name: 'bcprov-jdk18on', version: '1.71'
	implementation group: 'com.google.code.gson', name: 'gson', version: '2.9.0'
}

test {
	useJUnitPlatform()
}

jar {
	manifest {
		attributes (
			'Main-Class': 'com.mgrm.fidelius.FideliusApplication'
		)
	}
	/*
	 ** We can't bundle cryptographic libraries, such as bouncycastle into a fat jar.
	 ** They have to be signed for the JVM to load them, and their
	 ** signature is destroyed when merged into a fat jar.
	 ** 
	 ** It is also not recommended to exclude such signatures, to circumvent the above issue,
	 ** as this could eventually break some of the library's cryptographic operations.
	 ** 
	 ** doFirst {
	 ** 	from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
	 ** }
	 ** exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
	*/
}

Collection getListOfFolders(String dir, String pattern) {
	file(dir).listFiles({ file -> !file.isFile() && file.name ==~ pattern } as FileFilter)
}

task addBuildDistToExamples {
	// Perform a clean build of the project
	dependsOn('clean')
	dependsOn('build')
	tasks.findByName('build').mustRunAfter('clean')

	doLast {
		copy {
			// Clean any previous distributions from the examples folder
			delete getListOfFolders("${rootDir}/examples", /fidelius-cli-\d+\.\d+\.\d+/)

			// Unzip and copy build distribuion (.zip) into the examples folder
			def zipFile = file("build/distributions/fidelius-cli-${version}.zip")
			def outputDir = file("${rootDir}/examples")
			from zipTree(zipFile)
			into outputDir
		}
	}
}

application {
	mainClass = 'com.mgrm.fidelius.FideliusApplication'
}
