From c148e6a9cb5d346cf6cdd7fb705d68cfca7da1e9 Mon Sep 17 00:00:00 2001 From: Luis Padron Date: Tue, 19 Nov 2024 18:21:58 -0500 Subject: [PATCH] Allow library tool resource handling to be disabled (#928) This allows the wrap resource in filegroups and resource bundle generators to be disabled completely. It allows users to just pass in `data` and have the rule just forward that to the underlying libraries. This cleans up the code a bit too since we use `data` directly instead of `module_data` which had to be converted into a list in a few places. I added a test showcasing where this ability would be required. It fails without this change and passes when disabling the library tools --- rules/library.bzl | 26 ++++++++++------- .../bundle-in-data-resources/BUILD.bazel | 29 +++++++++++++++++++ .../BundleInData.swift | 13 +++++++++ .../BundleInDataTests.swift | 17 +++++++++++ .../bundle-in-data-resources/fake-data.txt | 1 + 5 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 tests/ios/frameworks/bundle-in-data-resources/BUILD.bazel create mode 100644 tests/ios/frameworks/bundle-in-data-resources/BundleInData.swift create mode 100644 tests/ios/frameworks/bundle-in-data-resources/BundleInDataTests.swift create mode 100644 tests/ios/frameworks/bundle-in-data-resources/fake-data.txt diff --git a/rules/library.bzl b/rules/library.bzl index d37e587f..2bf39f6f 100644 --- a/rules/library.bzl +++ b/rules/library.bzl @@ -980,12 +980,18 @@ def apple_library( #enable_framework_vfs = enable_framework_vfs ) - # Generate resource bundles - module_data = library_tools["wrap_resources_in_filegroup"]( - name = name + "_wrapped_resources_filegroup", - srcs = data, - testonly = testonly, - ) + # Wrap resources in a filegroup if requested. + if library_tools["wrap_resources_in_filegroup"]: + # Override the data as all the resources are now wrapped in a single filegroup target + data = [ + library_tools["wrap_resources_in_filegroup"]( + name = name + "_wrapped_resources_filegroup", + srcs = data, + testonly = testonly, + ), + ] + + # Generate resource bundles for any requested `resource_bundles`. resource_bundles = library_tools["resource_bundle_generator"]( name = name, library_tools = library_tools, @@ -1018,7 +1024,7 @@ def apple_library( "@build_bazel_rules_ios//:virtualize_frameworks": ["swift.vfsoverlay"], "//conditions:default": [], }), - data = [module_data], + data = data, tags = tags_manual, defines = defines + swift_defines, testonly = testonly, @@ -1112,7 +1118,7 @@ def apple_library( weak_sdk_frameworks = weak_sdk_frameworks, sdk_includes = sdk_includes, pch = pch, - data = [] if has_swift_sources else [module_data], + data = [] if has_swift_sources else data, tags = tags_manual, defines = defines + objc_defines, testonly = testonly, @@ -1122,7 +1128,7 @@ def apple_library( launch_screen_storyboard_name = name + "_launch_screen_storyboard" native.filegroup( name = launch_screen_storyboard_name, - srcs = [module_data], + srcs = data, output_group = "launch_screen_storyboard", tags = _MANUAL, testonly = testonly, @@ -1140,7 +1146,7 @@ def apple_library( transitive_deps = deps, deps = lib_names + deps, module_name = module_name, - data = module_data, + data = data, launch_screen_storyboard_name = launch_screen_storyboard_name, namespace = namespace, linkopts = copts_by_build_setting.linkopts + linkopts, diff --git a/tests/ios/frameworks/bundle-in-data-resources/BUILD.bazel b/tests/ios/frameworks/bundle-in-data-resources/BUILD.bazel new file mode 100644 index 00000000..12c00627 --- /dev/null +++ b/tests/ios/frameworks/bundle-in-data-resources/BUILD.bazel @@ -0,0 +1,29 @@ +load("//rules:framework.bzl", "apple_framework") +load("//rules:precompiled_apple_resource_bundle.bzl", "precompiled_apple_resource_bundle") +load("//rules:test.bzl", "ios_unit_test") + +precompiled_apple_resource_bundle( + name = "BundleInDataResources", + bundle_id = "com.example.BundleInDataResources", + bundle_name = "BundleInDataResources", + platforms = {"ios": "12.0"}, + resources = ["fake-data.txt"], +) + +apple_framework( + name = "BundleInDataResourcesFramework", + srcs = ["BundleInData.swift"], + data = [":BundleInDataResources"], + # Because the `library.bzl` wraps `data` in a filegroup by default, the `rules_apple` aspects + # seem to fail to find the resource bundle. It seems in order to directly use a resource bundle rule + # within `data`, it must be directly referenced vs. wrapped in a filegroup. + library_tools = {"wrap_resources_in_filegroup": None}, + platforms = {"ios": "12.0"}, +) + +ios_unit_test( + name = "BundleInDataTests", + srcs = ["BundleInDataTests.swift"], + minimum_os_version = "12.0", + deps = [":BundleInDataResourcesFramework"], +) diff --git a/tests/ios/frameworks/bundle-in-data-resources/BundleInData.swift b/tests/ios/frameworks/bundle-in-data-resources/BundleInData.swift new file mode 100644 index 00000000..de7d643b --- /dev/null +++ b/tests/ios/frameworks/bundle-in-data-resources/BundleInData.swift @@ -0,0 +1,13 @@ +import Foundation + +private class BundleInDataBundleFinder {} + +// Helper for finding the bundle named "BundleInDataResources" +public extension Bundle { + static let bundleInDataResources: Bundle = { + let container = Bundle(for: BundleInDataBundleFinder.self) + let bundlePath = container.path(forResource: "BundleInDataResources", ofType: "bundle")! + return Bundle(path: bundlePath)! + }() +} + diff --git a/tests/ios/frameworks/bundle-in-data-resources/BundleInDataTests.swift b/tests/ios/frameworks/bundle-in-data-resources/BundleInDataTests.swift new file mode 100644 index 00000000..dc19192c --- /dev/null +++ b/tests/ios/frameworks/bundle-in-data-resources/BundleInDataTests.swift @@ -0,0 +1,17 @@ +import Foundation +import XCTest + +@testable import BundleInDataResourcesFramework + +class BundleInDataTests: XCTestCase { + + func testBundleInData() throws { + let fakeDataPath = try XCTUnwrap(Bundle.bundleInDataResources.path(forResource: "fake-data", ofType: "txt")) + let fakeDataContents = try XCTUnwrap(String(contentsOfFile: fakeDataPath)) + + XCTAssertEqual( + fakeDataContents, + "fake-data\n" + ) + } +} diff --git a/tests/ios/frameworks/bundle-in-data-resources/fake-data.txt b/tests/ios/frameworks/bundle-in-data-resources/fake-data.txt new file mode 100644 index 00000000..a9671257 --- /dev/null +++ b/tests/ios/frameworks/bundle-in-data-resources/fake-data.txt @@ -0,0 +1 @@ +fake-data