Skip to content

Commit

Permalink
lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
midttuna committed Feb 17, 2025
1 parent 2169689 commit e5257a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import org.gradle.api.tasks.Copy
import org.gradle.kotlin.dsl.register

import org.gradle.kotlin.dsl.named
import org.gradle.api.provider.Provider
import edu.sc.seis.launch4j.tasks.DefaultLaunch4jTask

plugins {
id("com.draeger.medical.java-conventions")
Expand All @@ -9,63 +11,79 @@ plugins {
id("edu.sc.seis.launch4j")
}

val javaVersion = property("javaVersion").toString()
val javaVersion: String by project


val jreDirectoryName = "jdk-17.0.5+8-jre"
val jreBasePath = "jre"
val jreFullPath = "${jreBasePath}/${jreDirectoryName}"
val jreFullPath = "$jreBasePath/$jreDirectoryName"
val jreDownloadUrlPrefix = "https://github.com/adoptium/temurin17-binaries/releases/download/"
val jreDownloadFileName = "OpenJDK17U-jre_x64_windows_hotspot_17.0.5_8.zip"
val jreDownloadUrlSuffix = "jdk-17.0.5%2B8/${jreDownloadFileName}"
val jreDownloadUrl = "${jreDownloadUrlPrefix}${jreDownloadUrlSuffix}"
val jreDownloadUrlSuffix = "jdk-17.0.5%2B8/$jreDownloadFileName"
val jreDownloadUrl = "$jreDownloadUrlPrefix$jreDownloadUrlSuffix"


val buildDirProvider = layout.buildDirectory.map { it.asFile }

tasks.register<de.undercouch.gradle.tasks.download.Download>("downloadJre") {
src(jreDownloadUrl)
dest(file("${layout.buildDirectory.get().asFile}/${jreDownloadFileName}"))
dest(provider { buildDirProvider.get().resolve(jreDownloadFileName) })
overwrite(false)
onlyIfModified(true)
}

tasks.register<Copy>("unpackJre") {
doFirst {
file("${layout.buildDirectory.get().asFile}/").walk().forEach { file ->
buildDirProvider.get().walk().forEach { file ->
if (!file.setWritable(true)) {
println("Failed to set writable permission for ${file.absolutePath}")
}
}
}
dependsOn("downloadJre")
from(zipTree(file("${layout.buildDirectory.get().asFile}/${jreDownloadFileName}")))
into("${layout.buildDirectory.get().asFile}/${jreBasePath}")
dependsOn(tasks.named("downloadJre"))
from(provider { zipTree(buildDirProvider.get().resolve(jreDownloadFileName)) })
into(provider { buildDirProvider.get().resolve(jreBasePath) })
}

tasks.register("downloadAndUnpackJre") {
dependsOn("unpackJre")
dependsOn(tasks.named("unpackJre"))
}

tasks.register<Copy>("copyRuntimeLibs") {
from(configurations.runtimeClasspath) {
exclude { it.isDirectory }
}
into("${layout.buildDirectory.get().asFile}/lib")
into(provider { buildDirProvider.get().resolve("lib") })
}

tasks.named("createExe") {
// Use dynamic configuration (note: no compile-time checking)
(this as ExtensionAware).extensions.extraProperties.apply {
set("headerType", "console")
set("jar", "${layout.buildDirectory.get().asFile}/libs/${project.name}-${project.version}.jar")
set("outfile", "${project.name}-${project.version}.exe")
set("mainClassName", findProperty("mainClass")?.toString() ?: "com.draeger.medical.sdccc.TestSuite")
set("classpath", setOf("lib/**"))
set("jreMinVersion", javaVersion)
set("bundledJrePath", "./${jreFullPath}")
set("version", "${project.version}.0")
set("textVersion", "${project.version}")
set("fileDescription", "${project.name}")
set("copyright", "2023-2024 Draegerwerk AG & Co. KGaA")
set("productName", "${project.name}")
set("companyName", "Draegerwerk AG & Co. KGaA")
set("internalName", "sdccc")
}

tasks.named("createExe", DefaultLaunch4jTask::class) {
dependsOn(tasks.named("copyRuntimeLibs"), tasks.named("downloadAndUnpackJre"))

headerType = "console"
jar = provider {

layout.buildDirectory.get().asFile.resolve("libs/${project.name}-${project.version}.jar").absolutePath
}.get()
outfile = "${project.name}-${project.version}.exe" // Relative path: file will be placed in build/launch4j.
mainClassName = findProperty("mainClass")?.toString() ?: "com.draeger.medical.sdccc.TestSuite"
classpath = setOf("lib/**")
jreMinVersion = javaVersion
bundledJrePath = "./$jreFullPath"

version = "${project.version}.0"
textVersion = "${project.version}"
fileDescription = "${project.name}"
copyright = "2023-2024 Draegerwerk AG & Co. KGaA"

productName = "${project.name}"
companyName = "Draegerwerk AG & Co. KGaA"
internalName = "sdccc"
}

tasks.named("build") {
dependsOn(tasks.named("createExe"))
}
dependencies {
api(libs.gradleplugins.launch4j)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import org.gradle.api.tasks.JavaExec
import org.gradle.kotlin.dsl.creating
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.register

plugins {
id("com.draeger.medical.java-conventions")
id("org.jetbrains.kotlin.jvm")
}

val javaVersion = property("javaVersion").toString()

val javaVersion: String by project

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
Expand All @@ -18,35 +17,38 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {

val detekt by configurations.creating

val detektConfigPath = projectDir.path + File.separator +
(project.findProperty("detektConfigFilePath")?.toString() ?: "dev_config/detekt.yml")

val detektConfigPath = providers.provider {
projectDir.resolve(project.findProperty("detektConfigFilePath")?.toString() ?: "dev_config/detekt.yml")
}

// container to lazily call asPath upon evaluation, not during configuration, which can be too early
data class LazyToStringFileCollection(private val fileCollection: FileCollection) {
override fun toString(): String = fileCollection.asPath
}

val detektTask = tasks.register<JavaExec>("detekt") {
dependsOn("assemble")
tasks.register<JavaExec>("detekt") {
dependsOn(tasks.named("assemble"))
mainClass.set("io.gitlab.arturbosch.detekt.cli.Main")
classpath = detekt

val input = projectDir
val config = detektConfigPath
// The config is obtained via the provider when the task executes
val config = detektConfigPath.get().absolutePath
val exclude = ".*/build/.*,.*/resources/.*,**/build.gradle.kts,**/settings.gradle.kts"
val classpathNeededForDetekt = LazyToStringFileCollection(
project.sourceSets.main.get().runtimeClasspath +
project.sourceSets.test.get().runtimeClasspath +
project.sourceSets.main.get().compileClasspath +
project.sourceSets.test.get().compileClasspath
project.sourceSets["main"].runtimeClasspath +
project.sourceSets["test"].runtimeClasspath +
project.sourceSets["main"].compileClasspath +
project.sourceSets["test"].compileClasspath
)

val jdkHome = System.getProperty("java.home")
args(
"--input", input.absolutePath,
"--config", config,
"--excludes", exclude,
"--report", "html:${layout.buildDirectory.get().asFile}/reports/detekt/detekt.html",
"--report", provider { "html:${layout.buildDirectory.get().asFile.resolve("reports/detekt/detekt.html")}" }.get(),
"--classpath", classpathNeededForDetekt,
"--jdk-home", jdkHome,
"--jvm-target", javaVersion,
Expand All @@ -58,4 +60,4 @@ dependencies {
detekt(libs.detekt.cli)
detekt(libs.detekt.formatting)
api(libs.org.jetbrains.kotlin.kotlin.stdlib)
}
}

0 comments on commit e5257a7

Please sign in to comment.