diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index a26713e66..9039df05d 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -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: @@ -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) @@ -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): diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 933e562ee..c86e883ca 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -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 @@ -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: @@ -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) @@ -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, )) @@ -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, diff --git a/swiftpkg/tests/pkginfos_tests.bzl b/swiftpkg/tests/pkginfos_tests.bzl index 15b2c41b8..268999332 100644 --- a/swiftpkg/tests/pkginfos_tests.bzl +++ b/swiftpkg/tests/pkginfos_tests.bzl @@ -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(), ), @@ -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" ], diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 74e2e6cc3..b18554c08 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -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(), ), @@ -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"], @@ -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), diff --git a/swiftpkg/tests/testutils.bzl b/swiftpkg/tests/testutils.bzl index 5826379c4..5f555c218 100644 --- a/swiftpkg/tests/testutils.bzl +++ b/swiftpkg/tests/testutils.bzl @@ -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(