From ae27f8fa516818d32d7479c6c35483cca78a70e2 Mon Sep 17 00:00:00 2001 From: Honghao Zhang Date: Mon, 15 Jan 2024 14:30:02 -0800 Subject: [PATCH 1/2] bump up swift-tools-version to 5.8, for privacy manifest --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 0bf4ca4..b37b85a 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.0 +// swift-tools-version:5.8 //===----------------------------------------------------------------------===// // // This source file is part of the DeviceKit open source project From 4b4edb00c19d17566e05e286d7634b3c064cc332 Mon Sep 17 00:00:00 2001 From: Honghao Zhang Date: Mon, 15 Jan 2024 15:06:00 -0800 Subject: [PATCH 2/2] fixes macOS build --- Source/Device.generated.swift | 42 ++++++++++++++++++++++++++++++----- Source/Device.swift.gyb | 42 ++++++++++++++++++++++++++++++----- Tests/Tests.swift | 13 +++++++++++ 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/Source/Device.generated.swift b/Source/Device.generated.swift index f492b0b..1416504 100644 --- a/Source/Device.generated.swift +++ b/Source/Device.generated.swift @@ -628,6 +628,8 @@ public enum Device { case "i386", "x86_64", "arm64": return simulator(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "watchOS")) default: return unknown(identifier) } + #else + return unknown(identifier) #endif } @@ -879,6 +881,8 @@ public enum Device { } #elseif os(tvOS) return (width: -1, height: -1) + #else + return (width: -1, height: -1) #endif } @@ -1128,6 +1132,8 @@ public enum Device { return allTVs #elseif os(watchOS) return allWatches + #else + return [] #endif } @@ -1189,8 +1195,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().name - #else + #elseif canImport(UIKit) return UIDevice.current.name + #else + return nil #endif } @@ -1205,8 +1213,10 @@ public enum Device { } else { return UIDevice.current.systemName } - #else + #elseif canImport(UIKit) return UIDevice.current.systemName + #else + return nil #endif } @@ -1215,8 +1225,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().systemVersion - #else + #elseif canImport(UIKit) return UIDevice.current.systemVersion + #else + return nil #endif } @@ -1225,8 +1237,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().model - #else + #elseif canImport(UIKit) return UIDevice.current.model + #else + return nil #endif } @@ -1235,8 +1249,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().localizedModel - #else + #elseif canImport(UIKit) return UIDevice.current.localizedModel + #else + return nil #endif } @@ -1355,6 +1371,8 @@ public enum Device { } #elseif os(tvOS) return nil + #else + return nil #endif } @@ -1506,6 +1524,11 @@ extension Device: CustomStringConvertible { case .simulator(let model): return "Simulator (\(model.description))" case .unknown(let identifier): return identifier } + #else + switch self { + case .simulator(let model): return "Simulator (\(model.safeDescription))" + case .unknown(let identifier): return identifier + } #endif } @@ -1634,6 +1657,11 @@ extension Device: CustomStringConvertible { case .simulator(let model): return "Simulator (\(model.safeDescription))" case .unknown(let identifier): return identifier } + #else + switch self { + case .simulator(let model): return "Simulator (\(model.safeDescription))" + case .unknown(let identifier): return identifier + } #endif } @@ -2268,6 +2296,8 @@ extension Device { case .simulator(let model): return model.cpu case .unknown: return .unknown } + #else + return .unknown #endif } } @@ -2317,6 +2347,8 @@ extension Device.CPU: CustomStringConvertible { case .s9: return "S9" case .unknown: return "unknown" } + #else + return "unknown" #endif } } diff --git a/Source/Device.swift.gyb b/Source/Device.swift.gyb index 8b0fa98..13679fd 100644 --- a/Source/Device.swift.gyb +++ b/Source/Device.swift.gyb @@ -418,6 +418,8 @@ public enum Device { case "i386", "x86_64", "arm64": return simulator(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "watchOS")) default: return unknown(identifier) } + #else + return unknown(identifier) #endif } @@ -479,6 +481,8 @@ public enum Device { } #elseif os(tvOS) return (width: -1, height: -1) + #else + return (width: -1, height: -1) #endif } @@ -728,6 +732,8 @@ public enum Device { return allTVs #elseif os(watchOS) return allWatches + #else + return [] #endif } @@ -789,8 +795,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().name - #else + #elseif canImport(UIKit) return UIDevice.current.name + #else + return nil #endif } @@ -805,8 +813,10 @@ public enum Device { } else { return UIDevice.current.systemName } - #else + #elseif canImport(UIKit) return UIDevice.current.systemName + #else + return nil #endif } @@ -815,8 +825,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().systemVersion - #else + #elseif canImport(UIKit) return UIDevice.current.systemVersion + #else + return nil #endif } @@ -825,8 +837,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().model - #else + #elseif canImport(UIKit) return UIDevice.current.model + #else + return nil #endif } @@ -835,8 +849,10 @@ public enum Device { guard isCurrent else { return nil } #if os(watchOS) return WKInterfaceDevice.current().localizedModel - #else + #elseif canImport(UIKit) return UIDevice.current.localizedModel + #else + return nil #endif } @@ -860,6 +876,8 @@ public enum Device { } #elseif os(tvOS) return nil + #else + return nil #endif } @@ -915,6 +933,11 @@ extension Device: CustomStringConvertible { case .simulator(let model): return "Simulator (\(model.description))" case .unknown(let identifier): return identifier } + #else + switch self { + case .simulator(let model): return "Simulator (\(model.safeDescription))" + case .unknown(let identifier): return identifier + } #endif } @@ -947,6 +970,11 @@ extension Device: CustomStringConvertible { case .simulator(let model): return "Simulator (\(model.safeDescription))" case .unknown(let identifier): return identifier } + #else + switch self { + case .simulator(let model): return "Simulator (\(model.safeDescription))" + case .unknown(let identifier): return identifier + } #endif } @@ -1435,6 +1463,8 @@ extension Device { case .simulator(let model): return model.cpu case .unknown: return .unknown } + #else + return .unknown #endif } } @@ -1457,6 +1487,8 @@ extension Device.CPU: CustomStringConvertible { % end case .unknown: return "unknown" } + #else + return "unknown" #endif } } diff --git a/Tests/Tests.swift b/Tests/Tests.swift index eebfb77..1c5928e 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -17,20 +17,31 @@ class DeviceKitTests: XCTestCase { let device = Device.current func testDeviceSimulator() { + #if os(macOS) + XCTAssertFalse(device.isOneOf(Device.allSimulators)) + #else XCTAssertTrue(device.isOneOf(Device.allSimulators)) + #endif } func testIsSimulator() { + #if os(macOS) + XCTAssertFalse(device.isSimulator) + #else XCTAssertTrue(device.isSimulator) + #endif } func testDeviceDescription() { + #if os(macOS) + #else XCTAssertTrue(device.description.hasPrefix("Simulator")) XCTAssertTrue(device.description.contains("iPhone") || device.description.contains("iPad") || device.description.contains("iPod") || device.description.contains("TV") || device.description.contains("Apple Watch")) + #endif } func testIsCanvas() { @@ -40,6 +51,8 @@ class DeviceKitTests: XCTestCase { let otherDevice: Device = device == .appleTVHD ? .appleTV4K : .appleTVHD #elseif os(watchOS) let otherDevice: Device = device == .appleWatchUltra ? .appleWatchSeries8_41mm : .appleWatchUltra + #else + let otherDevice: Device = .unknown("mac") #endif XCTAssertEqual(otherDevice.isCanvas, nil) XCTAssertEqual(device.isCanvas, false)