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

feat: Add option to control verbosity of write_source_files. #1033

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion docs/write_source_files.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 32 additions & 4 deletions lib/private/write_source_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def write_source_file(
diff_test_failure_message = "{{DEFAULT_MESSAGE}}",
file_missing_failure_message = "{{DEFAULT_MESSAGE}}",
check_that_out_file_exists = True,
verbosity = "full",
**kwargs):
"""Write a file or directory to the source tree.

Expand Down Expand Up @@ -71,6 +72,8 @@ def write_source_file(
If `True`, the output file or directory must be in the same containing Bazel package as the target since the underlying mechanism
for this check is limited to files in the same Bazel package.

verbosity: Verbosity of message being when the copy target is run. One of `full`, `brief`, `quiet`.

**kwargs: Other common named parameters such as `tags` or `visibility`

Returns:
Expand Down Expand Up @@ -101,6 +104,7 @@ def write_source_file(
out_file = str(out_file) if out_file else None,
executable = executable,
additional_update_targets = additional_update_targets,
verbosity = verbosity,
**kwargs
)

Expand Down Expand Up @@ -202,6 +206,9 @@ _write_source_file_attrs = {
mandatory = False,
providers = [WriteSourceFileInfo],
),
"verbosity": attr.string(
values = ["full", "short", "quiet"],
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
"_macos_constraint": attr.label(default = "@platforms//os:macos"),
}
Expand Down Expand Up @@ -237,14 +244,23 @@ fi"""]
# Remove execute/search bit recursively from files bit not directories: https://superuser.com/a/434418
executable_dir = "chmod -R -x+X \"$out\""

progress_message_dir = ""
progress_message_file = ""
if ctx.attr.verbosity == "full":
progress_message_dir = "echo \"Copying directory $in to $out in $PWD\""
progress_message_file = "echo \"Copying file $in to $out in $PWD\""
elif ctx.attr.verbosity == "short":
progress_message_dir = "echo \"Updating directory $out\""
progress_message_file = "echo \"Updating file $out\""

for in_path, out_path in paths:
contents.append("""
in=$runfiles_dir/{in_path}
out={out_path}

mkdir -p "$(dirname "$out")"
if [[ -f "$in" ]]; then
echo "Copying file $in to $out in $PWD"
{progress_message_file}
# in case `cp` from previous command was terminated midway which can result in read-only files/dirs
chmod -R +w "$out" > /dev/null 2>&1 || true
rm -Rf "$out"
Expand All @@ -254,7 +270,7 @@ if [[ -f "$in" ]]; then
# cp should make the file not-executable but set the desired execute bit in both cases as a defense in depth
{executable_file}
else
echo "Copying directory $in to $out in $PWD"
{progress_message_dir}
# in case `cp` from previous command was terminated midway which can result in read-only files/dirs
chmod -R +w "$out" > /dev/null 2>&1 || true
rm -Rf "$out"/{{*,.[!.]*}}
Expand All @@ -268,6 +284,8 @@ fi
out_path = out_path,
executable_file = executable_file,
executable_dir = executable_dir,
progress_message_dir = progress_message_dir,
progress_message_file = progress_message_file,
))

contents.extend([
Expand Down Expand Up @@ -304,6 +322,12 @@ if defined BUILD_WORKSPACE_DIRECTORY (
cd %BUILD_WORKSPACE_DIRECTORY%
)"""]

progress_message = ""
if ctx.attr.verbosity == "full":
progress_message = "echo Copying %in% to %out% in %cd%"
elif ctx.attr.verbosity == "short":
progress_message = "echo Updating %out%"

for in_path, out_path in paths:
contents.append("""
set in=%runfiles_dir%\\{in_path}
Expand All @@ -317,15 +341,19 @@ if not defined BUILD_WORKSPACE_DIRECTORY (
del %out%
)

echo Copying %in% to %out% in %cd%
{progress_message}

if exist "%in%\\*" (
mkdir "%out%" >NUL 2>NUL
robocopy "%in%" "%out%" /E >NUL
) else (
copy %in% %out% >NUL
)
""".format(in_path = in_path.replace("/", "\\"), out_path = out_path.replace("/", "\\")))
""".format(
in_path = in_path.replace("/", "\\"),
out_path = out_path.replace("/", "\\"),
progress_message = progress_message,
))

contents.extend([
"cd %runfiles_dir%",
Expand Down
Loading