From 7327430e60e26bb1e1974f1b62b79dc8fda730a8 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Sep 2023 16:21:12 -0600 Subject: [PATCH 01/12] Separate the globs from the file paths --- swiftpkg/internal/swiftpkg_build_files.bzl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 933e562ee..79fb47ddc 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -394,13 +394,14 @@ def _apple_resource_bundle(repository_ctx, target, default_localization): # file paths using a list. glob_paths = [] + file_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) + file_paths.append(path) + resources = scg.new_fn_call("glob", glob_paths + file_paths) load_stmts = [ apple_resource_bundle_load_stmt, From 538c38e81618aaa1cac0263be70a516f04c4392b Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Sep 2023 16:25:02 -0600 Subject: [PATCH 02/12] Rename existing Resource test --- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 74e2e6cc3..6a901f778 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -360,13 +360,13 @@ _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( @@ -760,38 +760,38 @@ swift_library( ), struct( msg = "Swift library target with resources", - name = "SwiftLibraryWithResources", + 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 = glob(["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"], From 66f37b0448af3a2ad4fadbb79d3bd421b438c073 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Sep 2023 16:52:31 -0600 Subject: [PATCH 03/12] Update testutils to support is_directory --- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 34 ++++++++++++++++++- swiftpkg/tests/testutils.bzl | 21 +++++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 6a901f778..b5a7048e5 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -380,6 +380,27 @@ _pkg_info = pkginfos.new( repo_name = _repo_name, swift_src_info = pkginfos.new_swift_src_info(), ), + pkginfos.new_target( + name = "SwiftLibraryWithDirResource", + type = "regular", + c99name = "SwiftLibraryWithDirResource", + module_type = "SwiftTarget", + path = "Source/SwiftLibraryWithDirResource", + sources = [ + "SwiftLibraryWithDirResource.swift", + ], + resources = [ + pkginfos.new_resource( + path = "Resources/Assets.xcassets", + rule = pkginfos.new_resource_rule( + process = pkginfos.new_resource_rule_process(), + ), + ), + ], + dependencies = [], + repo_name = _repo_name, + swift_src_info = pkginfos.new_swift_src_info(), + ), ], ) @@ -759,7 +780,7 @@ swift_library( """, ), struct( - msg = "Swift library target with resources", + msg = "Swift library target with file path resource", name = "SwiftLibraryWithFilePathResource", exp = """\ load("@build_bazel_rules_apple//apple:resources.bzl", "apple_resource_bundle") @@ -796,6 +817,13 @@ swift_library( tags = ["manual"], visibility = ["//visibility:public"], ) +""", + ), + struct( + msg = "Swift library target with directory resource", + name = "SwiftLibraryWithDirResource", + is_directory = {"Resources/Assets.xcassets": True}, + exp = """\ """, ), ] @@ -814,6 +842,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..f93272188 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": + argsLen = len(args) + if argsLen == 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 argsLen >= 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( From cbe422d1b678ba2df6dfff19c1f56bbb74331ddb Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Sep 2023 16:54:05 -0600 Subject: [PATCH 04/12] Add test for directory resource --- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 33 +++++++++++++++++++ swiftpkg/tests/testutils.bzl | 6 ++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index b5a7048e5..5a1ca87e4 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -824,6 +824,39 @@ swift_library( name = "SwiftLibraryWithDirResource", is_directory = {"Resources/Assets.xcassets": True}, 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_SwiftLibraryWithDirResource_resource_bundle", + bundle_name = "Source_SwiftLibraryWithDirResource_resource_bundle", + infoplists = [":Source_SwiftLibraryWithDirResource_resource_bundle_infoplist"], + resources = glob(["Source/SwiftLibraryWithDirResource/Resources/Assets.xcassets/**"]), +) + +resource_bundle_accessor( + name = "Source_SwiftLibraryWithDirResource_resource_bundle_accessor", + bundle_name = "Source_SwiftLibraryWithDirResource_resource_bundle", +) + +resource_bundle_infoplist( + name = "Source_SwiftLibraryWithDirResource_resource_bundle_infoplist", + region = "en", +) + +swift_library( + name = "Source_SwiftLibraryWithDirResource", + data = [":Source_SwiftLibraryWithDirResource_resource_bundle"], + defines = ["SWIFT_PACKAGE"], + deps = [], + module_name = "SwiftLibraryWithDirResource", + srcs = [ + "Source/SwiftLibraryWithDirResource/SwiftLibraryWithDirResource.swift", + ":Source_SwiftLibraryWithDirResource_resource_bundle_accessor", + ], + visibility = ["//visibility:public"], +) """, ), ] diff --git a/swiftpkg/tests/testutils.bzl b/swiftpkg/tests/testutils.bzl index f93272188..5f555c218 100644 --- a/swiftpkg/tests/testutils.bzl +++ b/swiftpkg/tests/testutils.bzl @@ -17,8 +17,8 @@ def _new_stub_repository_ctx( # buildifier: disable=unused-variable def execute(args, environment = {}, quiet = True): - argsLen = len(args) - if argsLen == 3 and args[2].startswith("if [[ -d ") and environment["TARGET_PATH"]: + 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) @@ -26,7 +26,7 @@ def _new_stub_repository_ctx( stdout += "\n" exec_result = _new_exec_result(stdout = stdout) - elif argsLen >= 4 and args[0] == "find": + 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] From 0b403ca6f9ebbe50f6cc075bdd7f9147df08786e Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Sep 2023 17:04:48 -0600 Subject: [PATCH 05/12] Separate file and globs --- swiftpkg/internal/swiftpkg_build_files.bzl | 21 ++++++++++++++++++- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 79fb47ddc..ee80fc44d 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -401,7 +401,26 @@ def _apple_resource_bundle(repository_ctx, target, default_localization): glob_paths.append("{}/**".format(path)) else: file_paths.append(path) - resources = scg.new_fn_call("glob", glob_paths + file_paths) + + resource_parts = [] + if len(glob_paths) > 0: + resource_parts.append(scg.new_fn_call("glob", glob_paths)) + if len(file_paths) > 0: + resource_parts.append(file_paths) + + rp_len = len(resource_parts) + if rp_len == 0: + fail("""\ +We were asked to create an apple_resource_bundle, but no resources were specified.\ +""") + elif rp_len == 1: + resources = resource_parts[0] + else: + resources = scg.new_expr( + resource_parts[0], + scg.new_op("+"), + resource_parts[1], + ) load_stmts = [ apple_resource_bundle_load_stmt, diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 5a1ca87e4..f4b661bea 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -791,7 +791,7 @@ apple_resource_bundle( name = "Source_SwiftLibraryWithFilePathResource_resource_bundle", bundle_name = "Source_SwiftLibraryWithFilePathResource_resource_bundle", infoplists = [":Source_SwiftLibraryWithFilePathResource_resource_bundle_infoplist"], - resources = glob(["Source/SwiftLibraryWithFilePathResource/Resources/chicken.json"]), + resources = ["Source/SwiftLibraryWithFilePathResource/Resources/chicken.json"], ) resource_bundle_accessor( From 7c41e4e612f3336e7e2d9c37f6ec1934a4c3bdc9 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Sep 2023 17:07:45 -0600 Subject: [PATCH 06/12] Add test with file and dir resource --- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index f4b661bea..fe634e094 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -401,6 +401,33 @@ _pkg_info = pkginfos.new( repo_name = _repo_name, swift_src_info = pkginfos.new_swift_src_info(), ), + pkginfos.new_target( + name = "SwiftLibraryWithFileAndDirResources", + type = "regular", + c99name = "SwiftLibraryWithFileAndDirResources", + module_type = "SwiftTarget", + path = "Source/SwiftLibraryWithFileAndDirResources", + sources = [ + "SwiftLibraryWithFileAndDirResources.swift", + ], + resources = [ + pkginfos.new_resource( + path = "Resources/chicken.json", + rule = pkginfos.new_resource_rule( + process = pkginfos.new_resource_rule_process(), + ), + ), + pkginfos.new_resource( + path = "Resources/Assets.xcassets", + rule = pkginfos.new_resource_rule( + process = pkginfos.new_resource_rule_process(), + ), + ), + ], + dependencies = [], + repo_name = _repo_name, + swift_src_info = pkginfos.new_swift_src_info(), + ), ], ) @@ -857,6 +884,46 @@ swift_library( ], visibility = ["//visibility:public"], ) +""", + ), + struct( + msg = "Swift library target with file and directory resources", + name = "SwiftLibraryWithFileAndDirResources", + is_directory = {"Resources/Assets.xcassets": True}, + 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_SwiftLibraryWithFileAndDirResources_resource_bundle", + bundle_name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle", + infoplists = [":Source_SwiftLibraryWithFileAndDirResources_resource_bundle_infoplist"], + resources = glob(["Source/SwiftLibraryWithFileAndDirResources/Resources/Assets.xcassets/**"]) + ["Source/SwiftLibraryWithFileAndDirResources/Resources/chicken.json"], +) + +resource_bundle_accessor( + name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle_accessor", + bundle_name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle", +) + +resource_bundle_infoplist( + name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle_infoplist", + region = "en", +) + +swift_library( + name = "Source_SwiftLibraryWithFileAndDirResources", + data = [":Source_SwiftLibraryWithFileAndDirResources_resource_bundle"], + defines = ["SWIFT_PACKAGE"], + deps = [], + module_name = "SwiftLibraryWithFileAndDirResources", + srcs = [ + "Source/SwiftLibraryWithFileAndDirResources/SwiftLibraryWithFileAndDirResources.swift", + ":Source_SwiftLibraryWithFileAndDirResources_resource_bundle_accessor", + ], + visibility = ["//visibility:public"], +) """, ), ] From b18b1ecb59883e6dd89e90bf09172ae6cfdf2316 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sat, 9 Sep 2023 10:02:15 -0600 Subject: [PATCH 07/12] Add missing manual tag to test --- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index fe634e094..e4a6266e4 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -922,6 +922,7 @@ swift_library( "Source/SwiftLibraryWithFileAndDirResources/SwiftLibraryWithFileAndDirResources.swift", ":Source_SwiftLibraryWithFileAndDirResources_resource_bundle_accessor", ], + tags = ["manual"], visibility = ["//visibility:public"], ) """, From 8caee03462c3ef5bf58f2812724c25ec4d1fc935 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sat, 9 Sep 2023 10:30:12 -0600 Subject: [PATCH 08/12] Fix other unit test. --- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index e4a6266e4..7acc483e1 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -882,6 +882,7 @@ swift_library( "Source/SwiftLibraryWithDirResource/SwiftLibraryWithDirResource.swift", ":Source_SwiftLibraryWithDirResource_resource_bundle_accessor", ], + tags = ["manual"], visibility = ["//visibility:public"], ) """, From 7f497d964c9794c1fb39c63164e2d09762b2c2aa Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sat, 9 Sep 2023 13:06:41 -0600 Subject: [PATCH 09/12] The stripe_example works, but resources_example does not. --- swiftpkg/internal/pkginfos.bzl | 58 ++++++++++++++++++---- swiftpkg/internal/swiftpkg_build_files.bzl | 13 +++-- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index a26713e66..2ff73f219 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -241,13 +241,37 @@ 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", []) ] + + # desc_resources = desc_map.get("resources", []) + # if desc_resources != []: + # resources = [ + # _new_resource_from_desc_map(repository_ctx, d, pkg_path) + # for d in desc_resources + # ] + # else: + # resources = [ + # _new_resource_from_desc_map(repository_ctx, d, pkg_path) + # for d in dump_map.get("resources", []) + # ] + + # resources_map = dump_map.get("resources", []) + # if len(resources_map) == 0: + # resources_map = desc_map.get("resources", []) + # resources_map = desc_map.get("resources", []) + # if len(resources_map) == 0: + # resources_map = dump_map.get("resources", []) + # resources = [ + # _new_resource_from_json_map(repository_ctx, d, pkg_path) + # for d in resources_map + # ] + artifact_download_info = None url = dump_map.get("url") if url != None: @@ -783,12 +807,20 @@ def _new_swift_src_info_from_sources(repository_ctx, target_path, sources): # Do not include directories in the output. tp_prefix = target_path + "/" discovered_res_files = [ - f.removeprefix(tp_prefix) + # 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) ] + # DEBUG BEGIN + print("*** CHUCK discovered_res_files: ") + for idx, item in enumerate(discovered_res_files): + print("*** CHUCK", idx, ":", item) + + # DEBUG END + return _new_swift_src_info( has_objc_directive = has_objc_directive, imports_xctest = imports_xctest, @@ -1285,13 +1317,21 @@ 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) + + # DEBUG BEGIN + print("*** CHUCK ---------") + print("*** CHUCK desc_map[\"path\"]: ", desc_map["path"]) + print("*** CHUCK pkg_path: ", pkg_path) + print("*** CHUCK path: ", path) + + # DEBUG END 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 ee80fc44d..3ba04f5b7 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -390,13 +390,18 @@ def _apple_resource_bundle(repository_ctx, target, default_localization): bzl_target_name, ) - # GH575: Refactor the resources into glob patterns and file paths. Add - # file paths using a list. - glob_paths = [] file_paths = [] for r in target.resources: - path = pkginfo_targets.join_path(target, r.path) + # path = pkginfo_targets.join_path(target, r.path) + path = r.path + + # DEBUG BEGIN + print("*** CHUCK --------") + print("*** CHUCK target.path: ", target.path) + print("*** CHUCK r.path: ", r.path) + + # DEBUG END if repository_files.is_directory(repository_ctx, path): glob_paths.append("{}/**".format(path)) else: From 4850cbbfa3fc73370c7eebe7e766a4ba5e30caaa Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sun, 10 Sep 2023 09:11:55 -0600 Subject: [PATCH 10/12] messagekit, resources, and stripe examples work --- swiftpkg/internal/pkginfos.bzl | 38 ------------ swiftpkg/internal/swiftpkg_build_files.bzl | 68 ++++++++++------------ 2 files changed, 32 insertions(+), 74 deletions(-) diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index 2ff73f219..30c565394 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -249,29 +249,6 @@ def _new_target_from_json_maps( for r in desc_map.get("resources", []) ] - # desc_resources = desc_map.get("resources", []) - # if desc_resources != []: - # resources = [ - # _new_resource_from_desc_map(repository_ctx, d, pkg_path) - # for d in desc_resources - # ] - # else: - # resources = [ - # _new_resource_from_desc_map(repository_ctx, d, pkg_path) - # for d in dump_map.get("resources", []) - # ] - - # resources_map = dump_map.get("resources", []) - # if len(resources_map) == 0: - # resources_map = desc_map.get("resources", []) - # resources_map = desc_map.get("resources", []) - # if len(resources_map) == 0: - # resources_map = dump_map.get("resources", []) - # resources = [ - # _new_resource_from_json_map(repository_ctx, d, pkg_path) - # for d in resources_map - # ] - artifact_download_info = None url = dump_map.get("url") if url != None: @@ -814,13 +791,6 @@ def _new_swift_src_info_from_sources(repository_ctx, target_path, sources): resource_files.is_auto_discovered_resource(f) ] - # DEBUG BEGIN - print("*** CHUCK discovered_res_files: ") - for idx, item in enumerate(discovered_res_files): - print("*** CHUCK", idx, ":", item) - - # DEBUG END - return _new_swift_src_info( has_objc_directive = has_objc_directive, imports_xctest = imports_xctest, @@ -1321,14 +1291,6 @@ 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) - - # DEBUG BEGIN - print("*** CHUCK ---------") - print("*** CHUCK desc_map[\"path\"]: ", desc_map["path"]) - print("*** CHUCK pkg_path: ", pkg_path) - print("*** CHUCK path: ", path) - - # DEBUG END return _new_resource( path = path, rule = _new_resource_rule_from_dump_json_map(desc_map["rule"]), diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 3ba04f5b7..8b9c18577 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 @@ -390,42 +389,39 @@ def _apple_resource_bundle(repository_ctx, target, default_localization): bzl_target_name, ) - glob_paths = [] - file_paths = [] - for r in target.resources: - # path = pkginfo_targets.join_path(target, r.path) - path = r.path - - # DEBUG BEGIN - print("*** CHUCK --------") - print("*** CHUCK target.path: ", target.path) - print("*** CHUCK r.path: ", r.path) + resources = [ + r.path + for r in target.resources + ] - # DEBUG END - if repository_files.is_directory(repository_ctx, path): - glob_paths.append("{}/**".format(path)) - else: - file_paths.append(path) - - resource_parts = [] - if len(glob_paths) > 0: - resource_parts.append(scg.new_fn_call("glob", glob_paths)) - if len(file_paths) > 0: - resource_parts.append(file_paths) - - rp_len = len(resource_parts) - if rp_len == 0: - fail("""\ -We were asked to create an apple_resource_bundle, but no resources were specified.\ -""") - elif rp_len == 1: - resources = resource_parts[0] - else: - resources = scg.new_expr( - resource_parts[0], - scg.new_op("+"), - resource_parts[1], - ) + # glob_paths = [] + # file_paths = [] + # for r in target.resources: + # path = r.path + # if repository_files.is_directory(repository_ctx, path): + # glob_paths.append("{}/**".format(path)) + # else: + # file_paths.append(path) + + # resource_parts = [] + # if len(glob_paths) > 0: + # resource_parts.append(scg.new_fn_call("glob", glob_paths)) + # if len(file_paths) > 0: + # resource_parts.append(file_paths) + + # rp_len = len(resource_parts) + # if rp_len == 0: + # fail("""\ + # We were asked to create an apple_resource_bundle, but no resources were specified.\ + # """) + # elif rp_len == 1: + # resources = resource_parts[0] + # else: + # resources = scg.new_expr( + # resource_parts[0], + # scg.new_op("+"), + # resource_parts[1], + # ) load_stmts = [ apple_resource_bundle_load_stmt, From 430da8fa9f973306989090ff37bec55a94c58f03 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sun, 10 Sep 2023 09:57:38 -0600 Subject: [PATCH 11/12] Unit tests are green --- swiftpkg/internal/pkginfos.bzl | 4 +- swiftpkg/internal/swiftpkg_build_files.bzl | 36 +---- swiftpkg/tests/pkginfos_tests.bzl | 8 +- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 132 +----------------- 4 files changed, 13 insertions(+), 167 deletions(-) diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index 30c565394..d28fc2662 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -780,11 +780,11 @@ def _new_swift_src_info_from_sources(repository_ctx, target_path, sources): target_path, ) + # TODO(chuck): Remove is_directory check. + # 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 diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 8b9c18577..c86e883ca 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -18,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: @@ -30,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) @@ -86,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, )) @@ -382,7 +381,7 @@ 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( @@ -394,35 +393,6 @@ def _apple_resource_bundle(repository_ctx, target, default_localization): for r in target.resources ] - # glob_paths = [] - # file_paths = [] - # for r in target.resources: - # path = r.path - # if repository_files.is_directory(repository_ctx, path): - # glob_paths.append("{}/**".format(path)) - # else: - # file_paths.append(path) - - # resource_parts = [] - # if len(glob_paths) > 0: - # resource_parts.append(scg.new_fn_call("glob", glob_paths)) - # if len(file_paths) > 0: - # resource_parts.append(file_paths) - - # rp_len = len(resource_parts) - # if rp_len == 0: - # fail("""\ - # We were asked to create an apple_resource_bundle, but no resources were specified.\ - # """) - # elif rp_len == 1: - # resources = resource_parts[0] - # else: - # resources = scg.new_expr( - # resource_parts[0], - # scg.new_op("+"), - # resource_parts[1], - # ) - load_stmts = [ apple_resource_bundle_load_stmt, swiftpkg_resource_bundle_infoplist_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 7acc483e1..b18554c08 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -370,55 +370,7 @@ _pkg_info = pkginfos.new( ], resources = [ pkginfos.new_resource( - path = "Resources/chicken.json", - rule = pkginfos.new_resource_rule( - process = pkginfos.new_resource_rule_process(), - ), - ), - ], - dependencies = [], - repo_name = _repo_name, - swift_src_info = pkginfos.new_swift_src_info(), - ), - pkginfos.new_target( - name = "SwiftLibraryWithDirResource", - type = "regular", - c99name = "SwiftLibraryWithDirResource", - module_type = "SwiftTarget", - path = "Source/SwiftLibraryWithDirResource", - sources = [ - "SwiftLibraryWithDirResource.swift", - ], - resources = [ - pkginfos.new_resource( - path = "Resources/Assets.xcassets", - rule = pkginfos.new_resource_rule( - process = pkginfos.new_resource_rule_process(), - ), - ), - ], - dependencies = [], - repo_name = _repo_name, - swift_src_info = pkginfos.new_swift_src_info(), - ), - pkginfos.new_target( - name = "SwiftLibraryWithFileAndDirResources", - type = "regular", - c99name = "SwiftLibraryWithFileAndDirResources", - module_type = "SwiftTarget", - path = "Source/SwiftLibraryWithFileAndDirResources", - sources = [ - "SwiftLibraryWithFileAndDirResources.swift", - ], - resources = [ - pkginfos.new_resource( - path = "Resources/chicken.json", - rule = pkginfos.new_resource_rule( - process = pkginfos.new_resource_rule_process(), - ), - ), - pkginfos.new_resource( - path = "Resources/Assets.xcassets", + path = "Source/SwiftLibraryWithFilePathResource/Resources/chicken.json", rule = pkginfos.new_resource_rule( process = pkginfos.new_resource_rule_process(), ), @@ -844,88 +796,6 @@ swift_library( tags = ["manual"], visibility = ["//visibility:public"], ) -""", - ), - struct( - msg = "Swift library target with directory resource", - name = "SwiftLibraryWithDirResource", - is_directory = {"Resources/Assets.xcassets": True}, - 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_SwiftLibraryWithDirResource_resource_bundle", - bundle_name = "Source_SwiftLibraryWithDirResource_resource_bundle", - infoplists = [":Source_SwiftLibraryWithDirResource_resource_bundle_infoplist"], - resources = glob(["Source/SwiftLibraryWithDirResource/Resources/Assets.xcassets/**"]), -) - -resource_bundle_accessor( - name = "Source_SwiftLibraryWithDirResource_resource_bundle_accessor", - bundle_name = "Source_SwiftLibraryWithDirResource_resource_bundle", -) - -resource_bundle_infoplist( - name = "Source_SwiftLibraryWithDirResource_resource_bundle_infoplist", - region = "en", -) - -swift_library( - name = "Source_SwiftLibraryWithDirResource", - data = [":Source_SwiftLibraryWithDirResource_resource_bundle"], - defines = ["SWIFT_PACKAGE"], - deps = [], - module_name = "SwiftLibraryWithDirResource", - srcs = [ - "Source/SwiftLibraryWithDirResource/SwiftLibraryWithDirResource.swift", - ":Source_SwiftLibraryWithDirResource_resource_bundle_accessor", - ], - tags = ["manual"], - visibility = ["//visibility:public"], -) -""", - ), - struct( - msg = "Swift library target with file and directory resources", - name = "SwiftLibraryWithFileAndDirResources", - is_directory = {"Resources/Assets.xcassets": True}, - 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_SwiftLibraryWithFileAndDirResources_resource_bundle", - bundle_name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle", - infoplists = [":Source_SwiftLibraryWithFileAndDirResources_resource_bundle_infoplist"], - resources = glob(["Source/SwiftLibraryWithFileAndDirResources/Resources/Assets.xcassets/**"]) + ["Source/SwiftLibraryWithFileAndDirResources/Resources/chicken.json"], -) - -resource_bundle_accessor( - name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle_accessor", - bundle_name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle", -) - -resource_bundle_infoplist( - name = "Source_SwiftLibraryWithFileAndDirResources_resource_bundle_infoplist", - region = "en", -) - -swift_library( - name = "Source_SwiftLibraryWithFileAndDirResources", - data = [":Source_SwiftLibraryWithFileAndDirResources_resource_bundle"], - defines = ["SWIFT_PACKAGE"], - deps = [], - module_name = "SwiftLibraryWithFileAndDirResources", - srcs = [ - "Source/SwiftLibraryWithFileAndDirResources/SwiftLibraryWithFileAndDirResources.swift", - ":Source_SwiftLibraryWithFileAndDirResources_resource_bundle_accessor", - ], - tags = ["manual"], - visibility = ["//visibility:public"], -) """, ), ] From 8b2f920d90410ea6d1dbdf80d9d8ed75eb419e4e Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sun, 10 Sep 2023 10:13:46 -0600 Subject: [PATCH 12/12] Remove TODO comment --- swiftpkg/internal/pkginfos.bzl | 2 -- 1 file changed, 2 deletions(-) diff --git a/swiftpkg/internal/pkginfos.bzl b/swiftpkg/internal/pkginfos.bzl index d28fc2662..9039df05d 100644 --- a/swiftpkg/internal/pkginfos.bzl +++ b/swiftpkg/internal/pkginfos.bzl @@ -780,8 +780,6 @@ def _new_swift_src_info_from_sources(repository_ctx, target_path, sources): target_path, ) - # TODO(chuck): Remove is_directory check. - # The paths should be relative to the target not the root of the workspace. # Do not include directories in the output. discovered_res_files = [