From b95f423026468b2bb16cbe775fa2a9594dc9d3fd Mon Sep 17 00:00:00 2001 From: Dean Lindqvist Todevski Date: Fri, 17 Jan 2025 08:18:06 +0100 Subject: [PATCH 1/2] add verbosity option to `write_source_files` --- lib/private/write_source_file.bzl | 36 +++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/private/write_source_file.bzl b/lib/private/write_source_file.bzl index bfdd51da9..476268675 100644 --- a/lib/private/write_source_file.bzl +++ b/lib/private/write_source_file.bzl @@ -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. @@ -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: @@ -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 ) @@ -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"), } @@ -237,6 +244,15 @@ 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} @@ -244,7 +260,7 @@ 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" @@ -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"/{{*,.[!.]*}} @@ -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([ @@ -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} @@ -317,7 +341,7 @@ 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 @@ -325,7 +349,11 @@ if exist "%in%\\*" ( ) 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%", From 9ff209b19a0f37fc2761e9dd59d8e52b8699a955 Mon Sep 17 00:00:00 2001 From: Dean Lindqvist Todevski Date: Fri, 17 Jan 2025 08:19:19 +0100 Subject: [PATCH 2/2] update docs --- docs/write_source_files.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/write_source_files.md b/docs/write_source_files.md index 2db201290..8759937f6 100644 --- a/docs/write_source_files.md +++ b/docs/write_source_files.md @@ -123,7 +123,7 @@ Provider for write_source_file targets
 write_source_file(name, in_file, out_file, executable, additional_update_targets,
                   suggested_update_target, diff_test, diff_test_failure_message,
-                  file_missing_failure_message, check_that_out_file_exists, kwargs)
+                  file_missing_failure_message, check_that_out_file_exists, verbosity, kwargs)
 
Write a file or directory to the source tree. @@ -148,6 +148,7 @@ To disable the exists check and up-to-date test set `diff_test` to `False`. | diff_test_failure_message | Text to print when the diff test fails, with templating options for relevant targets.

Substitutions are performed on the failure message, with the following substitutions being available:

`{{DEFAULT_MESSAGE}}`: Prints the default error message, listing the target(s) that may be run to update the file(s).

`{{TARGET}}`: The target to update the individual file that does not match in the diff test.

`{{SUGGESTED_UPDATE_TARGET}}`: The suggested_update_target if specified. | `"{{DEFAULT_MESSAGE}}"` | | file_missing_failure_message | Text to print when the output file is missing. Subject to the same substitutions as diff_test_failure_message. | `"{{DEFAULT_MESSAGE}}"` | | check_that_out_file_exists | Test that the output file exists and print a helpful error message if it doesn't.

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. | `True` | +| verbosity | Verbosity of message being when the copy target is run. One of `full`, `brief`, `quiet`. | `"full"` | | kwargs | Other common named parameters such as `tags` or `visibility` | none | **RETURNS**