-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathTuistBundle+RevenueCat.swift
53 lines (50 loc) · 2.47 KB
/
TuistBundle+RevenueCat.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// swiftlint:disable:this file_name
// swiftlint:disable all
// swift-format-ignore-file
// swiftformat:disable all
import Foundation
// MARK: - Swift Bundle Accessor - for SPM
private class BundleFinder {}
extension Foundation.Bundle {
/// Since RevenueCat is a static framework, the bundle containing the resources is copied into the final product.
static let module: Bundle = {
let bundleName = "RevenueCat_RevenueCat"
let bundleFinderResourceURL = Bundle(for: BundleFinder.self).resourceURL
var candidates = [
Bundle.main.resourceURL,
bundleFinderResourceURL,
Bundle.main.bundleURL,
]
// This is a fix to make Previews work with bundled resources.
// Logic here is taken from SPM's generated `resource_bundle_accessors.swift` file,
// which is located under the derived data directory after building the project.
if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"] {
candidates.append(URL(fileURLWithPath: override))
// Deleting derived data and not rebuilding the frameworks containing resources may result in a state
// where the bundles are only available in the framework's directory that is actively being previewed.
// Since we don't know which framework this is, we also need to look in all the framework subpaths.
if let subpaths = try? Foundation.FileManager.default.contentsOfDirectory(atPath: override) {
for subpath in subpaths {
if subpath.hasSuffix(".framework") {
candidates.append(URL(fileURLWithPath: override + "/" + subpath))
}
}
}
}
// This is a fix to make unit tests work with bundled resources.
// Making this change allows unit tests to search one directory up for a bundle.
// More context can be found in this PR: https://github.com/tuist/tuist/pull/6895
#if canImport(XCTest)
candidates.append(bundleFinderResourceURL?.appendingPathComponent(".."))
#endif
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 RevenueCat_RevenueCat")
}()
}
// swiftformat:enable all
// swiftlint:enable all