diff --git a/examples/resources_example/Tests/MyAppTests/MyAppTests.swift b/examples/resources_example/Tests/MyAppTests/MyAppTests.swift index 0094769cf..e5b8af9ee 100644 --- a/examples/resources_example/Tests/MyAppTests/MyAppTests.swift +++ b/examples/resources_example/Tests/MyAppTests/MyAppTests.swift @@ -6,11 +6,17 @@ import SwiftUI import XCTest class MyAppTests: XCTestCase { - func test_CoolStuf_title_doesNotFail() throws { + func test_CoolStuff_title_doesNotFail() throws { let actual = CoolStuff.title() XCTAssertNotNil(actual) } + + func test_CoolStuff_bundleName() { + let bundle = Bundle.bundle(named: "package-with-resources_CoolUI") + XCTAssertNotNil(bundle) + } + func test_AppLovinSDKResources() throws { let url = ALResourceManager.resourceBundleURL XCTAssertNotNil(url) @@ -46,3 +52,33 @@ class MyAppTests: XCTestCase { IterableLogUtil.sharedInstance = nil } } + +private class BundleFinder {} + +extension Foundation.Bundle { + static func bundle(named bundleName: String) -> Bundle? { + 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, + + // Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). + Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(), + Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(), + ] + + for candidate in candidates { + let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") + if let bundle = bundlePath.flatMap(Bundle.init(url:)) { + return bundle + } + } + + return nil + } +} diff --git a/examples/resources_example/third_party/package_with_resources/Package.swift b/examples/resources_example/third_party/package_with_resources/Package.swift index 3129e9d40..0899d51ac 100644 --- a/examples/resources_example/third_party/package_with_resources/Package.swift +++ b/examples/resources_example/third_party/package_with_resources/Package.swift @@ -15,5 +15,9 @@ let package = Package( name: "CoolUI", resources: [.process("Resources")] ), + .testTarget( + name: "CoolUITests", + dependencies: ["CoolUI"] + ) ] ) diff --git a/examples/resources_example/third_party/package_with_resources/Tests/CoolUITests/BundleNameTest.swift b/examples/resources_example/third_party/package_with_resources/Tests/CoolUITests/BundleNameTest.swift new file mode 100644 index 000000000..acc4ede68 --- /dev/null +++ b/examples/resources_example/third_party/package_with_resources/Tests/CoolUITests/BundleNameTest.swift @@ -0,0 +1,41 @@ +import Foundation +import XCTest + +@testable import CoolUI + +class BundleNameTest: XCTestCase { + func testBundleName() { + let bundle = Bundle.bundle(named: "package-with-resources_CoolUI") + XCTAssertNotNil(bundle) + } +} + +private class BundleFinder {} + +extension Foundation.Bundle { + static func bundle(named bundleName: String) -> Bundle? { + 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, + + // Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). + Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(), + Bundle(for: BundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(), + ] + + for candidate in candidates { + let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") + if let bundle = bundlePath.flatMap(Bundle.init(url:)) { + return bundle + } + } + + return nil + } +} diff --git a/swiftpkg/internal/pkginfo_targets.bzl b/swiftpkg/internal/pkginfo_targets.bzl index e11a596bd..40faa9f7b 100644 --- a/swiftpkg/internal/pkginfo_targets.bzl +++ b/swiftpkg/internal/pkginfo_targets.bzl @@ -148,19 +148,20 @@ def _swift_hint_label_name(target_name): """ return target_name + _swift_hint_suffix -def _resource_bundle_name(module_name): +def _resource_bundle_name(package_name, module_name): """Returns the `bundle_name` for the module. For Swift packages, it appears the bundle name is of the format - `_`. + `_`. Args: + package_name: The package name. module_name: The module name. Returns: The `bundle_name` of the `apple_resource_bundle` as a `string`. """ - return module_name + "_" + module_name + return package_name + "_" + module_name def _resource_bundle_label_name(target_name): """Returns the name of the related `apple_resource_bundle` target. diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 09752549a..4cf72d0e6 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -543,15 +543,16 @@ def _handle_target_resources( return _apple_resource_bundle( target, + pkg_ctx.pkg_info.name, pkg_ctx.pkg_info.default_localization, include_swift_accessor = include_swift_accessor, include_objc_accessor = include_objc_accessor, ) -def _apple_resource_bundle(target, default_localization, include_swift_accessor, include_objc_accessor): +def _apple_resource_bundle(target, package_name, default_localization, include_swift_accessor, include_objc_accessor): bzl_target_name = pkginfo_targets.bazel_label_name(target) bundle_label_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name) - bundle_name = pkginfo_targets.resource_bundle_name(target.c99name) + bundle_name = pkginfo_targets.resource_bundle_name(package_name, target.c99name) infoplist_name = pkginfo_targets.resource_bundle_infoplist_label_name( bzl_target_name, ) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 5c1d0c85d..c10ea73d4 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -824,7 +824,7 @@ load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "resource_bundle_a apple_resource_bundle( name = "SwiftLibraryWithFilePathResource.rspm_resource_bundle", - bundle_name = "SwiftLibraryWithFilePathResource_SwiftLibraryWithFilePathResource", + bundle_name = "MyPackage_SwiftLibraryWithFilePathResource", infoplists = [":SwiftLibraryWithFilePathResource.rspm_resource_bundle_infoplist"], resources = ["Source/SwiftLibraryWithFilePathResource/Resources/chicken.json"], visibility = ["//:__subpackages__"], @@ -832,7 +832,7 @@ apple_resource_bundle( resource_bundle_accessor( name = "SwiftLibraryWithFilePathResource.rspm_resource_bundle_accessor", - bundle_name = "SwiftLibraryWithFilePathResource_SwiftLibraryWithFilePathResource", + bundle_name = "MyPackage_SwiftLibraryWithFilePathResource", ) resource_bundle_infoplist( @@ -865,7 +865,7 @@ load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "generate_modulema apple_resource_bundle( name = "ObjcLibraryWithResources.rspm_resource_bundle", - bundle_name = "ObjcLibraryWithResources_ObjcLibraryWithResources", + bundle_name = "MyPackage_ObjcLibraryWithResources", infoplists = [":ObjcLibraryWithResources.rspm_resource_bundle_infoplist"], resources = ["Source/ObjcLibraryWithResources/Resources/chicken.json"], visibility = ["//:__subpackages__"], @@ -929,7 +929,7 @@ objc_resource_bundle_accessor_hdr( objc_resource_bundle_accessor_impl( name = "ObjcLibraryWithResources.rspm_objc_resource_bundle_accessor_impl", - bundle_name = "ObjcLibraryWithResources_ObjcLibraryWithResources", + bundle_name = "MyPackage_ObjcLibraryWithResources", module_name = "ObjcLibraryWithResources", )