Skip to content

Commit

Permalink
Fix caching for checking runtime properties (#2438)
Browse files Browse the repository at this point in the history
Resolves #2329
  • Loading branch information
AlexeyTsvetkov authored Oct 31, 2022
1 parent 6501d9f commit 083e51f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private fun JvmApplicationContext.configureCommonJvmDesktopTasks(): CommonJvmDes
taskNameObject = "runtime"
) {
javaHome.set(app.javaHomeProvider)
javaRuntimePropertiesFile.set(jvmTmpDirForTask().file("properties.bin"))
}

val suggestRuntimeModules = tasks.register<AbstractSuggestModulesTask>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jetbrains.compose.desktop.application.tasks

import org.gradle.api.file.Directory
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
Expand All @@ -23,14 +24,17 @@ internal const val MIN_JAVA_RUNTIME_VERSION = 15

@CacheableTask
abstract class AbstractCheckNativeDistributionRuntime : AbstractComposeDesktopTask() {
@get:Input
@get:PathSensitive(PathSensitivity.ABSOLUTE)
@get:InputDirectory
val javaHome: Property<String> = objects.notNullProperty()

private val taskDir = project.layout.buildDirectory.dir("compose/tmp/$name")

@get:OutputFile
val javaRuntimePropertiesFile: RegularFileProperty = objects.fileProperty()
val javaRuntimePropertiesFile: Provider<RegularFile> = taskDir.map { it.file("properties.bin") }

@get:LocalState
val workingDir: Provider<Directory> = project.layout.buildDirectory.dir("compose/tmp/$name")
val workingDir: Provider<Directory> = taskDir.map { it.dir("localState") }

private val javaExec: File
get() = getTool("java")
Expand All @@ -47,6 +51,8 @@ abstract class AbstractCheckNativeDistributionRuntime : AbstractComposeDesktopTa

@TaskAction
fun run() {
taskDir.ioFile.mkdirs()

val javaRuntimeVersion = try {
getJavaRuntimeVersionUnsafe()?.toIntOrNull() ?: -1
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ class DesktopApplicationTest : GradlePluginTestBase() {
testPackageJvmDistributions()
}

@Test
fun gradleBuildCache() = with(testProject(TestProjects.jvm)) {
modifyGradleProperties {
setProperty("org.gradle.caching", "true")
}
modifyTextFile("settings.gradle") {
it + "\n" + """
buildCache {
local {
directory = new File(rootDir, 'build-cache')
}
}
""".trimIndent()
}

val packagingTask = ":packageDistributionForCurrentOS"
gradle(packagingTask).build().checks { check ->
check.taskOutcome(packagingTask, TaskOutcome.SUCCESS)
}

gradle("clean", packagingTask).build().checks { check ->
check.taskOutcome(":checkRuntime", TaskOutcome.FROM_CACHE)
check.taskOutcome(packagingTask, TaskOutcome.SUCCESS)
}
}

@Test
fun packageMpp() = with(testProject(TestProjects.mpp)) {
testPackageJvmDistributions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.jetbrains.compose.test.utils
import org.gradle.testkit.runner.GradleRunner
import org.jetbrains.compose.desktop.application.internal.ComposeProperties
import java.io.File
import java.util.Properties

data class TestEnvironment(
val workingDir: File,
Expand Down Expand Up @@ -77,5 +78,29 @@ class TestProject(

fun file(path: String): File =
testEnvironment.workingDir.resolve(path)

fun modifyTextFile(path: String, fn: (String) -> String) {
val file = file(path)
val oldContent = file.readText()
val newContent = fn(oldContent)
file.writeText(newContent)
}
fun modifyGradleProperties(fn: Properties.() -> Unit) {
val propertiesFile = file("gradle.properties")
val properties = Properties()
if (propertiesFile.exists()) {
propertiesFile.bufferedReader().use { reader ->
properties.load(reader)
}
}
fn(properties)
propertiesFile.delete()

if (properties.isNotEmpty()) {
propertiesFile.bufferedWriter().use { writer ->
properties.store(writer, null)
}
}
}
}

0 comments on commit 083e51f

Please sign in to comment.