Skip to content
20 changes: 10 additions & 10 deletions swiftpkg/internal/pkginfos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,14 @@ def _new_target_from_json_maps(
linker_settings = _new_linker_settings_from_dump_json_list(
dump_map["settings"],
)
resources_map = dump_map.get("resources", [])
if len(resources_map) == 0:
resources_map = desc_map.get("resources", [])

# The description JSON should have a list with all of the resources with
# their absolute paths.
resources = [
_new_resource_from_dump_json_map(d, pkg_path)
for d in resources_map
_new_resource_from_desc_map(r, pkg_path)
for r in desc_map.get("resources", [])
]

artifact_download_info = None
url = dump_map.get("url")
if url != None:
Expand Down Expand Up @@ -781,9 +782,8 @@ def _new_swift_src_info_from_sources(repository_ctx, target_path, sources):

# The paths should be relative to the target not the root of the workspace.
# Do not include directories in the output.
tp_prefix = target_path + "/"
discovered_res_files = [
f.removeprefix(tp_prefix)
f
for f in all_target_files
if not repository_files.is_directory(repository_ctx, f) and
resource_files.is_auto_discovered_resource(f)
Expand Down Expand Up @@ -1285,13 +1285,13 @@ def _new_resource_rule_process(localization = None):
localization = localization,
)

def _new_resource_from_dump_json_map(dump_map, pkg_path):
path = dump_map["path"]
def _new_resource_from_desc_map(desc_map, pkg_path):
path = desc_map["path"]
if paths.is_absolute(path):
path = paths.relativize(path, pkg_path)
return _new_resource(
path = path,
rule = _new_resource_rule_from_dump_json_map(dump_map["rule"]),
rule = _new_resource_rule_from_dump_json_map(desc_map["rule"]),
)

def _new_resource_rule_from_dump_json_map(dump_map):
Expand Down
23 changes: 7 additions & 16 deletions swiftpkg/internal/swiftpkg_build_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ load(":load_statements.bzl", "load_statements")
load(":pkginfo_target_deps.bzl", "pkginfo_target_deps")
load(":pkginfo_targets.bzl", "pkginfo_targets")
load(":pkginfos.bzl", "build_setting_kinds", "module_types", "pkginfos", "target_types")
load(":repository_files.bzl", "repository_files")
load(":starlark_codegen.bzl", scg = "starlark_codegen")

# MARK: - Target Entry Point
Expand All @@ -19,7 +18,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target):
if target.module_type == module_types.clang:
return _clang_target_build_file(repository_ctx, pkg_ctx, target)
elif target.module_type == module_types.swift:
return _swift_target_build_file(repository_ctx, pkg_ctx, target)
return _swift_target_build_file(pkg_ctx, target)
elif target.module_type == module_types.system_library:
return _system_library_build_file(target)
elif target.module_type == module_types.binary:
Expand All @@ -31,7 +30,7 @@ def _new_for_target(repository_ctx, pkg_ctx, target):

# MARK: - Swift Target

def _swift_target_build_file(repository_ctx, pkg_ctx, target):
def _swift_target_build_file(pkg_ctx, target):
if target.swift_src_info == None:
fail("Expected a `swift_src_info`. name: ", target.name)

Expand Down Expand Up @@ -87,7 +86,6 @@ def _swift_target_build_file(repository_ctx, pkg_ctx, target):
# Apparently, SPM provides a `Bundle.module` accessor. So, we do too.
# https://stackoverflow.com/questions/63237395/generating-resource-bundle-accessor-type-bundle-has-no-member-module
all_build_files.append(_apple_resource_bundle(
repository_ctx,
target,
pkg_ctx.pkg_info.default_localization,
))
Expand Down Expand Up @@ -383,24 +381,17 @@ def _apple_static_xcframework_import_build_file(target):

# MARK: - Apple Resource Group

def _apple_resource_bundle(repository_ctx, target, default_localization):
def _apple_resource_bundle(target, default_localization):
bzl_target_name = pkginfo_targets.bazel_label_name(target)
bundle_name = pkginfo_targets.resource_bundle_label_name(bzl_target_name)
infoplist_name = pkginfo_targets.resource_bundle_infoplist_label_name(
bzl_target_name,
)

# GH575: Refactor the resources into glob patterns and file paths. Add
# file paths using a list.

glob_paths = []
for r in target.resources:
path = pkginfo_targets.join_path(target, r.path)
if repository_files.is_directory(repository_ctx, path):
glob_paths.append("{}/**".format(path))
else:
glob_paths.append(path)
resources = scg.new_fn_call("glob", glob_paths)
resources = [
r.path
for r in target.resources
]

