Skip to content

Commit c27daf6

Browse files
authored
fix: Update bundle name generator to match SPM (#1183)
SPM generated bundles appear to have the format `<package_name>_<module_name>.bundle` rather than `<module_name>_<module_name>.bundle`. I added the same test with the same name to both the `resources_example` bazel tests and directly to the `CoolUI` example package to prove that the generated names match.
1 parent 3e16d4d commit c27daf6

File tree

6 files changed

+93
-10
lines changed

6 files changed

+93
-10
lines changed

examples/resources_example/Tests/MyAppTests/MyAppTests.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import SwiftUI
66
import XCTest
77

88
class MyAppTests: XCTestCase {
9-
func test_CoolStuf_title_doesNotFail() throws {
9+
func test_CoolStuff_title_doesNotFail() throws {
1010
let actual = CoolStuff.title()
1111
XCTAssertNotNil(actual)
1212
}
1313

14+
15+
func test_CoolStuff_bundleName() {
16+
let bundle = Bundle.bundle(named: "package-with-resources_CoolUI")
17+
XCTAssertNotNil(bundle)
18+
}
19+
1420
func test_AppLovinSDKResources() throws {
1521
let url = ALResourceManager.resourceBundleURL
1622
XCTAssertNotNil(url)
@@ -46,3 +52,33 @@ class MyAppTests: XCTestCase {
4652
IterableLogUtil.sharedInstance = nil
4753
}
4854
}
55+
56+
private class BundleFinder {}
57+
58+
extension Foundation.Bundle {
59+
static func bundle(named bundleName: String) -> Bundle? {
60+
let candidates = [
61+
// Bundle should be present here when the package is linked into an App.
62+
Bundle.main.resourceURL,
63+
64+
// Bundle should be present here when the package is linked into a framework.
65+
Bundle(for: BundleFinder.self).resourceURL,
66+
67+
// For command-line tools.
68+
Bundle.main.bundleURL,
69+
70+
// Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/").
71+
Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(),
72+
Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(),
73+
]
74+
75+
for candidate in candidates {
76+
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
77+
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
78+
return bundle
79+
}
80+
}
81+
82+
return nil
83+
}
84+
}

examples/resources_example/third_party/package_with_resources/Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ let package = Package(
1515
name: "CoolUI",
1616
resources: [.process("Resources")]
1717
),
18+
.testTarget(
19+
name: "CoolUITests",
20+
dependencies: ["CoolUI"]
21+
)
1822
]
1923
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
import XCTest
3+
4+
@testable import CoolUI
5+
6+
class BundleNameTest: XCTestCase {
7+
func testBundleName() {
8+
let bundle = Bundle.bundle(named: "package-with-resources_CoolUI")
9+
XCTAssertNotNil(bundle)
10+
}
11+
}
12+
13+
private class BundleFinder {}
14+
15+
extension Foundation.Bundle {
16+
static func bundle(named bundleName: String) -> Bundle? {
17+
let candidates = [
18+
// Bundle should be present here when the package is linked into an App.
19+
Bundle.main.resourceURL,
20+
21+
// Bundle should be present here when the package is linked into a framework.
22+
Bundle(for: BundleFinder.self).resourceURL,
23+
24+
// For command-line tools.
25+
Bundle.main.bundleURL,
26+
27+
// Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/").
28+
Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(),
29+
Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(),
30+
]
31+
32+
for candidate in candidates {
33+
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
34+
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
35+
return bundle
36+
}
37+
}
38+
39+
return nil
40+
}
41+
}

