Make very basic list devices command
This commit is contained in:
parent
3b3b421fef
commit
9a982c41e4
|
@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ public class XcodeSimulatorTool {
|
|||
case .version:
|
||||
print("xcode-simulator-tool version 0.1")
|
||||
case .listDevices:
|
||||
print("Listing devices")
|
||||
try ListDevicesCommand().run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue