Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package aws.sdk.kotlin.gradle.kmp

import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.withType
Expand All @@ -16,45 +17,47 @@ import org.jetbrains.kotlin.konan.target.HostManager
* https://youtrack.jetbrains.com/issue/KT-38317
*/
public fun Project.configureIosSimulatorTasks() {
val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 15"
if (this != rootProject) {
throw GradleException("This function should only be called from the root project.")
}
if (!HostManager.hostIsMac) return

val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 15"
val xcrun = "/usr/bin/xcrun"

tasks.register("bootIosSimulatorDevice", Exec::class.java) {
val bootTask = rootProject.tasks.maybeCreate("bootIosSimulatorDevice", Exec::class.java).apply {
isIgnoreExitValue = true
commandLine(xcrun, "simctl", "boot", simulatorDeviceName)

doLast {
val result = executionResult.get()
val code = result.exitValue
if (code != 148 && code != 149 && code != 405) { // ignore "simulator already running" errors
if (code != 148 && code != 149) { // ignore "simulator already running" errors
result.assertNormalExitValue()
}
}
}

tasks.register("shutdownIosSimulatorDevice", Exec::class.java) {
val shutdownTask = rootProject.tasks.maybeCreate("shutdownIosSimulatorDevice", Exec::class.java).apply {
isIgnoreExitValue = true
mustRunAfter(tasks.withType<KotlinNativeSimulatorTest>())
commandLine(xcrun, "simctl", "shutdown", simulatorDeviceName)

doLast {
val result = executionResult.get()
if (result.exitValue != 405) { // ignore "simulator already shutdown" errors
val code = result.exitValue
if (code != 148 && code != 149) { // ignore "simulator already shutdown" errors
result.assertNormalExitValue()
}
}
}

tasks.withType<KotlinNativeSimulatorTest>().configureEach {
if (!HostManager.hostIsMac) {
return@configureEach
allprojects {
val simulatorTasks = tasks.withType<KotlinNativeSimulatorTest>()
simulatorTasks.configureEach {
dependsOn(bootTask)
standalone.set(false)
device.set(simulatorDeviceName)
}

dependsOn("bootIosSimulatorDevice")
finalizedBy("shutdownIosSimulatorDevice")

standalone.set(false)
device.set(simulatorDeviceName)
shutdownTask.mustRunAfter(simulatorTasks)
}
}