Skip to content

Commit

Permalink
add verbosity option to write_source_files
Browse files Browse the repository at this point in the history
  • Loading branch information
r0bobo committed Jan 25, 2025
1 parent c6707f1 commit b95f423
Showing 1 changed file with 32 additions and 4 deletions.
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

0 comments on commit b95f423

Please sign in to comment.