refactor: rename copy_to_bin to output_filegroup
diff --git a/README.md b/README.md index 5d97e29..18ab6b0 100644 --- a/README.md +++ b/README.md
@@ -22,8 +22,8 @@ - [copy_directory](docs/copy_directory.md) Copies directories to another package. - [copy_file](docs/copy_file.md) Copies files to another package. -- [copy_to_bin](docs/copy_to_bin.md) Copies a source file to output tree at the same workspace-relative path. - [copy_to_directory](docs/copy_to_directory.md) Copies and arranges files and directories into a new directory. +- [output_filegroup](docs/output_filegroup.md) Copies a source file to output tree at the same workspace-relative path. - [write_source_files](docs/write_source_files.md) Write to one or more files or folders in the source tree. Stamp out tests that ensure the sources exist and are up to date. ## Transforming files
diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 7c5c2cd..586ff24 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel
@@ -18,8 +18,8 @@ ) stardoc_with_diff_test( - name = "copy_to_bin", - bzl_library_target = "//lib:copy_to_bin", + name = "output_filegroup", + bzl_library_target = "//lib:output_filegroup", ) stardoc_with_diff_test(
diff --git a/docs/copy_to_bin.md b/docs/copy_to_bin.md deleted file mode 100644 index d1b733d..0000000 --- a/docs/copy_to_bin.md +++ /dev/null
@@ -1,106 +0,0 @@ -<!-- Generated with Stardoc: http://skydoc.bazel.build --> - -A rule that copies source files to the output tree. - -This rule uses a Bash command (diff) on Linux/macOS/non-Windows, and a cmd.exe -command (fc.exe) on Windows (no Bash is required). - -Originally authored in rules_nodejs -https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabea4856f8e/internal/common/copy_to_bin.bzl - - -<a id="copy_file_to_bin_action"></a> - -## copy_file_to_bin_action - -<pre> -copy_file_to_bin_action(<a href="#copy_file_to_bin_action-ctx">ctx</a>, <a href="#copy_file_to_bin_action-file">file</a>, <a href="#copy_file_to_bin_action-is_windows">is_windows</a>) -</pre> - -Helper function that creates an action to copy a file to the output tree. - -File are copied to the same workspace-relative path. The resulting files is -returned. - -If the file passed in is already in the output tree is then it is returned -without a copy action. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| <a id="copy_file_to_bin_action-ctx"></a>ctx | The rule context. | none | -| <a id="copy_file_to_bin_action-file"></a>file | The file to copy. | none | -| <a id="copy_file_to_bin_action-is_windows"></a>is_windows | Deprecated and unused | <code>None</code> | - -**RETURNS** - -A File in the output tree. - - -<a id="copy_files_to_bin_actions"></a> - -## copy_files_to_bin_actions - -<pre> -copy_files_to_bin_actions(<a href="#copy_files_to_bin_actions-ctx">ctx</a>, <a href="#copy_files_to_bin_actions-files">files</a>, <a href="#copy_files_to_bin_actions-is_windows">is_windows</a>) -</pre> - -Helper function that creates actions to copy files to the output tree. - -Files are copied to the same workspace-relative path. The resulting list of -files is returned. - -If a file passed in is already in the output tree is then it is added -directly to the result without a copy action. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| <a id="copy_files_to_bin_actions-ctx"></a>ctx | The rule context. | none | -| <a id="copy_files_to_bin_actions-files"></a>files | List of File objects. | none | -| <a id="copy_files_to_bin_actions-is_windows"></a>is_windows | Deprecated and unused | <code>None</code> | - -**RETURNS** - -List of File objects in the output tree. - - -<a id="copy_to_bin"></a> - -## copy_to_bin - -<pre> -copy_to_bin(<a href="#copy_to_bin-name">name</a>, <a href="#copy_to_bin-srcs">srcs</a>, <a href="#copy_to_bin-kwargs">kwargs</a>) -</pre> - -Copies a source file to output tree at the same workspace-relative path. - -e.g. `<execroot>/path/to/file -> <execroot>/bazel-out/<platform>/bin/path/to/file` - -If a file passed in is already in the output tree is then it is added directly to the -DefaultInfo provided by the rule without a copy. - -This is useful to populate the output folder with all files needed at runtime, even -those which aren't outputs of a Bazel rule. - -This way you can run a binary in the output folder (execroot or runfiles_root) -without that program needing to rely on a runfiles helper library or be aware that -files are divided between the source tree and the output tree. - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| <a id="copy_to_bin-name"></a>name | Name of the rule. | none | -| <a id="copy_to_bin-srcs"></a>srcs | A list of labels. File(s) to copy. | none | -| <a id="copy_to_bin-kwargs"></a>kwargs | further keyword arguments, e.g. <code>visibility</code> | none | - -
diff --git a/docs/output_filegroup.md b/docs/output_filegroup.md new file mode 100644 index 0000000..166bfc3 --- /dev/null +++ b/docs/output_filegroup.md
@@ -0,0 +1,106 @@ +<!-- Generated with Stardoc: http://skydoc.bazel.build --> + +A rule that copies source files to the output tree. + +This rule uses a Bash command (diff) on Linux/macOS/non-Windows, and a cmd.exe +command (fc.exe) on Windows (no Bash is required). + +Originally authored in rules_nodejs +https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabea4856f8e/internal/common/copy_to_bin.bzl + + +<a id="output_file_action"></a> + +## output_file_action + +<pre> +output_file_action(<a href="#output_file_action-ctx">ctx</a>, <a href="#output_file_action-file">file</a>, <a href="#output_file_action-is_windows">is_windows</a>) +</pre> + +Helper function that creates an action to copy a file to the output tree. + +File are copied to the same workspace-relative path. The resulting files is +returned. + +If the file passed in is already in the output tree is then it is returned +without a copy action. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| <a id="output_file_action-ctx"></a>ctx | The rule context. | none | +| <a id="output_file_action-file"></a>file | The file to copy. | none | +| <a id="output_file_action-is_windows"></a>is_windows | Deprecated and unused | <code>None</code> | + +**RETURNS** + +A File in the output tree. + + +<a id="output_filegroup"></a> + +## output_filegroup + +<pre> +output_filegroup(<a href="#output_filegroup-name">name</a>, <a href="#output_filegroup-srcs">srcs</a>, <a href="#output_filegroup-kwargs">kwargs</a>) +</pre> + +Copies a source file to output tree at the same workspace-relative path. + +e.g. `<execroot>/path/to/file -> <execroot>/bazel-out/<platform>/bin/path/to/file` + +If a file passed in is already in the output tree is then it is added directly to the +DefaultInfo provided by the rule without a copy. + +This is useful to populate the output folder with all files needed at runtime, even +those which aren't outputs of a Bazel rule. + +This way you can run a binary in the output folder (execroot or runfiles_root) +without that program needing to rely on a runfiles helper library or be aware that +files are divided between the source tree and the output tree. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| <a id="output_filegroup-name"></a>name | Name of the rule. | none | +| <a id="output_filegroup-srcs"></a>srcs | A list of labels. File(s) to copy. | none | +| <a id="output_filegroup-kwargs"></a>kwargs | further keyword arguments, e.g. <code>visibility</code> | none | + + +<a id="output_files_actions"></a> + +## output_files_actions + +<pre> +output_files_actions(<a href="#output_files_actions-ctx">ctx</a>, <a href="#output_files_actions-files">files</a>, <a href="#output_files_actions-is_windows">is_windows</a>) +</pre> + +Helper function that creates actions to copy files to the output tree. + +Files are copied to the same workspace-relative path. The resulting list of +files is returned. + +If a file passed in is already in the output tree is then it is added +directly to the result without a copy action. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| <a id="output_files_actions-ctx"></a>ctx | The rule context. | none | +| <a id="output_files_actions-files"></a>files | List of File objects. | none | +| <a id="output_files_actions-is_windows"></a>is_windows | Deprecated and unused | <code>None</code> | + +**RETURNS** + +List of File objects in the output tree. + +
diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel index d574991..28b1ea7 100644 --- a/lib/BUILD.bazel +++ b/lib/BUILD.bazel
@@ -101,9 +101,9 @@ ) bzl_library( - name = "copy_to_bin", - srcs = ["copy_to_bin.bzl"], - deps = ["//lib/private/docs:copy_to_bin"], + name = "output_filegroup", + srcs = ["output_filegroup.bzl"], + deps = ["//lib/private/docs:output_filegroup"], ) bzl_library(
diff --git a/lib/copy_to_bin.bzl b/lib/copy_to_bin.bzl index 066c875..25f134d 100644 --- a/lib/copy_to_bin.bzl +++ b/lib/copy_to_bin.bzl
@@ -8,12 +8,13 @@ """ load( - "//lib/private:copy_to_bin.bzl", - _copy_file_to_bin_action = "copy_file_to_bin_action", - _copy_files_to_bin_actions = "copy_files_to_bin_actions", - _copy_to_bin = "copy_to_bin", + "//lib/private:output_filegroup.bzl", + _output_file_action = "output_file_action", + _output_filegroup = "output_filegroup", + _output_files_actions = "output_files_actions", ) -copy_file_to_bin_action = _copy_file_to_bin_action -copy_files_to_bin_actions = _copy_files_to_bin_actions -copy_to_bin = _copy_to_bin +# TODO(2.0): remove these legacy names +copy_file_to_bin_action = _output_file_action +copy_files_to_bin_actions = _output_files_actions +copy_to_bin = _output_filegroup
diff --git a/lib/output_filegroup.bzl b/lib/output_filegroup.bzl new file mode 100644 index 0000000..41ca8c6 --- /dev/null +++ b/lib/output_filegroup.bzl
@@ -0,0 +1,19 @@ +"""A rule that copies source files to the output tree. + +This rule uses a Bash command (diff) on Linux/macOS/non-Windows, and a cmd.exe +command (fc.exe) on Windows (no Bash is required). + +Originally authored in rules_nodejs +https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabea4856f8e/internal/common/copy_to_bin.bzl +""" + +load( + "//lib/private:output_filegroup.bzl", + _output_file_action = "output_file_action", + _output_filegroup = "output_filegroup", + _output_files_actions = "output_files_actions", +) + +output_file_action = _output_file_action +output_files_actions = _output_files_actions +output_filegroup = _output_filegroup
diff --git a/lib/private/docs/BUILD.bazel b/lib/private/docs/BUILD.bazel index a91d858..a579299 100644 --- a/lib/private/docs/BUILD.bazel +++ b/lib/private/docs/BUILD.bazel
@@ -58,8 +58,8 @@ ) bzl_library( - name = "copy_to_bin", - srcs = ["//lib/private:copy_to_bin.bzl"], + name = "output_filegroup", + srcs = ["//lib/private:output_filegroup.bzl"], deps = [ ":copy_file", "@bazel_skylib//lib:paths",
diff --git a/lib/private/copy_to_bin.bzl b/lib/private/output_filegroup.bzl similarity index 83% rename from lib/private/copy_to_bin.bzl rename to lib/private/output_filegroup.bzl index 58bfb4f..11f9079 100644 --- a/lib/private/copy_to_bin.bzl +++ b/lib/private/output_filegroup.bzl
@@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Implementation of copy_to_bin macro and underlying rules.""" +"""Implementation of output_filegroup macro and underlying rules.""" load("@bazel_skylib//lib:paths.bzl", "paths") load(":copy_file.bzl", "copy_file_action") -def copy_file_to_bin_action(ctx, file, is_windows = None): +def output_file_action(ctx, file, is_windows = None): """Helper function that creates an action to copy a file to the output tree. File are copied to the same workspace-relative path. The resulting files is @@ -65,7 +65,7 @@ def _file_in_external_repo_error_msg(file): return """ -Cannot use copy_to_bin to copy {file_basename} from the external repository @{repository}. +Cannot use output_filegroup to copy {file_basename} from the external repository @{repository}. Files can only be copied from the source tree to their short path equivalent in the output tree. """.format( file_basename = file.basename, @@ -73,15 +73,16 @@ ) def _file_in_different_package_error_msg(file, curr_package_label): + # TODO: fix bug when file is in a subfolder of a package the suggested target is incorrect return """ Expected to find file {file_basename} in {package}, but instead it is in {file_package}. -To use copy_to_bin, either move {file_basename} to {package}, or move the copy_to_bin +To use output_filegroup, either move {file_basename} to {package}, or move the output_filegroup target to {file_package} using: - buildozer 'new copy_to_bin {target_name}' {file_package}:__pkg__ + buildozer 'new output_filegroup {target_name}' {file_package}:__pkg__ buildozer 'add srcs {file_basename}' {file_package}:{target_name} - buildozer 'new_load @aspect_bazel_lib//lib:copy_to_bin.bzl copy_to_bin' {file_package}:__pkg__ + buildozer 'new_load @aspect_bazel_lib//lib:output_filegroup.bzl output_filegroup' {file_package}:__pkg__ buildozer 'add visibility {package}:__subpackages__' {file_package}:{target_name} """.format( @@ -91,7 +92,7 @@ package = "%s//%s" % (curr_package_label.workspace_name, curr_package_label.package), ) -def copy_files_to_bin_actions(ctx, files, is_windows = None): +def output_files_actions(ctx, files, is_windows = None): """Helper function that creates actions to copy files to the output tree. Files are copied to the same workspace-relative path. The resulting list of @@ -110,24 +111,24 @@ """ # TODO(2.0): remove deprecated & unused is_windows parameter - return [copy_file_to_bin_action(ctx, file, is_windows = is_windows) for file in files] + return [output_file_action(ctx, file, is_windows = is_windows) for file in files] -def _copy_to_bin_impl(ctx): - files = copy_files_to_bin_actions(ctx, ctx.files.srcs) +def _output_filegroup_impl(ctx): + files = output_files_actions(ctx, ctx.files.srcs) return DefaultInfo( files = depset(files), runfiles = ctx.runfiles(files = files), ) -_copy_to_bin = rule( - implementation = _copy_to_bin_impl, +_output_filegroup = rule( + implementation = _output_filegroup_impl, provides = [DefaultInfo], attrs = { "srcs": attr.label_list(mandatory = True, allow_files = True), }, ) -def copy_to_bin(name, srcs, **kwargs): +def output_filegroup(name, srcs, **kwargs): """Copies a source file to output tree at the same workspace-relative path. e.g. `<execroot>/path/to/file -> <execroot>/bazel-out/<platform>/bin/path/to/file` @@ -147,7 +148,7 @@ srcs: A list of labels. File(s) to copy. **kwargs: further keyword arguments, e.g. `visibility` """ - _copy_to_bin( + _output_filegroup( name = name, srcs = srcs, **kwargs
diff --git a/lib/tests/copy_to_directory_action/BUILD.bazel b/lib/tests/copy_to_directory_action/BUILD.bazel index c5f215c..f34ec35 100644 --- a/lib/tests/copy_to_directory_action/BUILD.bazel +++ b/lib/tests/copy_to_directory_action/BUILD.bazel
@@ -1,11 +1,11 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("//lib:diff_test.bzl", "diff_test") load("//lib:copy_to_directory.bzl", "copy_to_directory") -load("//lib:copy_to_bin.bzl", "copy_to_bin") +load("//lib:output_filegroup.bzl", "output_filegroup") load(":lib.bzl", "lib") load(":pkg.bzl", "pkg") -copy_to_bin( +output_filegroup( name = "copy_1", srcs = ["1"], ) @@ -16,7 +16,7 @@ # intentionally dup on "1" to make sure it is gracefully handled others = [ "1", - # also pass in a copy_to_bin copy of "1" to spice things up; + # also pass in a output_filegroup copy of "1" to spice things up; # this case is handled in the fix in https://github.com/aspect-build/bazel-lib/pull/205 "copy_1", "2",
diff --git a/lib/tests/copy_to_directory_bin_action/BUILD.bazel b/lib/tests/copy_to_directory_bin_action/BUILD.bazel index e4bafab..17e78ca 100644 --- a/lib/tests/copy_to_directory_bin_action/BUILD.bazel +++ b/lib/tests/copy_to_directory_bin_action/BUILD.bazel
@@ -1,11 +1,11 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("//lib:diff_test.bzl", "diff_test") load("//lib:copy_directory.bzl", "copy_directory") -load("//lib:copy_to_bin.bzl", "copy_to_bin") +load("//lib:output_filegroup.bzl", "output_filegroup") load(":lib.bzl", "lib") load(":pkg.bzl", "pkg") -copy_to_bin( +output_filegroup( name = "copy_1", srcs = ["1"], ) @@ -24,7 +24,7 @@ # intentionally dup on "1" to make sure it is gracefully handled others = [ "1", - # also pass in a copy_to_bin copy of "1" to spice things up; + # also pass in a output_filegroup copy of "1" to spice things up; # this case is handled in the fix in https://github.com/aspect-build/bazel-lib/pull/205 "copy_1", "2",
diff --git a/lib/tests/jq/BUILD.bazel b/lib/tests/jq/BUILD.bazel index d2d5fa2..08b22e5 100644 --- a/lib/tests/jq/BUILD.bazel +++ b/lib/tests/jq/BUILD.bazel
@@ -161,7 +161,7 @@ file2 = "raw_expected.json", ) -# Similar to copy_to_bin, this will create a file a.json in the output folder. +# Similar to output_filegroup, this will create a file a.json in the output folder. # That's the same path as an input file, but that's okay as long as the jq # rule doesn't pre-declare that output. jq(
diff --git a/lib/tests/copy_to_bin/BUILD.bazel b/lib/tests/output_filegroup/BUILD.bazel similarity index 81% rename from lib/tests/copy_to_bin/BUILD.bazel rename to lib/tests/output_filegroup/BUILD.bazel index 567f862..2c8da2d 100644 --- a/lib/tests/copy_to_bin/BUILD.bazel +++ b/lib/tests/output_filegroup/BUILD.bazel
@@ -1,9 +1,9 @@ -"tests for copy_to_bin" +"tests for output_filegroup" -load("//lib:copy_to_bin.bzl", "copy_to_bin") +load("//lib:output_filegroup.bzl", "output_filegroup") load("//lib:output_files.bzl", "output_files") -copy_to_bin( +output_filegroup( name = "copy", srcs = [ "file1", @@ -28,7 +28,7 @@ timeout = "short", srcs = ["test.sh"], args = [ - "lib/tests/copy_to_bin/file1", + "lib/tests/output_filegroup/file1", "$(execpath file1)", "$(execpath :pull_out_file1)", ], @@ -43,7 +43,7 @@ timeout = "short", srcs = ["test.sh"], args = [ - "lib/tests/copy_to_bin/file2", + "lib/tests/output_filegroup/file2", "$(execpath file2)", "$(execpath :pull_out_file2)", ], @@ -54,14 +54,14 @@ ) # Case: two different targets copying same file to bin -copy_to_bin( +output_filegroup( name = "copy_same_file_1", srcs = [ "file3", ], ) -copy_to_bin( +output_filegroup( name = "copy_same_file_2", srcs = [ "file3",
diff --git a/lib/tests/copy_to_bin/file1 b/lib/tests/output_filegroup/file1 similarity index 100% rename from lib/tests/copy_to_bin/file1 rename to lib/tests/output_filegroup/file1
diff --git a/lib/tests/copy_to_bin/file2 b/lib/tests/output_filegroup/file2 similarity index 100% rename from lib/tests/copy_to_bin/file2 rename to lib/tests/output_filegroup/file2
diff --git a/lib/tests/copy_to_bin/file3 b/lib/tests/output_filegroup/file3 similarity index 100% rename from lib/tests/copy_to_bin/file3 rename to lib/tests/output_filegroup/file3
diff --git a/lib/tests/copy_to_bin/test.sh b/lib/tests/output_filegroup/test.sh similarity index 100% rename from lib/tests/copy_to_bin/test.sh rename to lib/tests/output_filegroup/test.sh
diff --git a/lib/tests/utils_test.bzl b/lib/tests/utils_test.bzl index 0f4c808..b4e9d5c 100644 --- a/lib/tests/utils_test.bzl +++ b/lib/tests/utils_test.bzl
@@ -103,8 +103,8 @@ fail("does-not-exist does not exist") if not utils.file_exists("utils_test.bzl"): fail("utils_test.bzl does exist") - if (utils.file_exists("copy_to_bin")): - fail("copy_to_bin exists, but is a directory") + if (utils.file_exists("output_filegroup")): + fail("output_filegroup exists, but is a directory") # buildifier: disable=function-docstring def utils_test_suite():