Skip to content

Commit

Permalink
refactor: drop awk dep and isolate getting current machine uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
itsramiel committed Aug 28, 2024
1 parent 56d520a commit f60b1bd
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DeviceService: DeviceServiceProtocol {
private enum ProcessPaths: String {
case xcrun = "/usr/bin/xcrun"
case xcodeSelect = "/usr/bin/xcode-select"
case systemProfiler = "/usr/sbin/system_profiler"
}

private enum BundleURL: String {
Expand Down Expand Up @@ -312,18 +313,37 @@ extension DeviceService {
return simulators + devices
}

static var currentMachineUUID: String? {
guard let output = try? shellOut(
to: ProcessPaths.systemProfiler.rawValue,
arguments: ["SPHardwareDataType"]
) else { return nil }

let uuidIdx = 1
for line in output.components(separatedBy: "\n") {
guard let match = line.match("Hardware UUID: ([\\S]+)").first else {
continue
}

if match.count >= 2 {
return match[uuidIdx]
}
}

return nil
}

static func getIOSPhysicalDevices() throws -> [Device] {
guard let currentMachineUUID else { return [] }

// Command also returns current machine as a device, we filter it before returning
let devicesOutput = try shellOut(
to: ProcessPaths.xcrun.rawValue,
arguments: ["xctrace", "list", "devices"]
)
let splitted = devicesOutput.components(separatedBy: "\n")

let currentDeviceUuid = try shellOut(
to: "system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }'"
)

return parseIOSPhysicalDevices(result: splitted).filter { $0.identifier != currentDeviceUuid }
return parseIOSPhysicalDevices(result: splitted).filter { $0.identifier != currentMachineUUID }
}

static func getIOSSimulators() throws -> [Device] {
Expand Down

0 comments on commit f60b1bd

Please sign in to comment.