Skip to content

Commit

Permalink
Adds ComposeRuntime to klibSourceFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhE committed Nov 28, 2024
1 parent 5444044 commit a0d0721
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-nexus-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
cache: gradle

- name: Run Processor tests
run: ./gradlew clean :kmp-composeuiviewcontroller-ksp:test --tests "composeuiviewcontroller.ProcessorTest" --info
run: ./gradlew clean :kmp-composeuiviewcontroller-ksp:test --tests "composeuiviewcontroller.ProcessorTest"

- name: Publish to Nexus
if: success()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ import kotlin.test.assertTrue
@OptIn(ExperimentalCompilerApi::class)
class ProcessorTest {

private val jarPackages: List<File> = TestUtils.findFiles(
basePath = System.getProperty("user.home") + "/.gradle/caches/modules-2/files-2.1",
packages = listOf("org.jetbrains.compose.runtime", "org.jetbrains.compose.ui"),
extension = "jar",
exclude = listOf("sources", "metadata")
)
private val pluginPackages: List<File> = TestUtils.findFiles(
basePath = System.getProperty("user.home") + "/.gradle/caches/modules-2/files-2.1",
packages = listOf("org.jetbrains.compose", "org.jetbrains.kotlin.plugin.compose"),
extension = "plugin"
)
private lateinit var tempFolder: File
private lateinit var tempArgs: File
private var usesKsp2: Boolean = false
Expand All @@ -52,10 +41,7 @@ class ProcessorTest {
inheritClassPath = true
verbose = false
usesKsp2 = precursorTools.contains("ksp2")
if (usesKsp2) {
classpaths += jarPackages
pluginClasspaths += pluginPackages
} else {
if (!usesKsp2) {
languageVersion = "1.9"
}
}
Expand Down Expand Up @@ -85,15 +71,15 @@ class ProcessorTest {
import $composeUIViewControllerStateAnnotationName
import androidx.compose.runtime.Composable
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@Composable
fun ScreenA(state: ViewState) { }
@Composable
fun ScreenB(@ComposeUIViewControllerState state: ViewState) { }
""".trimIndent()
val compilation = prepareCompilation(kotlin("Screen.kt", code))
val compilation = prepareCompilation(kotlin("Screen.kt", code), *klibSourceFiles().toTypedArray())

val result = compilation.compile()
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)
Expand All @@ -115,7 +101,7 @@ class ProcessorTest {
import $composeUIViewControllerStateAnnotationName
import androidx.compose.runtime.Composable
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("MyFramework")
@Composable
Expand All @@ -141,7 +127,7 @@ class ProcessorTest {
import $composeUIViewControllerStateAnnotationName
import androidx.compose.runtime.Composable
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -165,7 +151,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController
@Composable
Expand Down Expand Up @@ -255,7 +241,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -274,7 +260,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand Down Expand Up @@ -383,7 +369,7 @@ class ProcessorTest {
fun `Composable functions from different files are parsed once only once`() {
val data = """
package com.mycomposable.data
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
""".trimIndent()
val codeA = """
package com.mycomposable.test
Expand Down Expand Up @@ -436,7 +422,7 @@ class ProcessorTest {
import platform.UIKit.UIViewController
import androidx.compose.runtime.*
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand Down Expand Up @@ -476,7 +462,7 @@ class ProcessorTest {
import androidx.compose.runtime.Composable
import com.mycomposable.test.ViewState
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand Down Expand Up @@ -554,7 +540,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -573,7 +559,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -591,7 +577,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -609,7 +595,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -627,7 +613,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand All @@ -645,7 +631,7 @@ class ProcessorTest {
import $composeUIViewControllerAnnotationName
import $composeUIViewControllerStateAnnotationName
data class ViewState(val field: Int)
data class ViewState(val field: Int = 0)
@ComposeUIViewController("ComposablesFramework")
@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("MemberVisibilityCanBePrivate")
@file:Suppress("MemberVisibilityCanBePrivate", "unused")

package composeuiviewcontroller

Expand All @@ -9,12 +9,16 @@ import java.io.File
object TestUtils {

/**
* Searches for files in a given Gradle cache path for specified package names.
* Searches for files in a specified Gradle cache path matching the given package names.
*
* NOTE: [klibSourceFiles] was updated for CI usage to replace [findFiles], addressing issues with relative path resolution.
*
* @param basePath The root directory to start the search (e.g., ~/.gradle/caches/modules-2/files-2.1).
* @param packages The list of package names to search for (e.g., "androidx.compose.runtime").
* @param extension The file extension
* @return A list of Files pointing to the .jar files found for the specified packages.
* @param packages A list of package names to search for (e.g., "androidx.compose.runtime").
* @param extension The file extension to filter by.
* @param exclude A list of names to exclude from the file name search (e.g., "source", "metadata").
* @param verbose Enables detailed logging of the search process.
* @return A list of [File] objects pointing to the files found.
*/
fun findFiles(
basePath: String,
Expand Down Expand Up @@ -45,29 +49,68 @@ object TestUtils {
return files
}

val jarPackages: List<File> = findFiles(
basePath = System.getProperty("user.home") + "/.gradle/caches/modules-2/files-2.1",
packages = listOf("org.jetbrains.compose.runtime", "org.jetbrains.compose.ui"),
extension = "jar",
exclude = listOf("sources", "metadata")
)
val pluginPackages: List<File> = findFiles(
basePath = System.getProperty("user.home") + "/.gradle/caches/modules-2/files-2.1",
packages = listOf("org.jetbrains.compose", "org.jetbrains.kotlin.plugin.compose"),
extension = "plugin"
)

/**
* The kotlin-compile-testing library does not support Kotlin Multiplatform (KMP).
* As a result, to work with .klib files generated by Kotlin/Native, a workaround is required.
* This involves generating these files in a dummy manner, ensuring that the compiler recognizes the sources and can proceed with compilation.
*
* Platform.kt and ComposeUi.kt provide .klib dependencies.
*
* ComposeRuntime.kt provides dependencies that can also be located using [findFiles].
* However, due to challenges with relative paths in a CI environment, this utility simplifies
* the process and eliminates the need for [findFiles] in such contexts.
*
* https://kotlinlang.slack.com/archives/C013BA8EQSE/p1732453625647829
*/
fun klibSourceFiles(): List<SourceFile> {
return listOf(
kotlin(
"ComposeUIViewController.kt", """
package androidx.compose.ui.window
import androidx.compose.runtime.Composable
import platform.UIKit.UIViewController
fun ComposeUIViewController(content: @Composable () -> Unit): UIViewController = UIViewController()
""".trimIndent()
), kotlin(
"UIViewController.kt", """
package platform.UIKit
open class UIViewController
""".trimIndent()
"Platform.kt",
"""
package platform.UIKit
open class UIViewController
""".trimIndent()
),
kotlin(
"ComposeUi.kt",
"""
package androidx.compose.ui.window
import androidx.compose.runtime.Composable
import platform.UIKit.UIViewController
fun ComposeUIViewController(content: @Composable () -> Unit): UIViewController = UIViewController()
""".trimIndent()
),
kotlin(
"ComposeRuntime.kt",
"""
package androidx.compose.runtime
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.TYPE, AnnotationTarget.TYPE_PARAMETER, AnnotationTarget.PROPERTY_GETTER)
annotation class Composable
interface MutableState<T> { var value: T }
fun <T> mutableStateOf(initialValue: T): MutableState<T> {
return object : MutableState<T> {
override var value: T = initialValue
}
}
""".trimIndent()
)
)
}
Expand Down

0 comments on commit a0d0721

Please sign in to comment.