Skip to content

Commit

Permalink
fix: handling of .xcdatamodeld resources (#875)
Browse files Browse the repository at this point in the history
Similar to asset catalogs, we want to include all of the files under
`.xcdatamodeld` directories.

---------

Signed-off-by: Brentley Jones <[email protected]>
Co-authored-by: Chuck Grindel <[email protected]>
  • Loading branch information
brentleyjones and cgrindel authored Feb 10, 2024
1 parent 9bbd940 commit b599933
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
29 changes: 15 additions & 14 deletions swiftpkg/internal/resource_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
# For a list of the auto-discovered resource types, see
# https://github.com/apple/swift-package-manager/blob/main/Sources/PackageLoading/TargetSourcesBuilder.swift#L634-L677
_XIB_EXTS = ["nib", "xib", "storyboard"]
_ASSET_CATALOG_EXTS = ["xcassets"]
_STRING_CATALOG_EXTS = ["xcstrings"]
_COREDATA_EXTS = ["xcdatamodeld", "xcdatamodel", "xcmappingmodel"]
_COREDATA_EXTS = ["xcmappingmodel"]
_METAL_EXTS = ["metal"]
_ALL_AUTO_DISCOVERED_RES_EXTS = _XIB_EXTS + _ASSET_CATALOG_EXTS + \
_STRING_CATALOG_EXTS + _COREDATA_EXTS + _METAL_EXTS
_ALL_AUTO_DISCOVERED_RES_EXTS_SET = sets.make(_ALL_AUTO_DISCOVERED_RES_EXTS)
_ALL_AUTO_DISCOVERED_RES_EXTS_SET = sets.make(
_XIB_EXTS + _STRING_CATALOG_EXTS + _COREDATA_EXTS + _METAL_EXTS,
)

def _is_under_asset_catalog_dir(path):
for ext in _ASSET_CATALOG_EXTS:
# This won't work for Windows. It is unclear how to determine to proper
# separator to use. The bazel-skylib paths.bzl just uses forward slash
# (/) without checking.
pattern = ".{}/".format(ext)
if path.find(pattern) > 0:
return True
return False
# This won't work for Windows. It is unclear how to determine to proper
# separator to use. The bazel-skylib paths.bzl just uses forward slash
# (/) without checking.
return path.find(".xcassets/") > 0

def _is_under_xcdatamodeld_dir(path):
# This won't work for Windows. It is unclear how to determine to proper
# separator to use. The bazel-skylib paths.bzl just uses forward slash
# (/) without checking.
return path.find(".xcdatamodeld/") > 0

def _is_auto_discovered_resource(path):
"""Determines whether the specified path points to an auto-discoverable \
Expand All @@ -43,7 +44,7 @@ def _is_auto_discovered_resource(path):
ext = ext_with_dot[1:] if ext_with_dot != "" else ""
return sets.contains(_ALL_AUTO_DISCOVERED_RES_EXTS_SET, ext) or \
_is_under_asset_catalog_dir(path) or \
False
_is_under_xcdatamodeld_dir(path)

resource_files = struct(
is_auto_discovered_resource = _is_auto_discovered_resource,
Expand Down
10 changes: 7 additions & 3 deletions swiftpkg/tests/resource_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ def _is_auto_discovered_resource_test(ctx):
struct(msg = "nib", path = "foo.nib", exp = True),
struct(msg = "xib", path = "foo.xib", exp = True),
struct(msg = "storyboard", path = "foo.storyboard", exp = True),
struct(msg = "xcassets dir", path = "foo.xcassets", exp = True),
struct(msg = "xcassets dir", path = "foo.xcassets", exp = False),
struct(
msg = "file under xcassets dir",
path = "foo.xcassets/bar.png",
exp = True,
),
struct(msg = "xcstrings", path = "foo.xcstrings", exp = True),
struct(msg = "xcdatamodeld", path = "foo.xcdatamodeld", exp = True),
struct(msg = "xcdatamodel", path = "foo.xcdatamodel", exp = True),
struct(msg = "xcdatamodeld", path = "foo.xcdatamodeld", exp = False),
struct(
msg = "content under .xcdatamodeld",
path = "foo.xcdatamodeld/bar.xcdatamodel/content",
exp = True,
),
struct(msg = "xcmappingmodel", path = "foo.xcmappingmodel", exp = True),
struct(msg = "metal", path = "foo.metal", exp = True),
struct(msg = "swift", path = "foo.swift", exp = False),
Expand Down

0 comments on commit b599933

Please sign in to comment.