load_stmts = [
apple_resource_bundle_load_stmt,
Expand Down
8 changes: 7 additions & 1 deletion swiftpkg/tests/pkginfos_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _new_from_parsed_json_for_swift_targets_test(ctx):
product_memberships = ["printstuff"],
resources = [
pkginfos.new_resource(
path = "Resources/chicken.json",
path = "Sources/MySwiftPackage/Resources/chicken.json",
rule = pkginfos.new_resource_rule(
process = pkginfos.new_resource_rule_process(),
),
Expand Down Expand Up @@ -539,6 +539,12 @@ _swift_arg_parser_desc_json = """
"product_memberships" : [
"printstuff"
],
"resources": [
{
"path" : "/Users/chuck/code/cgrindel/rules_swift_package_manager/gh009_update_repos_new/examples/pkg_manifest/Sources/MySwiftPackage/Resources/chicken.json",
"rule" : { "process" : {} }
}
],
"sources" : [
"MySwiftPackage.swift"
],
Expand Down
42 changes: 23 additions & 19 deletions swiftpkg/tests/swiftpkg_build_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,17 @@ _pkg_info = pkginfos.new(
swift_src_info = pkginfos.new_swift_src_info(has_objc_directive = True),
),
pkginfos.new_target(
name = "SwiftLibraryWithResources",
name = "SwiftLibraryWithFilePathResource",
type = "regular",
c99name = "SwiftLibraryWithResources",
c99name = "SwiftLibraryWithFilePathResource",
module_type = "SwiftTarget",
path = "Source/SwiftLibraryWithResources",
path = "Source/SwiftLibraryWithFilePathResource",
sources = [
"SwiftLibraryWithResources.swift",
"SwiftLibraryWithFilePathResource.swift",
],
resources = [
pkginfos.new_resource(
path = "Resources/chicken.json",
path = "Source/SwiftLibraryWithFilePathResource/Resources/chicken.json",
rule = pkginfos.new_resource_rule(
process = pkginfos.new_resource_rule_process(),
),
Expand Down Expand Up @@ -759,39 +759,39 @@ swift_library(
""",
),
struct(
msg = "Swift library target with resources",
name = "SwiftLibraryWithResources",
msg = "Swift library target with file path resource",
name = "SwiftLibraryWithFilePathResource",
exp = """\
load("@build_bazel_rules_apple//apple:resources.bzl", "apple_resource_bundle")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
load("@rules_swift_package_manager//swiftpkg:build_defs.bzl", "resource_bundle_accessor", "resource_bundle_infoplist")

apple_resource_bundle(
name = "Source_SwiftLibraryWithResources_resource_bundle",
bundle_name = "Source_SwiftLibraryWithResources_resource_bundle",
infoplists = [":Source_SwiftLibraryWithResources_resource_bundle_infoplist"],
resources = glob(["Source/SwiftLibraryWithResources/Resources/chicken.json"]),
name = "Source_SwiftLibraryWithFilePathResource_resource_bundle",
bundle_name = "Source_SwiftLibraryWithFilePathResource_resource_bundle",
infoplists = [":Source_SwiftLibraryWithFilePathResource_resource_bundle_infoplist"],
resources = ["Source/SwiftLibraryWithFilePathResource/Resources/chicken.json"],
)

resource_bundle_accessor(
name = "Source_SwiftLibraryWithResources_resource_bundle_accessor",
bundle_name = "Source_SwiftLibraryWithResources_resource_bundle",
name = "Source_SwiftLibraryWithFilePathResource_resource_bundle_accessor",
bundle_name = "Source_SwiftLibraryWithFilePathResource_resource_bundle",
)

resource_bundle_infoplist(
name = "Source_SwiftLibraryWithResources_resource_bundle_infoplist",
name = "Source_SwiftLibraryWithFilePathResource_resource_bundle_infoplist",
region = "en",
)

swift_library(
name = "Source_SwiftLibraryWithResources",
data = [":Source_SwiftLibraryWithResources_resource_bundle"],
name = "Source_SwiftLibraryWithFilePathResource",
data = [":Source_SwiftLibraryWithFilePathResource_resource_bundle"],
defines = ["SWIFT_PACKAGE"],
deps = [],
module_name = "SwiftLibraryWithResources",
module_name = "SwiftLibraryWithFilePathResource",
srcs = [
"Source/SwiftLibraryWithResources/SwiftLibraryWithResources.swift",
":Source_SwiftLibraryWithResources_resource_bundle_accessor",
"Source/SwiftLibraryWithFilePathResource/SwiftLibraryWithFilePathResource.swift",
":Source_SwiftLibraryWithFilePathResource_resource_bundle_accessor",
],
tags = ["manual"],
visibility = ["//visibility:public"],
Expand All @@ -814,6 +814,10 @@ swift_library(
]
for dirname, file_paths in getattr(t, "find_results", {}).items()
},
is_directory_results = {
paths.normalize(paths.join(target.path, path)): result
for path, result in getattr(t, "is_directory", {}).items()
},
)
actual = scg.to_starlark(
swiftpkg_build_files.new_for_target(repository_ctx, _pkg_ctx, target),
Expand Down
21 changes: 17 additions & 4 deletions swiftpkg/tests/testutils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@ def _new_exec_result(return_code = 0, stdout = "", stderr = ""):
stderr = stderr,
)

def _new_stub_repository_ctx(repo_name, file_contents = {}, find_results = {}):
def _new_stub_repository_ctx(
repo_name,
file_contents = {},
find_results = {},
is_directory_results = {}):
def read(path):
return file_contents.get(path, "")

# buildifier: disable=unused-variable
def execute(args, environment = {}, quiet = True):
# The find command that we expect is `find -H -L path`.
# See repository_files.list_files_under for details.
if len(args) >= 4 and args[0] == "find":
args_len = len(args)
if args_len == 3 and args[2].startswith("if [[ -d ") and environment["TARGET_PATH"]:
# Look for the is_directory check.
path = environment["TARGET_PATH"]
result = is_directory_results.get(path, False)
stdout = "TRUE" if result else "FALSE"
stdout += "\n"
exec_result = _new_exec_result(stdout = stdout)

elif args_len >= 4 and args[0] == "find":
# The find command that we expect is `find -H -L path`.
# See repository_files.list_files_under for details.
path = args[3]
results = find_results.get(path, [])
exec_result = _new_exec_result(
Expand Down