-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Xcode 9 support With the large number of simulator changes in Xcode 9 came a change where the notification we're hijacking needs to send along the UDIDs of the simulators where the change should take effect. This is because you can have multiple running simulators with different locations. To do this, for now, we're just reading the output of `simctl list` and sending all booted UDIDs along. * Use JSON output instead
- Loading branch information
Keith Smiley
authored
Aug 10, 2017
1 parent
dad22eb
commit a52b6bf
Showing
3 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
|
||
enum SimulatorFetchError: String, Error { | ||
case simctlFailed = "Running `simctl list` failed" | ||
case failedToReadOutput = "Failed to read output from simctl" | ||
case noBootedSimulators = "No simulators are currently booted" | ||
} | ||
|
||
func getBootedSimulators() throws -> [String] { | ||
let task = Process() | ||
task.launchPath = "/usr/bin/xcrun" | ||
task.arguments = ["simctl", "list", "-j", "devices"] | ||
|
||
let pipe = Pipe() | ||
task.standardOutput = pipe | ||
|
||
task.launch() | ||
|
||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
task.waitUntilExit() | ||
pipe.fileHandleForReading.closeFile() | ||
|
||
if task.terminationStatus != 0 { | ||
throw SimulatorFetchError.simctlFailed | ||
} | ||
|
||
guard let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any] else { | ||
throw SimulatorFetchError.failedToReadOutput | ||
} | ||
|
||
let devices = json["devices"] as? [String: [[String: String]]] ?? [:] | ||
let bootedIDs = devices | ||
.flatMap { $1 } | ||
.filter { $0["state"] == "Booted" } | ||
.flatMap { $0["udid"] } | ||
|
||
if bootedIDs.isEmpty { | ||
throw SimulatorFetchError.noBootedSimulators | ||
} | ||
|
||
return bootedIDs | ||
} |