From 56bcf38c12258f70e33516845f4c03b270dd2063 Mon Sep 17 00:00:00 2001 From: Tadeas Kriz Date: Tue, 16 Apr 2024 19:35:20 -0400 Subject: [PATCH] Fix Gradle Configuration Cache support. # Conflicts: # SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/configuration/CreateSkieConfigurationTask.kt --- .../CreateSkieConfigurationTask.kt | 3 ++- .../fatframework/FatFrameworkConfigurator.kt | 23 ++++++++-------- .../touchlab/skie/plugin/util/DarwinTarget.kt | 5 ++-- .../util/InjectedFileSystemOperations.kt | 9 +++++++ .../Test Gradle Configuration Cache.run.xml | 26 +++++++++++++++++++ .../GradleConfigurationCacheTests.kt | 13 ++++++++-- 6 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/InjectedFileSystemOperations.kt create mode 100644 test-runner/.run/Test Gradle Configuration Cache.run.xml diff --git a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/configuration/CreateSkieConfigurationTask.kt b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/configuration/CreateSkieConfigurationTask.kt index b916bce2..250b80c1 100644 --- a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/configuration/CreateSkieConfigurationTask.kt +++ b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/configuration/CreateSkieConfigurationTask.kt @@ -45,8 +45,9 @@ internal abstract class CreateSkieConfigurationTask : DefaultTask() { fun registerTask(target: SkieTarget) { val createConfiguration = target.registerSkieTargetBasedTask("createConfiguration") { + val skieExtension = project.skieExtension configurationFile.set(target.skieBuildDirectory.skieConfiguration) - configuration.set(project.provider { project.skieExtension.buildConfiguration() }) + configuration.set(project.provider { skieExtension.buildConfiguration() }) dependsOn(target.createSkieBuildDirectoryTask) } diff --git a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/fatframework/FatFrameworkConfigurator.kt b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/fatframework/FatFrameworkConfigurator.kt index 6388e771..8ea27f0d 100644 --- a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/fatframework/FatFrameworkConfigurator.kt +++ b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/fatframework/FatFrameworkConfigurator.kt @@ -6,19 +6,17 @@ import org.gradle.api.Project import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask import org.jetbrains.kotlin.gradle.tasks.FrameworkLayout import org.jetbrains.kotlin.konan.target.Architecture -import org.jetbrains.kotlin.konan.target.Family import java.io.File internal object FatFrameworkConfigurator { - fun configureSkieForFatFrameworks(project: Project) { - project.fixFatFrameworkNameForCocoaPodsPlugin() - project.configureFatFrameworkPatching() + fixFatFrameworkNameForCocoaPodsPlugin(project) + configureFatFrameworkPatching(project) } - private fun Project.fixFatFrameworkNameForCocoaPodsPlugin() { - pluginManager.withPlugin("kotlin-native-cocoapods") { - tasks.withType().matching { it.name == "fatFramework" }.configureEach { + private fun fixFatFrameworkNameForCocoaPodsPlugin(project: Project) { + project.pluginManager.withPlugin("kotlin-native-cocoapods") { + project.tasks.withType().matching { it.name == "fatFramework" }.configureEach { // Unfortunately has to be done in `doFirst` to make sure the task is already configured by the plugin when we run our code doFirstOptimized { val commonFrameworkName = frameworks.map { it.name }.distinct().singleOrNull() ?: return@doFirstOptimized @@ -28,8 +26,9 @@ internal object FatFrameworkConfigurator { } } - private fun Project.configureFatFrameworkPatching() { - tasks.withType().configureEach { + private fun configureFatFrameworkPatching(project: Project) { + val injectedFileSystemOperations = project.objects.newInstance() + project.tasks.withType().configureEach { doLastOptimized { // There shouldn't be any real difference between the frameworks except for architecture, so we consider the first one "primary" val primaryFramework = frameworks.first() @@ -38,7 +37,7 @@ internal object FatFrameworkConfigurator { isMacosFramework = primaryFramework.files.isMacosFramework, ) - // FatFramewrokTask writes its own + // FatFrameworkTask writes its own target.moduleFile.writeText( primaryFramework.files.moduleFile.readText() ) @@ -74,12 +73,12 @@ internal object FatFrameworkConfigurator { target.swiftModuleDir.mkdirs() frameworksByArchs.toList().forEach { (_, framework) -> - project.copy { + injectedFileSystemOperations.fileSystemOperations.copy { from(framework.files.apiNotes) into(target.headerDir) } framework.files.swiftModuleFiles(framework.darwinTarget.targetTriple).forEach { swiftmoduleFile -> - project.copy { + injectedFileSystemOperations.fileSystemOperations.copy { from(swiftmoduleFile) into(target.swiftModuleDir) } diff --git a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/DarwinTarget.kt b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/DarwinTarget.kt index ef3087f4..84e0fbbe 100644 --- a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/DarwinTarget.kt +++ b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/DarwinTarget.kt @@ -33,7 +33,7 @@ internal data class DarwinTarget( DarwinTarget(KonanTarget.TVOS_SIMULATOR_ARM64, "arm64-apple-tvos-simulator", "appletvsimulator"), DarwinTarget(KonanTarget.MACOS_X64, "x86_64-apple-macos", "macosx"), DarwinTarget(KonanTarget.MACOS_ARM64, "arm64-apple-macos", "macosx"), - ).associateBy { it.konanTarget } + ).associateBy { it.konanTarget.name } } } @@ -41,4 +41,5 @@ internal val FrameworkDescriptor.darwinTarget: DarwinTarget get() = target.darwinTarget internal val KonanTarget.darwinTarget: DarwinTarget - get() = DarwinTarget.allTargets[this] ?: error("Unknown konan target: $this") + // We can't use `KotlinTarget` directly as the instance can differ when Gradle Configuration Cache is used + get() = DarwinTarget.allTargets[name] ?: error("Unknown konan target: $this") diff --git a/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/InjectedFileSystemOperations.kt b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/InjectedFileSystemOperations.kt new file mode 100644 index 00000000..5a5430af --- /dev/null +++ b/SKIE/skie-gradle/plugin/src/main/kotlin/co/touchlab/skie/plugin/util/InjectedFileSystemOperations.kt @@ -0,0 +1,9 @@ +package co.touchlab.skie.plugin.util + +import org.gradle.api.file.FileSystemOperations +import javax.inject.Inject + +interface InjectedFileSystemOperations { + @get:Inject + val fileSystemOperations: FileSystemOperations +} diff --git a/test-runner/.run/Test Gradle Configuration Cache.run.xml b/test-runner/.run/Test Gradle Configuration Cache.run.xml new file mode 100644 index 00000000..a29e1962 --- /dev/null +++ b/test-runner/.run/Test Gradle Configuration Cache.run.xml @@ -0,0 +1,26 @@ + + + + + + + true + true + false + false + + + \ No newline at end of file diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/configurationcache/GradleConfigurationCacheTests.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/configurationcache/GradleConfigurationCacheTests.kt index 777c2db0..69acf24a 100644 --- a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/configurationcache/GradleConfigurationCacheTests.kt +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/configurationcache/GradleConfigurationCacheTests.kt @@ -6,6 +6,7 @@ import co.touchlab.skie.test.annotation.filter.Smoke import co.touchlab.skie.test.annotation.type.GradleTests import co.touchlab.skie.test.base.BaseGradleTests import co.touchlab.skie.test.runner.BuildConfiguration +import co.touchlab.skie.test.template.Templates import co.touchlab.skie.test.util.KotlinTarget import co.touchlab.skie.test.util.KotlinVersion import co.touchlab.skie.test.util.LinkMode @@ -21,9 +22,9 @@ import kotlin.test.assertEquals class GradleConfigurationCacheTests: BaseGradleTests() { // We don't need to run the whole build to check configuration cache - private val gradleArguments = arrayOf("dependencies") + private val gradleArguments = arrayOf("assemble") private val assertGradleResult = { result: BuildResult -> - assertEquals(TaskOutcome.SUCCESS, result.task(":dependencies")?.outcome) + assertEquals(TaskOutcome.SUCCESS, result.task(":assemble")?.outcome) } @MatrixTest @@ -36,6 +37,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() { } } + copyToCommonMain(Templates.basic) + runGradle( arguments = gradleArguments, assertResult = assertGradleResult, @@ -60,6 +63,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() { } } + copyToCommonMain(Templates.basic) + runGradle( arguments = gradleArguments, assertResult = assertGradleResult, @@ -89,6 +94,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() { } } + copyToCommonMain(Templates.basic) + runGradle( arguments = gradleArguments, assertResult = assertGradleResult, @@ -116,6 +123,8 @@ class GradleConfigurationCacheTests: BaseGradleTests() { } } + copyToCommonMain(Templates.basic) + runGradle( arguments = gradleArguments, assertResult = assertGradleResult,