Skip to content

Commit

Permalink
Make desktop preview task fully configuration cache compliant (#4410)
Browse files Browse the repository at this point in the history
The issue was in a case where there wasn't a direct dependency on the
skiko artifact, the task would attempt to get one at task execution
time. This moves that to task configuration time.
  • Loading branch information
eygraber committed Apr 4, 2024
1 parent 9233486 commit 93f3725
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private fun registerConfigurePreviewTask(
) { previewTask ->
runtimeFiles.configureUsageBy(previewTask) { (runtimeJars, _) ->
previewClasspath = runtimeJars
skikoRuntime = tryGetSkikoRuntimeIfNeeded()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ abstract class AbstractConfigureDesktopPreviewTask : AbstractComposeDesktopTask(
@get:InputFiles
internal lateinit var previewClasspath: FileCollection

@get:InputFiles
internal lateinit var skikoRuntime: FileCollection

@get:Internal
internal val javaHome: Property<String> = objects.notNullProperty<String>().apply {
set(providers.systemProperty("java.home"))
Expand Down Expand Up @@ -58,7 +61,7 @@ abstract class AbstractConfigureDesktopPreviewTask : AbstractComposeDesktopTask(
val previewClasspathString =
(previewClasspath.files.asSequence() +
uiTooling.files.asSequence() +
tryGetSkikoRuntimeFilesIfNeeded().asSequence()
skikoRuntime.files.asSequence()
).pathString()

val gradleLogger = logger
Expand All @@ -78,7 +81,7 @@ abstract class AbstractConfigureDesktopPreviewTask : AbstractComposeDesktopTask(
}
}

private fun tryGetSkikoRuntimeFilesIfNeeded(): Collection<File> {
internal fun tryGetSkikoRuntimeIfNeeded(): FileCollection {
try {
var hasSkikoJvm = false
var hasSkikoJvmRuntime = false
Expand All @@ -96,21 +99,20 @@ abstract class AbstractConfigureDesktopPreviewTask : AbstractComposeDesktopTask(
}
}
}
if (hasSkikoJvmRuntime) return emptyList()
if (hasSkikoJvmRuntime) return project.files()

if (hasSkikoJvm && !skikoVersion.isNullOrBlank()) {
val skikoRuntimeConfig = project.detachedDependency(
return project.detachedDependency(
groupId = "org.jetbrains.skiko",
artifactId = "skiko-awt-runtime-${currentTarget.id}",
version = skikoVersion
).excludeTransitiveDependencies()
return skikoRuntimeConfig.files
}
} catch (e: Exception) {
// OK
}

return emptyList()
return project.files()
}

private fun Sequence<File>.pathString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ class GradlePluginTest : GradlePluginTestBase() {
connectionThread.join(5000)
}

val expectedReceivedConfigCount = 2
val expectedReceivedConfigCount = 3
val actualReceivedConfigCount = receivedConfigCount.get()
check(actualReceivedConfigCount == 2) {
check(actualReceivedConfigCount == expectedReceivedConfigCount) {
"Expected to receive $expectedReceivedConfigCount preview configs, got $actualReceivedConfigCount"
}
}
Expand All @@ -309,6 +309,11 @@ class GradlePluginTest : GradlePluginTestBase() {
gradle(mppTask, portProperty, previewTargetProperty).checks {
check.taskSuccessful(mppTask)
}

val commonTask = ":common:configureDesktopPreviewDesktop"
gradle(commonTask, portProperty, previewTargetProperty).checks {
check.taskSuccessful(commonTask)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id 'org.jetbrains.kotlin.multiplatform'
id 'org.jetbrains.compose'
}

kotlin {
jvm('desktop') {}

sourceSets {
commonMain.dependencies {
api compose.runtime
api compose.foundation
api compose.material
api compose.uiTooling
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/

import androidx.compose.material.Text
import androidx.compose.material.Button
import androidx.compose.runtime.*

@Composable
fun ExampleComposable() {
var text by remember { mutableStateOf("Hello, World!") }

Button(onClick = {
text = "Hello, $platformName!"
}) {
Text(text)
}
}

val platformName: String = "Desktop"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import androidx.compose.runtime.Composable
import androidx.compose.desktop.ui.tooling.preview.Preview

@Preview
@Composable
fun ExamplePreview() {
ExampleComposable()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ dependencyResolutionManagement {
}
}
rootProject.name = 'jvmPreview'
include(':jvm', ':mpp')
include(':common', ':jvm', ':mpp')

0 comments on commit 93f3725

Please sign in to comment.