Implementations of CommonDirs for Linux

This commit is contained in:
Max Howell
2019-01-20 13:03:38 -05:00
parent a98ba37e59
commit 152ad8a8ae
2 changed files with 76 additions and 35 deletions

View File

@@ -1,24 +1,90 @@
import Foundation import Foundation
extension Path { extension Path {
// helper to allow search path and domain mask to be passed in /// Returns a `Path` containing ``FileManager.default.currentDirectoryPath`.
private static func pathFor(searchPathDirectory path: FileManager.SearchPathDirectory, domain: FileManager.SearchPathDomainMask = .userDomainMask) -> Path? { public static var cwd: Path {
guard let pathString = FileManager.default.urls(for: path, in: .userDomainMask).last?.relativeString else { return Path(string: FileManager.default.currentDirectoryPath)
return nil
} }
/// Returns a `Path` representing the root path.
public static var root: Path {
return Path(string: "/")
}
/// Returns a `Path` representing the users home directory
public static var home: Path {
let string: String
#if os(macOS)
if #available(OSX 10.12, *) {
string = FileManager.default.homeDirectoryForCurrentUser.path
} else {
string = NSHomeDirectory()
}
#else
string = NSHomeDirectory()
#endif
return Path(string: string)
}
/// Helper to allow search path and domain mask to be passed in.
private static func path(for searchPath: FileManager.SearchPathDirectory) -> Path {
#if os(Linux)
// the urls(for:in:) function is not implemented on Linux
//TODO strictly we should first try to use the provided binary tool
let foo = { ProcessInfo.processInfo.environment[$0].flatMap(Path.init) ?? $1 }
switch searchPath {
case .documentDirectory:
return Path.home/"Documents"
case .applicationSupportDirectory:
return foo("XDG_DATA_HOME", Path.home/".local/share")
case .cachesDirectory:
return foo("XDG_CACHE_HOME", Path.home/".cache")
default:
fatalError()
}
#else
guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else {
switch searchPath {
case .documentDirectory:
return Path.home/"Documents"
case .applicationSupportDirectory:
return Path.home/"Library/Application Support"
case .cachesDirectory:
return Path.home/"Library/Caches"
default:
fatalError()
}
}
return Path(string: pathString) return Path(string: pathString)
#endif
} }
public static var documents: Path? { /**
return pathFor(searchPathDirectory: .documentDirectory) The root for user documents.
- Note: There is no standard location for documents on Linux, thus we return `~/Documents`.
- Note: You should create a subdirectory before creating any files.
*/
public static var documents: Path {
return path(for: .documentDirectory)
} }
public static var caches: Path? { /**
return pathFor(searchPathDirectory: .cachesDirectory) The root for cache files.
- Note: On Linux this is 'XDG_CACHE_HOME'.
- Note: You should create a subdirectory before creating any files.
*/
public static var caches: Path {
return path(for: .cachesDirectory)
} }
public static var applicationSupport: Path? { /**
return pathFor(searchPathDirectory: .applicationSupportDirectory) For data that supports your running application.
- Note: On Linux is `XDG_DATA_HOME`.
- Note: You should create a subdirectory before creating any files.
*/
public static var applicationSupport: Path {
return path(for: .applicationSupportDirectory)
} }
} }

View File

@@ -16,31 +16,6 @@ public struct Path: Equatable, Hashable, Comparable {
/// The underlying filesystem path /// The underlying filesystem path
public let string: String public let string: String
/// Returns a `Path` containing ``FileManager.default.currentDirectoryPath`.
public static var cwd: Path {
return Path(string: FileManager.default.currentDirectoryPath)
}
/// Returns a `Path` representing the root path.
public static var root: Path {
return Path(string: "/")
}
/// Returns a `Path` representing the users home directory
public static var home: Path {
let string: String
#if os(macOS)
if #available(OSX 10.12, *) {
string = FileManager.default.homeDirectoryForCurrentUser.path
} else {
string = NSHomeDirectory()
}
#else
string = NSHomeDirectory()
#endif
return Path(string: string)
}
/** /**
Returns the filename extension of this path. Returns the filename extension of this path.
- Remark: Implemented via `NSString.pathExtension`. - Remark: Implemented via `NSString.pathExtension`.