Skip to content

Commit

Permalink
Allow library tool resource handling to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
luispadron committed Nov 19, 2024
1 parent 57d46ed commit cc69e9b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 17 deletions.
41 changes: 24 additions & 17 deletions rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions tests/ios/frameworks/bundle-in-data-resources/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
13 changes: 13 additions & 0 deletions tests/ios/frameworks/bundle-in-data-resources/BundleInData.swift
Original file line number Diff line number Diff line change
@@ -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)!
}()
}

Original file line number Diff line number Diff line change
@@ -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"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fake-data

0 comments on commit cc69e9b

Please sign in to comment.