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

Allow substitution of user-defined variables in RPM preamble #787

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/private/util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ def substitute_package_variables(ctx, attribute_value):

# Map $(var) to {x} and then use format for substitution.
# This is brittle and I hate it. We should have template substitution
# in the Starlark runtime.
return attribute_value.replace("$(", "{").replace(")", "}").format(**vars)
# in the Starlark runtime. This loop compensates for mismatched counts
# of $(foo) so that we don't try replace things like (bar) because we
# have no regex matching
for _ in range(attribute_value.count("$(")):
if attribute_value.find(")") == -1:
aiuto marked this conversation as resolved.
Show resolved Hide resolved
fail("mismatched variable declaration")

attribute_value = attribute_value.replace("$(", "{", 1)
attribute_value = attribute_value.replace(")", "}", 1)

return attribute_value.format(**vars)
4 changes: 2 additions & 2 deletions pkg/rpm_pfg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load(
"PackageSymlinkInfo",
"PackageVariablesInfo",
)
load("//pkg/private:util.bzl", "setup_output_files")
load("//pkg/private:util.bzl", "setup_output_files", "substitute_package_variables")

rpm_filetype = [".rpm"]

Expand Down Expand Up @@ -349,7 +349,7 @@ def _pkg_rpm_impl(ctx):
)
ctx.actions.write(
output = preamble_file,
content = "\n".join(preamble_pieces),
content = substitute_package_variables(ctx, "\n".join(preamble_pieces)),
)
files.append(preamble_file)
args.append("--preamble=" + preamble_file.path)
Expand Down