import Darwin import Foundation enum NamePaths { static func lexical(_ path: URL) -> URL { var components: [Substring] = [] for component in path.path.split(separator: "/") { if component == "." { continue } if component == ".." { if !components.isEmpty { components.removeLast() }; continue } components.append(component) } return URL(fileURLWithPath: "/" + components.joined(separator: "/")) } static func canonical(_ path: URL) throws -> URL { guard let resolved = realpath(path.path, nil) else { throw DevError("Could not resolve \(path.path): \(String(cString: strerror(errno)))") } defer { free(resolved) } return URL(fileURLWithPath: String(cString: resolved)) } static func existingParent(_ path: URL) throws -> URL? { guard let resolved = realpath(path.path, nil) else { if errno == ENOENT || errno == ENOTDIR { return nil } throw DevError("Could not resolve \(path.path): \(String(cString: strerror(errno)))") } defer { free(resolved) } return URL(fileURLWithPath: String(cString: resolved)) } static func expanded(_ value: String) -> URL { if value == "~" || value.hasPrefix("~/"), let home = ProcessInfo.processInfo.environment["HOME"] { return URL(fileURLWithPath: home + value.dropFirst()) } return URL(fileURLWithPath: NSString(string: value).expandingTildeInPath) } }