From b6ef2bfde986007722b0c7ffbc64b45ef1a91c4e Mon Sep 17 00:00:00 2001 From: Lukas Romsicki <3951690+lfroms@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:58:31 -0400 Subject: [PATCH] Terminate app on iOS simulator before install to address iOS 18 change (#18) --- .../AppleDeviceKit/Extensions/ShellCommand+SimCtl.swift | 4 ++++ TophatModules/Sources/AppleDeviceKit/Simulator+Device.swift | 3 +++ TophatModules/Sources/AppleDeviceKit/Utilities/SimCtl.swift | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/TophatModules/Sources/AppleDeviceKit/Extensions/ShellCommand+SimCtl.swift b/TophatModules/Sources/AppleDeviceKit/Extensions/ShellCommand+SimCtl.swift index 4ad9273..3223ec9 100644 --- a/TophatModules/Sources/AppleDeviceKit/Extensions/ShellCommand+SimCtl.swift +++ b/TophatModules/Sources/AppleDeviceKit/Extensions/ShellCommand+SimCtl.swift @@ -27,6 +27,7 @@ enum SimCtlCommand { case boot(device: String) case install(device: String, bundleUrl: URL) case launch(device: String, bundleIdentifier: String, arguments: [String]) + case terminate(device: String, bundleIdentifier: String) } extension SimCtlCommand: ShellCommand { @@ -47,6 +48,9 @@ extension SimCtlCommand: ShellCommand { case .launch(let device, let bundleIdentifier, let arguments): return ["simctl", "launch", device, bundleIdentifier] + arguments + + case .terminate(let device, let bundleIdentifier): + return ["simctl", "terminate", device, bundleIdentifier] } } } diff --git a/TophatModules/Sources/AppleDeviceKit/Simulator+Device.swift b/TophatModules/Sources/AppleDeviceKit/Simulator+Device.swift index cfd028e..08281f3 100644 --- a/TophatModules/Sources/AppleDeviceKit/Simulator+Device.swift +++ b/TophatModules/Sources/AppleDeviceKit/Simulator+Device.swift @@ -56,6 +56,9 @@ extension Simulator: Device { func install(application: Application) throws { do { + // Failure to terminate is permitted as the application may not be running. + try? SimCtl.terminate(udid: id, bundleIdentifier: application.bundleIdentifier) + try SimCtl.install(udid: id, bundleUrl: application.url) } catch { throw DeviceError.failedToInstallApp(bundleUrl: application.url, deviceType: .virtual) diff --git a/TophatModules/Sources/AppleDeviceKit/Utilities/SimCtl.swift b/TophatModules/Sources/AppleDeviceKit/Utilities/SimCtl.swift index 2c28d21..03f724d 100644 --- a/TophatModules/Sources/AppleDeviceKit/Utilities/SimCtl.swift +++ b/TophatModules/Sources/AppleDeviceKit/Utilities/SimCtl.swift @@ -37,6 +37,10 @@ final class SimCtl { static func launch(udid: String, bundleIdentifier: String, arguments: [String]) throws { try run(command: .simCtl(.launch(device: udid, bundleIdentifier: bundleIdentifier, arguments: arguments)), log: log) } + + static func terminate(udid: String, bundleIdentifier: String) throws { + try run(command: .simCtl(.terminate(device: udid, bundleIdentifier: bundleIdentifier)), log: log) + } } private extension Simulator {