-
-
Notifications
You must be signed in to change notification settings - Fork 5
[metadata] extensions: define package_metadata_override rule
#106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yannic
wants to merge
4
commits into
main
Choose a base branch
from
yannic-extensions-metadata-override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| load("@bazel_skylib//rules:diff_test.bzl", "diff_test") | ||
| load("@stardoc//stardoc:stardoc.bzl", "stardoc") | ||
|
|
||
| stardoc( | ||
| name = "defs", | ||
| out = "defs.generated.md", | ||
| input = "@package_metadata_extensions//:defs.bzl", | ||
| deps = [ | ||
| "@package_metadata_extensions//:srcs", | ||
| ], | ||
| ) | ||
|
|
||
| diff_test( | ||
| name = "defs_test", | ||
| file1 = ":defs", | ||
| file2 = "defs.md", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Module `@package_metadata_extensions` | ||
|
|
||
| Extensions to [@package_metadata](../metadata) that require newer Bazel versions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <!-- Generated with Stardoc: http://skydoc.bazel.build --> | ||
|
|
||
| Public API of `@package_metadata_extensions`. | ||
|
|
||
| <a id="package_metadata_override"></a> | ||
|
|
||
| ## package_metadata_override | ||
|
|
||
| <pre> | ||
| load("@package_metadata_extensions//:defs.bzl", "package_metadata_override") | ||
|
|
||
| package_metadata_override(*, <a href="#package_metadata_override-name">name</a>, <a href="#package_metadata_override-metadata">metadata</a>, <a href="#package_metadata_override-targets">targets</a>, <a href="#package_metadata_override-visibility">visibility</a>) | ||
| </pre> | ||
|
|
||
|
|
||
|
|
||
| **ATTRIBUTES** | ||
|
|
||
|
|
||
| | Name | Description | Type | Mandatory | Default | | ||
| | :------------- | :------------- | :------------- | :------------- | :------------- | | ||
| | <a id="package_metadata_override-name"></a>name | A unique name for this macro instance. Normally, this is also the name for the macro's main or only target. The names of any other targets that this macro might create will be this name with a string suffix. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | | ||
| | <a id="package_metadata_override-metadata"></a>metadata | A `package_metadata` target to use for the provided packages.<br><br>This overrides any `package_metadata` directly declared by the packages. | <a href="https://bazel.build/concepts/labels">Label</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | | | ||
| | <a id="package_metadata_override-targets"></a>targets | A list of targets the override applies to.<br><br>This allows passing targets of the form<br><br> - `//foo/...` matches all targets under `//foo` in the current repository. - `//foo` matches all targets in `//foo` (but not in subpackages). - `//foo:bar` matches exactly target `//foo:bar`.<br><br>This also supports referencing packages and targets in external repositories.<br><br> - `@other_repo//foo/...` matches all targets under `//foo` in `other_repo`. - `@other_repo//foo` matches all targets in `//foo` in `other_repo` (but not in subpackages). - `@other_repo//foo:bar` matches exactly target `//foo:bar` in `other_repo`. | List of strings; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | | | ||
| | <a id="package_metadata_override-visibility"></a>visibility | The visibility to be passed to this macro's exported targets. It always implicitly includes the location where this macro is instantiated, so this attribute only needs to be explicitly set if you want the macro's targets to be additionally visible somewhere else. | <a href="https://bazel.build/concepts/labels">List of labels</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | optional | | | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| load("@bazel_skylib//rules:diff_test.bzl", "diff_test") | ||
| load("@stardoc//stardoc:stardoc.bzl", "stardoc") | ||
|
|
||
| [ | ||
| [ | ||
| stardoc( | ||
| name = file, | ||
| out = "{}.generated.md".format(file), | ||
| input = "@package_metadata_extensions//rules:{}.bzl".format(file), | ||
| deps = [ | ||
| "@package_metadata_extensions//rules:srcs", | ||
| ], | ||
| ), | ||
| diff_test( | ||
| name = "{}_test".format(file), | ||
| file1 = ":{}".format(file), | ||
| file2 = "{}.md".format(file), | ||
| ), | ||
| ] | ||
| for file in [ | ||
| "package_metadata_override", | ||
| ] | ||
| ] |
27 changes: 27 additions & 0 deletions
27
docs/metadata-extensions/rules/package_metadata_override.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <!-- Generated with Stardoc: http://skydoc.bazel.build --> | ||
|
|
||
| Declares rule `package_metadata_override`. | ||
|
|
||
| <a id="package_metadata_override"></a> | ||
|
|
||
| ## package_metadata_override | ||
|
|
||
| <pre> | ||
| load("@package_metadata_extensions//rules:package_metadata_override.bzl", "package_metadata_override") | ||
|
|
||
| package_metadata_override(*, <a href="#package_metadata_override-name">name</a>, <a href="#package_metadata_override-metadata">metadata</a>, <a href="#package_metadata_override-targets">targets</a>, <a href="#package_metadata_override-visibility">visibility</a>) | ||
| </pre> | ||
|
|
||
|
|
||
|
|
||
| **ATTRIBUTES** | ||
|
|
||
|
|
||
| | Name | Description | Type | Mandatory | Default | | ||
| | :------------- | :------------- | :------------- | :------------- | :------------- | | ||
| | <a id="package_metadata_override-name"></a>name | A unique name for this macro instance. Normally, this is also the name for the macro's main or only target. The names of any other targets that this macro might create will be this name with a string suffix. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | | ||
| | <a id="package_metadata_override-metadata"></a>metadata | A `package_metadata` target to use for the provided packages.<br><br>This overrides any `package_metadata` directly declared by the packages. | <a href="https://bazel.build/concepts/labels">Label</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | | | ||
| | <a id="package_metadata_override-targets"></a>targets | A list of targets the override applies to.<br><br>This allows passing targets of the form<br><br> - `//foo/...` matches all targets under `//foo` in the current repository. - `//foo` matches all targets in `//foo` (but not in subpackages). - `//foo:bar` matches exactly target `//foo:bar`.<br><br>This also supports referencing packages and targets in external repositories.<br><br> - `@other_repo//foo/...` matches all targets under `//foo` in `other_repo`. - `@other_repo//foo` matches all targets in `//foo` in `other_repo` (but not in subpackages). - `@other_repo//foo:bar` matches exactly target `//foo:bar` in `other_repo`. | List of strings; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | required | | | ||
| | <a id="package_metadata_override-visibility"></a>visibility | The visibility to be passed to this macro's exported targets. It always implicitly includes the location where this macro is instantiated, so this attribute only needs to be explicitly set if you want the macro's targets to be additionally visible somewhere else. | <a href="https://bazel.build/concepts/labels">List of labels</a>; <a href="https://bazel.build/reference/be/common-definitions#configurable-attributes">nonconfigurable</a> | optional | | | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| """Public API of `@package_metadata_extensions`.""" | ||
|
|
||
| load("//rules:package_metadata_override.bzl", _package_metadata_override = "package_metadata_override") | ||
|
|
||
| visibility("public") | ||
|
|
||
| # Rules | ||
| package_metadata_override = _package_metadata_override |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| exports_files( | ||
| [ | ||
| "package_specification_info.bzl", | ||
| ], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| filegroup( | ||
| name = "srcs", | ||
| srcs = [ | ||
| "package_specification_info.bzl", | ||
| ], | ||
| visibility = ["//visibility:public"], | ||
| ) |
54 changes: 54 additions & 0 deletions
54
metadata-extensions/providers/package_specification_info.bzl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Declares provider `PackageSpecificationInfo`.""" | ||
|
|
||
| visibility([ | ||
| "//...", | ||
| ]) | ||
|
|
||
| def _contains_impl(configs, label): | ||
| config = configs.get(label.repo_name, None) | ||
| if not config: | ||
| return False | ||
|
|
||
| for cfg in config: | ||
| if cfg.recursive: | ||
| if label.package.startswith(cfg.package): | ||
| return True | ||
| continue | ||
|
|
||
| if label.package != cfg.package: | ||
| continue | ||
|
|
||
| if cfg.target != None: | ||
| if label.name == cfg.target: | ||
| return True | ||
| else: | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| def _contains(negative, positive, label): | ||
| if _contains_impl(negative, label): | ||
| return False | ||
|
|
||
| return _contains_impl(positive, label) | ||
|
|
||
| def _init(negative, positive): | ||
| return { | ||
| "contains": lambda target: _contains(negative, positive, target), | ||
| } | ||
|
|
||
| PackageSpecificationInfo, _create = provider( | ||
| doc = """ | ||
| Information about transitive package specifications used in package groups. | ||
|
|
||
| This is a Starlark implementation of [PackageSpecificationInfo](https://bazel.build/rules/lib/providers/PackageSpecificationInfo). | ||
| """.strip(), | ||
| fields = { | ||
| "contains": """ | ||
| Checks if a target exists in a package group. | ||
|
|
||
| Parameter **MUST** be a label. | ||
| """.strip(), | ||
| }, | ||
| init = _init, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| exports_files( | ||
| [ | ||
| "package_metadata_override.bzl", | ||
| ], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| filegroup( | ||
| name = "srcs", | ||
| srcs = [ | ||
| "package_group.bzl", | ||
| "package_metadata_override.bzl", | ||
| ] + [ | ||
| "//providers:srcs", | ||
| "@package_metadata//providers:srcs", | ||
| ], | ||
| visibility = ["//visibility:public"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Declares rule `package_group`.""" | ||
|
|
||
| load("//providers:package_specification_info.bzl", "PackageSpecificationInfo") | ||
|
|
||
| visibility([ | ||
| "//...", | ||
| ]) | ||
|
|
||
| def _package_group_rule_impl(ctx): | ||
| positive = {} | ||
| negative = {} | ||
| for pattern in [json.decode(pattern) for pattern in ctx.attr.patterns]: | ||
| if pattern["negative"]: | ||
| config = negative | ||
| else: | ||
| config = positive | ||
|
|
||
| repo = pattern["repo"] | ||
| config.setdefault(repo, []).append(struct( | ||
| package = pattern["package"], | ||
| target = pattern["target"], | ||
| recursive = pattern["recursive"], | ||
| )) | ||
|
|
||
| return [ | ||
| PackageSpecificationInfo( | ||
| negative = negative, | ||
| positive = positive, | ||
| ), | ||
| ] | ||
|
|
||
| _package_group_rule = rule( | ||
| implementation = _package_group_rule_impl, | ||
| attrs = { | ||
| "patterns": attr.string_list( | ||
| mandatory = True, | ||
| ), | ||
| }, | ||
| provides = [ | ||
| PackageSpecificationInfo, | ||
| ], | ||
| ) | ||
|
|
||
| def _package_group_impl(name, packages, visibility): | ||
| patterns = [] | ||
| for label, configs in packages.items(): | ||
| for config in json.decode(configs): | ||
| patterns.append({ | ||
| "negative": config["negative"], | ||
| "package": config["package"], | ||
| "recursive": config["recursive"], | ||
| "repo": label.repo_name, | ||
| "target": config["target"], | ||
| }) | ||
|
|
||
| _package_group_rule( | ||
| name = name, | ||
| visibility = visibility, | ||
| patterns = [json.encode(pattern) for pattern in patterns], | ||
| ) | ||
|
|
||
| package_group = macro( | ||
| implementation = _package_group_impl, | ||
| attrs = { | ||
| "packages": attr.label_keyed_string_dict( | ||
| mandatory = True, | ||
| configurable = False, | ||
| doc = """ | ||
| A dict from a dummy label to the parsed configuration. | ||
|
|
||
| Key: a label in the repository of the target pattern. Only `Label.repo_name` is | ||
| used. Does not need to extist. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: typo |
||
| Value: a `json.encode()`'d list of dicts defining the configuration of the | ||
| target pattern. | ||
|
|
||
| ```starlark | ||
| { | ||
| "negative": True | False | ||
| "pattern": "path/to/package/...", | ||
| } | ||
| ``` | ||
| """.strip(), | ||
| ), | ||
| }, | ||
| doc = """ | ||
| A starlark implementation of [package_group](https://bazel.build/reference/be/functions#package_group). | ||
|
|
||
| Unlike the native implementation in Bazel, this allows patterns referencing other repositories. | ||
|
|
||
| This has a very subtle API. Not for direct use. | ||
| """.strip(), | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.