Make very basic list devices command

This commit is contained in:
Simon Kågedal Reimer 2019-05-04 20:59:55 +02:00
parent 3b3b421fef
commit 9a982c41e4
4 changed files with 44 additions and 3 deletions

View File

@ -0,0 +1,14 @@
import Foundation
import SPMUtility
struct ListDevicesCommand {
func run() throws {
let allDevices = try Simctl.listDevices()
for (runtime, devices) in allDevices.devices {
print("\(runtime):")
for device in devices {
print(" - \(device.name)")
}
}
}
}

View File

@ -7,7 +7,26 @@ struct Simctl {
case listDevices(errorOutput: String)
}
static func listDevices() throws -> Data {
struct DeviceList: Codable {
var devices: [String: [Device]]
}
struct Device: Codable {
enum State: String, Codable {
case shutdown = "Shutdown"
case booted = "Booted"
}
var isAvailable: Bool
var name: String
var udid: String
var state: State
}
static func listDevices() throws -> DeviceList {
return try parseDeviceList(readListDevices())
}
static func readListDevices() throws -> Data {
let process = Process(
args: "xcrun", "simctl", "list", "--json", "devices"
)
@ -20,4 +39,9 @@ struct Simctl {
return Data(try result.output.dematerialize())
}
static func parseDeviceList(_ data: Data) throws -> DeviceList {
let decoder = JSONDecoder()
return try decoder.decode(DeviceList.self, from: data)
}
}

View File

@ -36,7 +36,7 @@ public class XcodeSimulatorTool {
case .version:
print("xcode-simulator-tool version 0.1")
case .listDevices:
print("Listing devices")
try ListDevicesCommand().run()
}
}
}

View File

@ -4,7 +4,7 @@ import XCTest
class SimctlTests: XCTestCase {
func testListDevices() throws {
let jsonData = try Simctl.listDevices()
let jsonData: Data = try Simctl.readListDevices()
let obj = try JSONSerialization.jsonObject(with: jsonData, options: [])
XCTAssertTrue(obj is [String: Any])
guard let dict = obj as? [String: Any] else {
@ -12,5 +12,8 @@ class SimctlTests: XCTestCase {
}
XCTAssertEqual(dict.keys.count, 1)
XCTAssertEqual(dict.keys.first, "devices")
XCTAssertNoThrow(try Simctl.parseDeviceList(jsonData))
_ = try Simctl.parseDeviceList(jsonData)
}
}