Skip to content

Instantly share code, notes, and snippets.

@kam800
Created May 26, 2018 12:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kam800/ae364706988526abf5b09bf959c5646e to your computer and use it in GitHub Desktop.
Save kam800/ae364706988526abf5b09bf959c5646e to your computer and use it in GitHub Desktop.
import Foundation
public extension Bundle {
private final class Marker {}
// Looking for bundle involves a lot of IO
private static let cache: NSCache<NSString, Bundle> = NSCache<NSString, Bundle>()
public static func resourcesBundle(forModuleName moduleName: String) -> Bundle {
if let cachedBundle = cache.object(forKey: moduleName as NSString) {
return cachedBundle
}
let bundle = resolved(forModuleName: moduleName)
cache.setObject(bundle, forKey: moduleName as NSString)
return bundle
}
private static func resolved(forModuleName moduleName: String) -> Bundle {
return Bundle.main.anyNested(forModuleName: moduleName)
?? Bundle.main
}
private func anyNested(forModuleName moduleName: String) -> Bundle? {
if let framework = nestedFramework(name: moduleName) {
return framework.nestedResourceBundle(moduleName: moduleName)
?? framework
}
return nestedResourceBundle(moduleName: moduleName)
}
private func nestedFramework(name: String) -> Bundle? {
return path(forResource: name, ofType: "framework", inDirectory: "Frameworks")
.flatMap(Bundle.init(path:))
}
private func nestedResourceBundle(moduleName: String) -> Bundle? {
guard let resourceBundlePath =
path(forResource: moduleName + "Resources", ofType: "bundle")
?? path(forResource: moduleName, ofType: "bundle")
else { return nil }
return Bundle(path: resourceBundlePath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment