Skip to content

Clean up some Darwin platform conditions #8679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Basics/Cancellator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public final class Cancellator: Cancellable, Sendable {
self.register(name: "\(process.arguments.joined(separator: " "))", handler: process.terminate)
}

#if !os(iOS) && !os(watchOS) && !os(tvOS)
#if !canImport(Darwin) || os(macOS)
public func register(_ process: Foundation.Process) -> RegistrationKey? {
self.register(name: "\(process.description)", handler: process.terminate(timeout:))
}
Expand Down Expand Up @@ -245,7 +245,7 @@ extension AsyncProcess {
}
}

#if !os(iOS) && !os(watchOS) && !os(tvOS)
#if !canImport(Darwin) || os(macOS)
extension Foundation.Process {
fileprivate func terminate(timeout: DispatchTime) {
guard self.isRunning else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basics/DispatchTimeInterval+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension DispatchTimeInterval {
return String(format: "%.2f", Double(value) / Double(1_000_000_000)) + "s"
case .never:
return "n/a"
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if canImport(Darwin)
@unknown default:
return "n/a"
#endif
Expand Down
68 changes: 3 additions & 65 deletions Sources/Basics/JSON+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,8 @@ import class Foundation.DateFormatter
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
extension DateFormatter {
public static let iso8601: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}()
}

extension JSONEncoder.DateEncodingStrategy {
public static let customISO8601 = custom {
var container = $1.singleValueContainer()
try container.encode(DateFormatter.iso8601.string(from: $0))
}
}

extension JSONDecoder.DateDecodingStrategy {
public static let customISO8601 = custom {
let container = try $0.singleValueContainer()
let string = try container.decode(String.self)
if let date = DateFormatter.iso8601.date(from: string) {
return date
}
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date: \(string)")
}
}
#endif

extension JSONEncoder.DateEncodingStrategy {
public static var safeISO8601: JSONEncoder.DateEncodingStrategy {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
return .iso8601
} else {
return .customISO8601
}
#else
return .iso8601
#endif
}
}

extension JSONDecoder.DateDecodingStrategy {
public static var safeISO8601: JSONDecoder.DateDecodingStrategy {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
return .iso8601
} else {
return .customISO8601
}
#else
return .iso8601
#endif
}
}

extension JSONDecoder {
public static func makeWithDefaults(dateDecodingStrategy: DateDecodingStrategy = .safeISO8601) -> JSONDecoder {
public static func makeWithDefaults(dateDecodingStrategy: DateDecodingStrategy = .iso8601) -> JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = dateDecodingStrategy
return decoder
Expand All @@ -82,7 +26,7 @@ extension JSONDecoder {
extension JSONEncoder {
public static func makeWithDefaults(
prettified: Bool = true,
dateEncodingStrategy: DateEncodingStrategy = .safeISO8601
dateEncodingStrategy: DateEncodingStrategy = .iso8601
) -> JSONEncoder {
Self.makeWithDefaults(
sortKeys: prettified,
Expand All @@ -96,19 +40,13 @@ extension JSONEncoder {
sortKeys: Bool,
prettyPrint: Bool,
escapeSlashes: Bool,
dateEncodingStrategy: DateEncodingStrategy = .safeISO8601
dateEncodingStrategy: DateEncodingStrategy = .iso8601
) -> JSONEncoder {
let encoder = JSONEncoder()
var outputFormatting: JSONEncoder.OutputFormatting = []

if sortKeys {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
if #available(macOS 10.15, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
outputFormatting.insert(.sortedKeys)
}
#else
outputFormatting.insert(.sortedKeys)
#endif
}
if prettyPrint {
outputFormatting.insert(.prettyPrinted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
delegate: PluginScriptRunnerDelegate,
completion: @escaping (Result<Int32, Error>) -> Void
) {
#if os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin) && !os(macOS)
callbackQueue.async {
completion(.failure(DefaultPluginScriptRunnerError.pluginUnavailable(reason: "subprocess invocations are unavailable on this platform")))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/_InternalTestSupport/MockBuildTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
public struct MockToolchain: PackageModel.Toolchain {
#if os(Windows)
public let librarianPath = AbsolutePath("/fake/path/to/link.exe")
#elseif os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
#elseif canImport(Darwin)
public let librarianPath = AbsolutePath("/fake/path/to/libtool")
#else
public let librarianPath = AbsolutePath("/fake/path/to/llvm-ar")
Expand Down