From 12c7b348d68269081bdf08845d0a82b43e54aae0 Mon Sep 17 00:00:00 2001 From: Niall McCormack Date: Sat, 19 Jan 2019 00:24:20 +0800 Subject: [PATCH] added hooks for common directories - Documents, Caches and Application Support --- Sources/Path+CommonDirectories.swift | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Sources/Path+CommonDirectories.swift diff --git a/Sources/Path+CommonDirectories.swift b/Sources/Path+CommonDirectories.swift new file mode 100644 index 0000000..1878170 --- /dev/null +++ b/Sources/Path+CommonDirectories.swift @@ -0,0 +1,24 @@ +import Foundation + +extension Path { + // helper to allow search path and domain mask to be passed in + private static func pathFor(searchPathDirectory path: FileManager.SearchPathDirectory, domain: FileManager.SearchPathDomainMask = .userDomainMask) -> Path? { + guard let pathString = FileManager.default.urls(for: path, in: .userDomainMask).last?.relativeString else { + return nil + } + + return Path(string: pathString) + } + + public static var documents: Path? { + return pathFor(searchPathDirectory: .documentDirectory) + } + + public static var caches: Path? { + return pathFor(searchPathDirectory: .cachesDirectory) + } + + public static var applicationSupport: Path? { + return pathFor(searchPathDirectory: .applicationSupportDirectory) + } +}