Skip to content
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

pants: Add st2_shell_sources_and_resources() macro to pants-plugins/macros.py #5890

Merged
merged 6 commits into from Feb 14, 2023

Conversation

cognifloyd
Copy link
Member

Background

This is another part of introducing pants, as discussed in various TSC meetings.

Related PRs can be found in:

Overview of this PR

This PR adds our first pants macro: st2_shell_sources_and_resources()

I'm planning to add 3 more macros to help with building our python_distribution targets (for buildling wheels) in future PRs. This just introduces the concept of macros.

Macros

Macros are useful to reduce boilerplate in BUILD files.
...
Macros are defined in Python files that act like a normal BUILD file. They have access to all the symbols you normally have registered in a BUILD file, such as all of your target types.

Macros cannot import other modules, just like BUILD files cannot have import statements.

https://www.pantsbuild.org/docs/macros

Our macro file is pants-plugins/macros.py, and we tell pants about it with [GLOBAL].build_file_prelude_globs in pants.toml:

st2/pants.toml

Line 11 in eb6902f

build_file_prelude_globs = ["pants-plugins/macros.py"]

This PR only introduces one macro. The key parts of this macro function are shell_sources(...) and resources(...) - ie, that's where our macro adds the normal pants targets using the kwargs passed to it.

def st2_shell_sources_and_resources(**kwargs):
"""This creates a shell_sources and a resources target.
This is needed because python_sources dependencies on shell_sources
are silently ignored. So, we also need the resources target
to allow depending on them.
"""
shell_sources(**kwargs) # noqa: F821
kwargs.pop("skip_shellcheck", None)
kwargs["name"] += "_resources"
resources(**kwargs) # noqa: F821

And then we use the macro just like a regular target in a BUILD file:

st2_shell_sources_and_resources(
name="shell",
sources=["*.sh"],
skip_shellcheck=True,
)

@cognifloyd cognifloyd added this to the pants milestone Feb 6, 2023
@cognifloyd cognifloyd self-assigned this Feb 6, 2023
@pull-request-size pull-request-size bot added the size/M PR that changes 30-99 lines. Good size to review. label Feb 6, 2023
@cognifloyd
Copy link
Member Author

Rebased and added pants-plugins/README.md entry.

@cognifloyd cognifloyd requested a review from a team February 8, 2023 19:02
auto-merge was automatically disabled February 9, 2023 20:11

Merge queue setting changed

auto-merge was automatically disabled February 9, 2023 20:24

Merge queue setting changed

Pants macros are used to define functions that can be used in BUILD
files to inject additional functions that can add multiple targets
to the BUILD file at once. These functions look like "targets"
without building a whole plugin for them.
@cognifloyd cognifloyd merged commit 975c798 into master Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance pantsbuild size/M PR that changes 30-99 lines. Good size to review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants