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.

Revisions

  1. ctreffs created this gist Jun 2, 2022.
    29 changes: 29 additions & 0 deletions resource_bundle_accessor.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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>")
    }()
    }