|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// A utility for managing Apple simulators. |
| 4 | +enum AppleSimulatorManager { |
| 5 | + /// Lists available simulators. |
| 6 | + /// - Parameter searchTerm: If provided, the simulators will be filtered using |
| 7 | + /// the search term. |
| 8 | + /// - Returns: A list of available simulators matching the search term (if |
| 9 | + /// provided), or a failure if an error occurs. |
| 10 | + static func listAvailableSimulators( |
| 11 | + searchTerm: String? = nil |
| 12 | + ) async throws(Error) -> [Simulator] { |
| 13 | + let data = try await Error.catch(withMessage: .failedToRunSimCTL) { |
| 14 | + try await Process.create( |
| 15 | + "/usr/bin/xcrun", |
| 16 | + arguments: [ |
| 17 | + "simctl", "list", "devices", |
| 18 | + searchTerm, "available", "--json", |
| 19 | + ].compactMap { $0 } |
| 20 | + ).getOutputData(excludeStdError: true) |
| 21 | + } |
| 22 | + |
| 23 | + let simulatorList = try Error.catch(withMessage: .failedToDecodeJSON) { |
| 24 | + try JSONDecoder().decode(SimulatorList.self, from: data) |
| 25 | + } |
| 26 | + |
| 27 | + return simulatorList.devices |
| 28 | + .compactMap { (platform, platformSimulators) -> [Simulator]? in |
| 29 | + guard |
| 30 | + let os = NonMacAppleOS.allCases.first(where: { osCandidate in |
| 31 | + platform.hasPrefix(osCandidate.simulatorRuntimePrefix) |
| 32 | + }) |
| 33 | + else { |
| 34 | + return nil |
| 35 | + } |
| 36 | + |
| 37 | + return platformSimulators.map { simulator in |
| 38 | + Simulator( |
| 39 | + id: simulator.id, |
| 40 | + name: simulator.name, |
| 41 | + isAvailable: simulator.isAvailable, |
| 42 | + isBooted: simulator.state == .booted, |
| 43 | + os: .apple(os) |
| 44 | + ) |
| 45 | + } |
| 46 | + } |
| 47 | + .flatMap { $0 } |
| 48 | + } |
| 49 | + |
| 50 | + /// Boots a simulator. If it's already running, nothing is done. |
| 51 | + /// - Parameter id: The name or id of the simulator to start. |
| 52 | + static func bootSimulator( |
| 53 | + id: String, |
| 54 | + architecture: BuildArchitecture? = nil, |
| 55 | + additionalArguments: [String]? = nil |
| 56 | + ) async throws(Error) { |
| 57 | + var arguments = ["simctl", "boot", id] |
| 58 | + if let architecture { |
| 59 | + arguments += ["--arch=\(architecture.rawValue)"] |
| 60 | + } |
| 61 | + if let additionalArguments { |
| 62 | + arguments += additionalArguments |
| 63 | + } |
| 64 | + |
| 65 | + do { |
| 66 | + // We use getOutputData to access the data on error |
| 67 | + _ = try await Process.create( |
| 68 | + "/usr/bin/xcrun", |
| 69 | + arguments: arguments |
| 70 | + ).getOutputData() |
| 71 | + } catch { |
| 72 | + // If the device is already booted, count it as a success |
| 73 | + guard |
| 74 | + case let .nonZeroExitStatusWithOutput(data, _, _) = error.message, |
| 75 | + let output = String(data: data, encoding: .utf8), |
| 76 | + output.hasSuffix("Unable to boot device in current state: Booted\n") |
| 77 | + else { |
| 78 | + throw Error(.failedToRunSimCTL, cause: error) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// Launches an app on the simulator (the app must already be installed). |
| 84 | + /// - Parameters: |
| 85 | + /// - bundleIdentifier: The app's bundle identifier. |
| 86 | + /// - simulatorId: The name or id of the simulator to launch in. |
| 87 | + /// - connectConsole: If `true`, the function will block and the current |
| 88 | + /// process will print the stdout and stderr of the running app. |
| 89 | + /// - arguments: Command line arguments to pass to the app. |
| 90 | + /// - environmentVariables: Additional environment variables to pass to the |
| 91 | + /// app. |
| 92 | + static func launchApp( |
| 93 | + _ bundleIdentifier: String, |
| 94 | + simulatorId: String, |
| 95 | + connectConsole: Bool, |
| 96 | + arguments: [String], |
| 97 | + environmentVariables: [String: String] |
| 98 | + ) async throws(Error) { |
| 99 | + let process = Process.create( |
| 100 | + "/usr/bin/xcrun", |
| 101 | + arguments: [ |
| 102 | + "simctl", "launch", connectConsole ? "--console-pty" : nil, |
| 103 | + simulatorId, bundleIdentifier, |
| 104 | + ].compactMap { $0 } + arguments, |
| 105 | + runSilentlyWhenNotVerbose: false |
| 106 | + ) |
| 107 | + |
| 108 | + // TODO: Ensure that environment variables are passed correctly |
| 109 | + var prefixedVariables: [String: String] = [:] |
| 110 | + for (key, value) in environmentVariables { |
| 111 | + prefixedVariables["SIMCTL_CHILD_" + key] = value |
| 112 | + } |
| 113 | + |
| 114 | + process.addEnvironmentVariables(prefixedVariables) |
| 115 | + |
| 116 | + try await Error.catch(withMessage: .failedToRunSimCTL) { |
| 117 | + try await process.runAndWait() |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Installs an app on the simulator. |
| 122 | + /// - Parameters: |
| 123 | + /// - bundle: The app bundle to install. |
| 124 | + /// - simulatorId: The name or id of the simulator to install on. |
| 125 | + /// - Returns: A failure if an error occurs. |
| 126 | + static func installApp( |
| 127 | + _ bundle: URL, |
| 128 | + simulatorId: String |
| 129 | + ) async throws(Error) { |
| 130 | + try await Error.catch(withMessage: .failedToRunSimCTL) { |
| 131 | + try await Process.create( |
| 132 | + "/usr/bin/xcrun", |
| 133 | + arguments: [ |
| 134 | + "simctl", "install", simulatorId, bundle.path, |
| 135 | + ] |
| 136 | + ).runAndWait() |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// Opens the latest booted simulator in the simulator app. |
| 141 | + /// - Returns: A failure if an error occurs. |
| 142 | + static func openSimulatorApp() async throws(Error) { |
| 143 | + try await Error.catch(withMessage: .failedToOpenSimulator) { |
| 144 | + try await Process.create( |
| 145 | + "/usr/bin/open", |
| 146 | + arguments: [ |
| 147 | + "-a", "Simulator", |
| 148 | + ] |
| 149 | + ).runAndWait() |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments