Skip to content

Commit 742d3f6

Browse files
committed
extra_files aspect hint
Signed-off-by: John Flanagan <[email protected]>
1 parent 8fac6d1 commit 742d3f6

File tree

10 files changed

+180
-1
lines changed

10 files changed

+180
-1
lines changed

docs/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
55
# Generating
66

77
_DOC_COMPONENTS = [
8+
"aspect_hints",
89
"project_options",
910
"providers",
1011
"top_level_target",
@@ -20,7 +21,10 @@ _DOC_COMPONENTS = [
2021
out = "{}.md".format(component),
2122
input = "//xcodeproj/internal/docs:{}.bzl".format(component),
2223
tags = ["manual"],
23-
deps = ["//xcodeproj/internal"],
24+
deps = [
25+
"//xcodeproj/internal",
26+
"//xcodeproj/internal/files",
27+
],
2428
)
2529
for component in _DOC_COMPONENTS
2630
]
@@ -36,6 +40,7 @@ genrule(
3640
":xcode_schemes",
3741
":xcode_build_settings",
3842
":providers",
43+
":aspect_hints",
3944
],
4045
outs = ["bazel.generated.md"],
4146
cmd = """\

docs/bazel.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ load("@rules_xcodeproj//xcodeproj:defs.bzl", "xcodeproj")
5050
- [Providers](#providers)
5151
- [`XcodeProjAutomaticTargetProcessingInfo`](#XcodeProjAutomaticTargetProcessingInfo)
5252
- [`XcodeProjInfo`](#XcodeProjInfo)
53+
- [Aspect Hints](#aspect-hints)
54+
- [`xcodeproj_extra_files`](#xcodeproj_extra_files)
5355

5456
# Core
5557

@@ -1095,3 +1097,47 @@ Provides information needed to generate an Xcode project.
10951097
| <a id="XcodeProjInfo-xcode_targets"></a>xcode_targets | A `depset` of values from `xcode_targets.make`, which potentially will become targets in the Xcode project. |
10961098

10971099

1100+
# Aspect Hints
1101+
1102+
Aspect hints that can be used to provide additional information during project generation.
1103+
1104+
<a id="xcodeproj_extra_files"></a>
1105+
1106+
## xcodeproj_extra_files
1107+
1108+
<pre>
1109+
xcodeproj_extra_files(<a href="#xcodeproj_extra_files-name">name</a>, <a href="#xcodeproj_extra_files-files">files</a>)
1110+
</pre>
1111+
1112+
This rule is used to surface extra files that should be included in the Xcode
1113+
project navigator, but otherwise aren't inputs to a target. The provider
1114+
created by this rule should be attached to the related target via an aspect
1115+
hint.
1116+
1117+
This is only used when xcodeproj.generation_mode = "incremental" is set.
1118+
1119+
**EXAMPLE**
1120+
1121+
```starlark
1122+
swift_library(
1123+
...
1124+
aspect_hints = [":library_extra_files"],
1125+
...
1126+
)
1127+
1128+
# Display the README.md file located alongside the Swift library in Xcode
1129+
xcodeproj_extra_files(
1130+
name = "library_extra_files",
1131+
files = ["README.md"],
1132+
)
1133+
```
1134+
1135+
**ATTRIBUTES**
1136+
1137+
1138+
| Name | Description | Type | Mandatory | Default |
1139+
| :------------- | :------------- | :------------- | :------------- | :------------- |
1140+
| <a id="xcodeproj_extra_files-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
1141+
| <a id="xcodeproj_extra_files-files"></a>files | The list of extra files to surface in the Xcode navigator. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
1142+
1143+

test/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test_suite(
1515
"//test/internal/targets",
1616
"//test/internal/xcode_schemes",
1717
"//test/internal/xcschemes",
18+
"//test/internal/xcodeproj_extra_files",
1819
],
1920
)
2021

@@ -32,6 +33,7 @@ bzl_library(
3233
"//test/internal/targets:bzls",
3334
"//test/internal/xcode_schemes:bzls",
3435
"//test/internal/xcschemes:bzls",
36+
"//test/internal/xcodeproj_extra_files:bzls",
3537
],
3638
)
3739

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load(":xcodeproj_extra_files_tests.bzl", "xcodeproj_extra_files_test_suite")
3+
4+
xcodeproj_extra_files_test_suite(name = "xcodeproj_extra_files_test")
5+
6+
test_suite(name = "xcodeproj_extra_files")
7+
8+
bzl_library(
9+
name = "bzls",
10+
srcs = glob(
11+
["*.bzl"],
12+
exclude = ["utils.bzl"],
13+
),
14+
visibility = ["//test:__pkg__"],
15+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "analysistest")
2+
load("//xcodeproj/internal/files:xcodeproj_extra_files.bzl", "xcodeproj_extra_files", "XcodeProjExtraFilesHintInfo")
3+
4+
def _provider_contents_test_impl(ctx):
5+
env = analysistest.begin(ctx)
6+
7+
target_under_test = analysistest.target_under_test(env)
8+
9+
files_list = target_under_test[XcodeProjExtraFilesHintInfo].files.to_list()
10+
11+
asserts.equals(env, len(files_list), 1)
12+
asserts.equals(env, files_list[0].path, "test/internal/xcodeproj_extra_files/BUILD")
13+
14+
return analysistest.end(env)
15+
16+
provider_contents_test = analysistest.make(_provider_contents_test_impl)
17+
18+
def _test_provider_contents():
19+
xcodeproj_extra_files(
20+
name = "xcodeproj_extra_files_subject",
21+
files = ["BUILD"],
22+
tags = ["manual"],
23+
)
24+
25+
provider_contents_test(
26+
name = "provider_contents_test",
27+
target_under_test = ":xcodeproj_extra_files_subject",
28+
)
29+
30+
def xcodeproj_extra_files_test_suite(name):
31+
_test_provider_contents()
32+
33+
native.test_suite(
34+
name = name,
35+
tests = [
36+
":provider_contents_test",
37+
],
38+
)

xcodeproj/defs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ load(
2323
_xcodeproj = "xcodeproj",
2424
)
2525
load("//xcodeproj/internal:xcodeprojinfo.bzl", _XcodeProjInfo = "XcodeProjInfo")
26+
load("//xcodeproj/internal/files:xcodeproj_extra_files.bzl", _xcodeproj_extra_files = "xcodeproj_extra_files")
2627
load("//xcodeproj/internal/xcschemes:xcschemes.bzl", _xcschemes = "xcschemes")
2728

2829
# Re-exporting providers
@@ -34,6 +35,7 @@ project_options = _project_options
3435
top_level_target = _top_level_target
3536
top_level_targets = _top_level_targets
3637
xcodeproj = _xcodeproj
38+
xcodeproj_extra_files = _xcodeproj_extra_files
3739
xcode_provisioning_profile = _xcode_provisioning_profile
3840

3941
# Re-exporting APIs
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""# Aspect Hints
2+
3+
Aspect hints that can be used to provide additional information during project generation.
4+
"""
5+
6+
load("//xcodeproj/internal/files:xcodeproj_extra_files.bzl", _xcodeproj_extra_files = "xcodeproj_extra_files")
7+
8+
xcodeproj_extra_files = _xcodeproj_extra_files

