Bundle extensions don’t return optional Paths

Rationale: Paths are not guaranteed to exist, the Bundle functions return optional if the path doesn't exist. So we'll provide a sensible default instead and you need to check the result exists at some point instead.

This makes more elegant chains, the chain will fail when you operate on it, but you don’t have to do a check for optional first. Or risk a bang.
This commit is contained in:
Max Howell
2019-01-26 15:20:09 -05:00
parent 93e2701950
commit 83c83dcaba

View File

@@ -10,13 +10,31 @@ public extension Bundle {
} }
/// Returns the path for the shared-frameworks directory in this bundle. /// Returns the path for the shared-frameworks directory in this bundle.
var sharedFrameworks: Path? { var sharedFrameworks: Path {
return sharedFrameworksPath.flatMap(Path.init) var `default`: Path {
#if os(macOS)
return path.join("Contents/Frameworks")
#elseif os(Linux)
return path.join("lib")
#else
return path.join("Frameworks")
#endif
}
return sharedFrameworksPath.flatMap(Path.init) ?? `default`
} }
/// Returns the path for the resources directory in this bundle. /// Returns the path for the resources directory in this bundle.
var resources: Path? { var resources: Path {
return resourcePath.flatMap(Path.init) var `default`: Path {
#if os(macOS)
return path.join("Contents/Resources")
#elseif os(Linux)
return path.join("share")
#else
return path
#endif
}
return resourcePath.flatMap(Path.init) ?? `default`
} }
/// Returns the path for this bundle. /// Returns the path for this bundle.