swiftpkg/internal/pkginfo_targets.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,20 @@ def _swift_hint_label_name(target_name):
148148
"""
149149
return target_name + _swift_hint_suffix
150150

151-
def _resource_bundle_name(module_name):
151+
def _resource_bundle_name(package_name, module_name):
152152
"""Returns the `bundle_name` for the module.
153153
154154
For Swift packages, it appears the bundle name is of the format
155-
`<module_name>_<module_name>`.
155+
`<package_name>_<module_name>`.
156156
157157
Args:
158+
package_name: The package name.
158159
module_name: The module name.
159160
160161
Returns:
161162
The `bundle_name` of the `apple_resource_bundle` as a `string`.
162163
"""
163-
return module_name + "_" + module_name
164+
return package_name + "_" + module_name
164165

165166
def _resource_bundle_label_name(target_name):
166167
"""Returns the name of the related `apple_resource_bundle` target.

swiftpkg/internal/swiftpkg_build_files.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,15 +543,16 @@ def _handle_target_resources(
543543

544544
return _apple_resource_bundle(
545545
target,
546+
pkg_ctx.pkg_info.name,
546547
pkg_ctx.pkg_info.default_localization,
547548
include_swift_accessor = include_swift_accessor,
548549
include_objc_accessor = include_objc_accessor,
549550
)
550551

551-
def _apple_resource_bundle(target, default_localization, include_swift_accessor, include_objc_accessor):
552+
def _apple_resource_bundle(target, package_name, default_localization, include_swift_accessor, include_objc_accessor):
552553
bzl_target_name = pkginfo_targets.bazel_label_name(target)
553554
bundle_label_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name)
554-
bundle_name = pkginfo_targets.resource_bundle_name(target.c99name)
555+
bundle_name = pkginfo_targets.resource_bundle_name(package_name, target.c99name)
555556
infoplist_name = pkginfo_targets.resource_bundle_infoplist_label_name(
556557
bzl_target_name,
557558
)

swiftpkg/tests/swiftpkg_build_files_tests.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,15 +824,15 @@ load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "resource_bundle_a
824824
825825
apple_resource_bundle(
826826
name = "SwiftLibraryWithFilePathResource.rspm_resource_bundle",
827-
bundle_name = "SwiftLibraryWithFilePathResource_SwiftLibraryWithFilePathResource",
827+
bundle_name = "MyPackage_SwiftLibraryWithFilePathResource",
828828
infoplists = [":SwiftLibraryWithFilePathResource.rspm_resource_bundle_infoplist"],
829829
resources = ["Source/SwiftLibraryWithFilePathResource/Resources/chicken.json"],
830830
visibility = ["//:__subpackages__"],
831831
)
832832
833833
resource_bundle_accessor(
834834
name = "SwiftLibraryWithFilePathResource.rspm_resource_bundle_accessor",
835-
bundle_name = "SwiftLibraryWithFilePathResource_SwiftLibraryWithFilePathResource",
835+
bundle_name = "MyPackage_SwiftLibraryWithFilePathResource",
836836
)
837837
838838
resource_bundle_infoplist(
@@ -865,7 +865,7 @@ load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "generate_modulema
865865
866866
apple_resource_bundle(
867867
name = "ObjcLibraryWithResources.rspm_resource_bundle",
868-
bundle_name = "ObjcLibraryWithResources_ObjcLibraryWithResources",
868+
bundle_name = "MyPackage_ObjcLibraryWithResources",
869869
infoplists = [":ObjcLibraryWithResources.rspm_resource_bundle_infoplist"],
870870
resources = ["Source/ObjcLibraryWithResources/Resources/chicken.json"],
871871
visibility = ["//:__subpackages__"],
@@ -929,7 +929,7 @@ objc_resource_bundle_accessor_hdr(
929929
930930
objc_resource_bundle_accessor_impl(
931931
name = "ObjcLibraryWithResources.rspm_objc_resource_bundle_accessor_impl",
932-
bundle_name = "ObjcLibraryWithResources_ObjcLibraryWithResources",
932+
bundle_name = "MyPackage_ObjcLibraryWithResources",
933933
module_name = "ObjcLibraryWithResources",
934934
)
935935

0 commit comments

Comments
 (0)