Skip to content

Commit

Permalink
Remove ResourceValues Linux compatibility hack
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 23, 2022
1 parent 19a055f commit dd1ce9d
Showing 1 changed file with 8 additions and 43 deletions.
51 changes: 8 additions & 43 deletions Sources/SwiftFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public func enumerateFiles(withInputURL inputURL: URL,

let queue = concurrent ? DispatchQueue.global(qos: .userInitiated) : completionQueue

func resolveInputURL(_ inputURL: URL, options: Options) -> (URL, ResourceValues, Options)? {
func resolveInputURL(_ inputURL: URL, options: Options) -> (URL, URLResourceValues, Options)? {
let fileOptions = options.fileOptions ?? .default
let inputURL = inputURL.standardizedFileURL
if options.shouldSkipFile(inputURL) {
Expand Down Expand Up @@ -614,48 +614,13 @@ public func expandPath(_ path: String, in directory: String) -> URL {
return URL(fileURLWithPath: directory).appendingPathComponent(path).standardized
}

struct ResourceValues {
let isRegularFile: Bool?
let isDirectory: Bool?
let isAliasFile: Bool?
let isSymbolicLink: Bool?
let creationDate: Date?
let path: String?
}

func getResourceValues(for url: URL, keys: [URLResourceKey]) throws -> ResourceValues {
let manager = FileManager.default
#if os(macOS)
if let resourceValues = try? url.resourceValues(forKeys: Set(keys)) {
return ResourceValues(
isRegularFile: resourceValues.isRegularFile,
isDirectory: resourceValues.isDirectory,
isAliasFile: resourceValues.isAliasFile,
isSymbolicLink: resourceValues.isSymbolicLink,
creationDate: resourceValues.creationDate,
path: resourceValues.path
)
}
if manager.fileExists(atPath: url.path) {
throw FormatError.reading("Failed to read attributes for \(url.path)")
}
#else
var isDirectory: ObjCBool = false
if manager.fileExists(atPath: url.path, isDirectory: &isDirectory) {
guard let attributes = try? manager.attributesOfItem(atPath: url.path) else {
throw FormatError.reading("Failed to read attributes for \(url.path)")
}
let isSymbolicLink = url.resolvingSymlinksInPath() != url
return ResourceValues(
isRegularFile: !isDirectory.boolValue && !isSymbolicLink,
isDirectory: isDirectory.boolValue,
isAliasFile: false,
isSymbolicLink: isSymbolicLink,
creationDate: attributes[.creationDate] as? Date,
path: url.path
)
}
#endif
func getResourceValues(for url: URL, keys: [URLResourceKey]) throws -> URLResourceValues {
if let resourceValues = try? url.resourceValues(forKeys: Set(keys)) {
return resourceValues
}
if FileManager.default.fileExists(atPath: url.path) {
throw FormatError.reading("Failed to read attributes for \(url.path)")
}
throw FormatError.options("File not found at \(url.path)")
}

Expand Down

0 comments on commit dd1ce9d

Please sign in to comment.