Skip to content

Commit

Permalink
Move KotlinNative tasks into doLast
Browse files Browse the repository at this point in the history
  • Loading branch information
JSMonk committed Sep 6, 2024
1 parent 96007d0 commit de8d675
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions modules/gradle-plugin/src/main/kotlin/NativeMultiplatformTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package org.jetbrains.compose.plugin.storytale
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.configurationcache.extensions.capitalized
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.task
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
Expand Down Expand Up @@ -86,33 +90,35 @@ private fun Project.createNativeStorytaleExecTask(
): Task? {
if (!target.name.contains("Simulator")) return null

var deviceId: String? = null
val deviceId = objects.property<String>()
val targetSuffix = target.name.capitalized()
val linkTask = tasks.findByPath("link${StorytaleGradlePlugin.STORYTALE_TASK_GROUP.capitalized()}${StorytaleGradlePlugin.LINK_BUILD_VERSION}Framework$targetSuffix") as? KotlinNativeLink
?: error("Link task was not created for target ${target.name}")

val unzipXCodeProjectTask = createUnzipResourceTask(extension)
val simulatorRegistrationTask = createSimulatorRegistrationTask(unzipXCodeProjectTask, targetSuffix) { deviceId = it }
val simulatorRegistrationTask = createSimulatorRegistrationTask(unzipXCodeProjectTask, targetSuffix, deviceId)

val simulatorId = deviceId!!
val buildTask = createBuildTask(targetSuffix, simulatorId, unzipXCodeProjectTask, simulatorRegistrationTask, linkTask)
val buildTask = createBuildTask(targetSuffix, deviceId, unzipXCodeProjectTask, simulatorRegistrationTask, linkTask)
val platform = if (target.konanTarget === KonanTarget.IOS_SIMULATOR_ARM64) "iphonesimulator" else "iphoneos"

val copyResourcesTask = createCopyNativeResourcesTask(platform, target, targetSuffix, compilation, unzipXCodeProjectTask, buildTask)
val installAppTask = createInstallApplicationToSimulatorTask(simulatorId, targetSuffix, platform, unzipXCodeProjectTask, buildTask, copyResourcesTask)
val installAppTask = createInstallApplicationToSimulatorTask(deviceId, targetSuffix, platform, unzipXCodeProjectTask, buildTask, copyResourcesTask)

return task("${target.name}${StorytaleGradlePlugin.STORYTALE_SOURCESET_SUFFIX}Run") {
group = StorytaleGradlePlugin.STORYTALE_TASK_GROUP
dependsOn(unzipXCodeProjectTask)
dependsOn(installAppTask)

inputs.property("deviceId", deviceId)

doLast {
exec {
workingDir = unzipXCodeProjectTask.outputDir.get().asFile
commandLine(
"/usr/bin/xcrun",
"simctl",
"launch",
simulatorId,
deviceId.get(),
StorytaleGradlePlugin.STORYTALE_NATIVE_PROJECT_PATH
)
}
Expand All @@ -136,31 +142,34 @@ private fun Project.createUnzipResourceTask(extension: StorytaleExtension): Unzi
}
}

private fun Project.createSimulatorRegistrationTask(unzipResourceTask: UnzipResourceTask, targetSuffix: String, updateDeviceId: (String) -> Unit): Task {
private fun Project.createSimulatorRegistrationTask(unzipResourceTask: UnzipResourceTask, targetSuffix: String, deviceIdProperty: Property<String>): Task {
return task("${StorytaleGradlePlugin.STORYTALE_TASK_GROUP}Register$targetSuffix") {
val deviceId: String
group = StorytaleGradlePlugin.STORYTALE_TASK_GROUP
dependsOn(unzipResourceTask)

val simulatorName = StorytaleGradlePlugin.STORYTALE_DEVICE_NAME
val deviceList = execute("/usr/bin/xcrun", "simctl", "list", "devices")
val matches = Regex("""$simulatorName \(([0-9A-F-]+)\) \((\w+)\)""").find(deviceList)
doLast {
val deviceId: String

val existingDeviceId = matches?.groups?.get(1)?.value
val isBooted = matches?.groups?.get(2)?.value == "Booted"
val simulatorName = StorytaleGradlePlugin.STORYTALE_DEVICE_NAME
val deviceList = execute("/usr/bin/xcrun", "simctl", "list", "devices")
val matches = Regex("""$simulatorName \(([0-9A-F-]+)\) \((\w+)\)""").find(deviceList)

if (existingDeviceId == null) {
val availableSimulator = findAvailableIPhoneSimulator()
val availableRuntime = findAvailableIOsRuntime()
val existingDeviceId = matches?.groups?.get(1)?.value
val isBooted = matches?.groups?.get(2)?.value == "Booted"

deviceId = execute("/usr/bin/xcrun", "simctl", "create", simulatorName, availableSimulator, availableRuntime).trim()
} else {
deviceId = existingDeviceId
}
if (existingDeviceId == null) {
val availableSimulator = findAvailableIPhoneSimulator()
val availableRuntime = findAvailableIOsRuntime()

updateDeviceId(deviceId)
deviceId = execute("/usr/bin/xcrun", "simctl", "create", simulatorName, availableSimulator, availableRuntime).trim()
} else {
deviceId = existingDeviceId
}

if (!isBooted) exec { commandLine("/usr/bin/xcrun", "simctl", "boot", deviceId) }
if (!isBooted) exec { commandLine("/usr/bin/xcrun", "simctl", "boot", deviceId) }

deviceIdProperty.set(deviceId)
}
}
}

Expand All @@ -186,7 +195,7 @@ private fun Project.findAvailableIOsRuntime(): String {

private fun Project.createBuildTask(
targetSuffix: String,
deviceId: String,
deviceId: Property<String>,
unzipResourceTask: UnzipResourceTask,
simulatorRegistrationTask: Task,
linkTask: KotlinNativeLink
Expand All @@ -197,6 +206,8 @@ private fun Project.createBuildTask(
dependsOn(simulatorRegistrationTask)
dependsOn(linkTask)

inputs.property("deviceId", deviceId)

val xcodeProjectPath = unzipResourceTask.outputDir.get().asFile
inputs.files(linkTask.outputs.files)
outputs.dir(xcodeProjectPath.resolve(StorytaleGradlePlugin.DERIVED_DATA_DIRECTORY_NAME))
Expand All @@ -214,7 +225,7 @@ private fun Project.createBuildTask(
"-scheme",
StorytaleGradlePlugin.STORYTALE_NATIVE_PROJECT_NAME,
"-destination",
"id=${deviceId}",
"id=${deviceId.get()}",
"-derivedDataPath",
StorytaleGradlePlugin.DERIVED_DATA_DIRECTORY_NAME,
"FRAMEWORK_SEARCH_PATHS=$frameworkPath"
Expand Down Expand Up @@ -256,7 +267,7 @@ private fun Project.createCopyNativeResourcesTask(
}

private fun Project.createInstallApplicationToSimulatorTask(
deviceId: String,
deviceId: Property<String>,
targetSuffix: String,
platform: String,
unzipResourceTask: UnzipResourceTask,
Expand All @@ -269,6 +280,8 @@ private fun Project.createInstallApplicationToSimulatorTask(
dependsOn(buildTask)
dependsOn(copyResourcesTask)

inputs.property("deviceId", deviceId)

doLast {
exec {
workingDir = unzipResourceTask.outputDir.get().asFile
Expand All @@ -277,7 +290,7 @@ private fun Project.createInstallApplicationToSimulatorTask(
"/usr/bin/xcrun",
"simctl",
"install",
deviceId,
deviceId.get(),
appPath
)
}
Expand Down

0 comments on commit de8d675

Please sign in to comment.