From cc69e9b5f3a8980aa81c7814005367d60ebf2203 Mon Sep 17 00:00:00 2001 From: Luis Padron Date: Mon, 18 Nov 2024 23:14:18 -0500 Subject: [PATCH] Allow library tool resource handling to be disabled --- rules/library.bzl | 41 +++++++++++-------- .../bundle-in-data-resources/BUILD.bazel | 29 +++++++++++++ .../BundleInData.swift | 13 ++++++ .../BundleInDataTests.swift | 17 ++++++++ .../bundle-in-data-resources/fake-data.txt | 1 + 5 files changed, 84 insertions(+), 17 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..65a65ac6 100644 --- a/rules/library.bzl +++ b/rules/library.bzl @@ -980,19 +980,26 @@ 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, - ) - resource_bundles = library_tools["resource_bundle_generator"]( - name = name, - library_tools = library_tools, - resource_bundles = kwargs.pop("resource_bundles", {}), - platforms = platforms, - ) - deps += resource_bundles + # 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 files. + if library_tools["resource_bundle_generator"]: + resource_bundles = library_tools["resource_bundle_generator"]( + name = name, + library_tools = library_tools, + resource_bundles = kwargs.pop("resource_bundles", {}), + platforms = platforms, + ) + deps += resource_bundles if has_swift_sources: # Forward the kwargs and the swift specific kwargs to the swift_library @@ -1018,7 +1025,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 +1119,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 +1129,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 +1147,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