From 83c83dcaba947205b470f7a31416542e2d41cba5 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sat, 26 Jan 2019 15:20:09 -0500 Subject: [PATCH] =?UTF-8?q?Bundle=20extensions=20don=E2=80=99t=20return=20?= =?UTF-8?q?optional=20Paths?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/Extensions.swift | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Sources/Extensions.swift b/Sources/Extensions.swift index be9ec0a..f03e87e 100644 --- a/Sources/Extensions.swift +++ b/Sources/Extensions.swift @@ -10,13 +10,31 @@ public extension Bundle { } /// Returns the path for the shared-frameworks directory in this bundle. - var sharedFrameworks: Path? { - return sharedFrameworksPath.flatMap(Path.init) + var sharedFrameworks: Path { + 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. - var resources: Path? { - return resourcePath.flatMap(Path.init) + var resources: Path { + 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.