|
| 1 | +import class Foundation.Bundle |
| 2 | +import class Foundation.ProcessInfo |
| 3 | +import struct Foundation.URL |
| 4 | + |
| 5 | +private class BundleFinder {} |
| 6 | + |
| 7 | +extension Foundation.Bundle { |
| 8 | + /// Returns the resource bundle associated with the current Swift module. |
| 9 | + static let module: Bundle = { |
| 10 | + let bundleName = "LoopAlgorithmToPython_LoopAlgorithmToPythonTests" |
| 11 | + |
| 12 | + let overrides: [URL] |
| 13 | + #if DEBUG |
| 14 | + // The 'PACKAGE_RESOURCE_BUNDLE_PATH' name is preferred since the expected value is a path. The |
| 15 | + // check for 'PACKAGE_RESOURCE_BUNDLE_URL' will be removed when all clients have switched over. |
| 16 | + // This removal is tracked by rdar://107766372. |
| 17 | + if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"] |
| 18 | + ?? ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_URL"] { |
| 19 | + overrides = [URL(fileURLWithPath: override)] |
| 20 | + } else { |
| 21 | + overrides = [] |
| 22 | + } |
| 23 | + #else |
| 24 | + overrides = [] |
| 25 | + #endif |
| 26 | + |
| 27 | + print("BUNDLEFILE") |
| 28 | + |
| 29 | + let candidates = overrides + [ |
| 30 | + // Bundle should be present here when the package is linked into an App. |
| 31 | + Bundle.main.resourceURL, |
| 32 | + |
| 33 | + // Bundle should be present here when the package is linked into a framework. |
| 34 | + Bundle(for: BundleFinder.self).resourceURL, |
| 35 | + |
| 36 | + // For command-line tools. |
| 37 | + Bundle.main.bundleURL, |
| 38 | + ] |
| 39 | + |
| 40 | + for candidate in candidates { |
| 41 | + let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") |
| 42 | + print(bundlePath) |
| 43 | + if let bundle = bundlePath.flatMap(Bundle.init(url:)) { |
| 44 | + print("BUNDLE:", bundle) |
| 45 | + return bundle |
| 46 | + } |
| 47 | + } |
| 48 | + fatalError("unable to find bundle named LoopAlgorithmToPython_LoopAlgorithmToPythonTests") |
| 49 | + }() |
| 50 | +} |
0 commit comments