xcodeproj/internal/docs/bazel.header.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ load("@rules_xcodeproj//xcodeproj:defs.bzl", "xcodeproj")
5050
- [Providers](#providers)
5151
- [`XcodeProjAutomaticTargetProcessingInfo`](#XcodeProjAutomaticTargetProcessingInfo)
5252
- [`XcodeProjInfo`](#XcodeProjInfo)
53+
- [Aspect Hints](#aspect-hints)
54+
- [`xcodeproj_extra_files`](#xcodeproj_extra_files)
5355

5456
# Core

xcodeproj/internal/files/incremental_input_files.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ load(
1111
"memory_efficient_depset",
1212
)
1313
load("//xcodeproj/internal:xcodeprojinfo.bzl", "XcodeProjInfo")
14+
load(":xcodeproj_extra_files.bzl", "XcodeProjExtraFilesHintInfo")
1415
load(":incremental_resources.bzl", resources_module = "incremental_resources")
1516
load(":linker_input_files.bzl", "linker_input_files")
1617

@@ -408,6 +409,13 @@ def _collect_incremental_input_files(
408409
rule_files = ctx.rule.files,
409410
)
410411

412+
# Collect any extra fila provided via the `xcodeproj_extra_files` aspect hint
413+
for hint in rule_attr.aspect_hints:
414+
if XcodeProjExtraFilesHintInfo in hint:
415+
hint_extra_files = hint[XcodeProjExtraFilesHintInfo].files
416+
if hint_extra_files:
417+
extra_files.extend(hint_extra_files.to_list())
418+
411419
product_framework_files = memory_efficient_depset(
412420
transitive = [
413421
info.inputs._product_framework_files
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Rule for providing extra files from targets to the project generator"""
2+
3+
XcodeProjExtraFilesHintInfo = provider(
4+
doc = "Provides a list of extra files to include during project generation",
5+
fields = {
6+
"files": "List of files to include in the extra files.",
7+
},
8+
)
9+
10+
def _xcodeproj_extra_files_impl(ctx):
11+
"""Create a provider to surface extra files via an aspect hint.
12+
13+
Args:
14+
ctx: The rule context.
15+
16+
Returns:
17+
A `XcodeProjExtraFilesHintInfo` provider.
18+
"""
19+
return [XcodeProjExtraFilesHintInfo(files = depset(ctx.files.files))]
20+
21+
xcodeproj_extra_files = rule(
22+
doc = """\
23+
This rule is used to surface extra files that should be included in the Xcode
24+
project navigator, but otherwise aren't inputs to a target. The provider
25+
created by this rule should be attached to the related target via an aspect
26+
hint.
27+
28+
This is only used when xcodeproj.generation_mode = "incremental" is set.
29+
30+
**EXAMPLE**
31+
32+
```starlark
33+
swift_library(
34+
...
35+
aspect_hints = [":library_extra_files"],
36+
...
37+
)
38+
39+
# Display the README.md file located alongside the Swift library in Xcode
40+
xcodeproj_extra_files(
41+
name = "library_extra_files",
42+
files = ["README.md"],
43+
)
44+
```
45+
""",
46+
implementation = _xcodeproj_extra_files_impl,
47+
attrs = {
48+
"files": attr.label_list(
49+
doc = "The list of extra files to surface in the Xcode navigator.",
50+
allow_files = True,
51+
),
52+
},
53+
)

0 commit comments

Comments
 (0)