Skip to content

Instantly share code, notes, and snippets.

@ctreffs
Created June 2, 2022 08:05
Show Gist options
  • Save ctreffs/ad9d23e08d586cf75e4d1c3bb1b1061f to your computer and use it in GitHub Desktop.
Save ctreffs/ad9d23e08d586cf75e4d1c3bb1b1061f to your computer and use it in GitHub Desktop.
resource_bundle_accessor.swift
import class Foundation.Bundle
private class BundleFinder {}
extension Foundation.Bundle {
/// Returns the resource bundle associated with the current Swift module.
static var module: Bundle = {
let bundleName = "<PACKAGENAME_MODULENAME>"
let candidates = [
// Bundle should be present here when the package is linked into an App.
Bundle.main.resourceURL,
// Bundle should be present here when the package is linked into a framework.
Bundle(for: BundleFinder.self).resourceURL,
// For command-line tools.
Bundle.main.bundleURL,
]
for candidate in candidates {
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle named <PACKAGENAME_MODULENAME>")
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment