plugins {
	id "java"
}

ext {
	junitVersion = "5.11.4"
	testcontainersVersion = "1.21.3"
	assertjCoreVersion = "3.27.6"
}

repositories {
	mavenCentral()
}

group "org.adoptium"
version "1.0.0-SNAPSHOT"

/*
 * The versions list defines the Alpine versions that this package is uploaded to.
 *
 * If a version like sid is missing here, the package won't be included in the sid repository and therefore not
 * installable via apk. Never use suite names like testing or unstable. This list is usually bigger than the list
 * of versions we test with as defined by the class AlpineFlavours in the source set packageTest.
 *
 * **Attention**: If you alter the list, check if the class AlpineFlavours needs to be updated, too.
 */
java {
	sourceCompatibility = JavaVersion.VERSION_1_8
	targetCompatibility = JavaVersion.VERSION_1_8
}

sourceSets {
	packageTest {
		compileClasspath += sourceSets.main.output
		runtimeClasspath += sourceSets.main.output
	}
}

configurations {
	packageTestImplementation.extendsFrom implementation
	packageTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
	packageTestImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
	packageTestImplementation "org.testcontainers:testcontainers:$testcontainersVersion"
	packageTestImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
	packageTestImplementation "org.assertj:assertj-core:$assertjCoreVersion"
}

test {
	useJUnitPlatform()
	testLogging {
		events "passed", "skipped", "failed"
	}
}

task packageJreAlpine {
	dependsOn "assemble"

	group = "packaging"
	description = "Creates deb package for Alpine flavours."

	def outputDir = new File(project.buildDir.absolutePath, "ospackage")
	outputs.dir(outputDir)

	def product = getProduct()
	def productVersion = getProductVersion()
	def gpgKey = getGPGKey()
	def arch = getArch()

	// Get container registry property. If not set, return empty string that is equivalent to dockerhub
	def containerRegistry = getContainerRegistry()

	doLast {
		if (!file("src/main/packaging/$product/$productVersion").exists()) {
			throw new IllegalArgumentException("Unknown product $product/$productVersion")
		}

		project.copy {
			from("src/main/packaging/$product/$productVersion/")
			into("${buildDir}/generated/packaging")
		}
		if (gpgKey) {
			project.exec {
				workingDir "src/main/packaging"
				commandLine "docker", "build", "--no-cache", "--pull",
					"-t", "adoptium-packages-linux-jdk-alpine",
					"--build-arg=CONTAINER_REGISTRY=${containerRegistry}",
					"--secret", "id=gpg,src=${gpgKey}",
					"-f", "Dockerfile",
					getProjectDir().absolutePath + "/src/main/packaging"
			}
		} else {
			project.exec {
				workingDir "src/main/packaging"
				commandLine "docker", "build", "--no-cache", "--pull",
					"-t", "adoptium-packages-linux-jdk-alpine",
					"--build-arg=CONTAINER_REGISTRY=${containerRegistry}",
					"-f", "Dockerfile",
					getProjectDir().absolutePath + "/src/main/packaging"
			}
		}
		project.exec {
			workingDir getRootDir()
			commandLine "docker", "run",
				"--rm",
				"-e", "buildArch=${arch}",
				"--mount", "type=bind,source=${buildDir},target=/home/builder/build",
				"--mount", "type=bind,source=${outputDir.absolutePath},target=/home/builder/out",
				"adoptium-packages-linux-jdk-alpine:latest"
		}
	}
}

task checkJreAlpine(type: Test) {
	dependsOn packageJreAlpine

	description = 'Tests the generated Alpine packages.'
	group = 'verification'

	testClassesDirs = sourceSets.packageTest.output.classesDirs
	classpath = sourceSets.packageTest.runtimeClasspath

	def product = getProduct()
	def productVersion = getProductVersion()
	def gpgKey = getGPGKey()

	def containerRegistry = getContainerRegistry()

	environment "PACKAGE", "${product}-${productVersion}-jre"
	if (gpgKey != null) {
		environment "JDKGPG", "$gpgKey"
		println "We set the gpgkey!"
	}

	if (containerRegistry != null && containerRegistry != "") {
		environment "containerRegistry", "$containerRegistry"
		println "Container registry set to $containerRegistry"
	}

	useJUnitPlatform()
	testLogging {
		events "passed", "skipped", "failed"
	}

	doFirst {
		if (!file("src/main/packaging/$product/$productVersion").exists()) {
			throw new IllegalArgumentException("Unknown product $product/$productVersion")
		}
	}
}

parent.packageJre.dependsOn(packageJreAlpine)
parent.checkJrePackage.dependsOn(checkJreAlpine)
