Skip to content

Commit

Permalink
fix: Update bundle name generator to match SPM (#1183)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jflan-dd committed Jul 29, 2024
1 parent 3e16d4d commit c27daf6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 10 deletions.
38 changes: 37 additions & 1 deletion examples/resources_example/Tests/MyAppTests/MyAppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ let package = Package(
name: "CoolUI",
resources: [.process("Resources")]
),
.testTarget(
name: "CoolUITests",
dependencies: ["CoolUI"]
)
]
)
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 4 additions & 3 deletions swiftpkg/internal/pkginfo_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
`<module_name>_<module_name>`.
`<package_name>_<module_name>`.
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.
Expand Down
5 changes: 3 additions & 2 deletions swiftpkg/internal/swiftpkg_build_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
8 changes: 4 additions & 4 deletions swiftpkg/tests/swiftpkg_build_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,15 @@ 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__"],
)
resource_bundle_accessor(
name = "SwiftLibraryWithFilePathResource.rspm_resource_bundle_accessor",
bundle_name = "SwiftLibraryWithFilePathResource_SwiftLibraryWithFilePathResource",
bundle_name = "MyPackage_SwiftLibraryWithFilePathResource",
)
resource_bundle_infoplist(
Expand Down Expand Up @@ -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__"],
Expand Down Expand Up @@ -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",
)
Expand Down

0 comments on commit c27daf6

Please sign in to comment.