Skip to content

Commit

Permalink
Allow library tool resource handling to be disabled (#928)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
luispadron authored Nov 19, 2024
1 parent 57d46ed commit c148e6a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
26 changes: 16 additions & 10 deletions rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
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 c148e6a

Please sign in to comment.