feat: add include_prefixes to copy_to_directory (#193)

This commit is contained in:
Greg Magolan 2022-07-27 15:15:42 -07:00 committed by GitHub
parent 998ee22369
commit 61c7cc209b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 145 additions and 22 deletions

View File

@ -11,8 +11,8 @@ the `root_paths`, `exclude_prefixes` and `replace_prefixes` attributes.
## copy_to_directory
<pre>
copy_to_directory(<a href="#copy_to_directory-name">name</a>, <a href="#copy_to_directory-allow_overwrites">allow_overwrites</a>, <a href="#copy_to_directory-exclude_prefixes">exclude_prefixes</a>, <a href="#copy_to_directory-include_external_repositories">include_external_repositories</a>, <a href="#copy_to_directory-out">out</a>,
<a href="#copy_to_directory-replace_prefixes">replace_prefixes</a>, <a href="#copy_to_directory-root_paths">root_paths</a>, <a href="#copy_to_directory-srcs">srcs</a>)
copy_to_directory(<a href="#copy_to_directory-name">name</a>, <a href="#copy_to_directory-allow_overwrites">allow_overwrites</a>, <a href="#copy_to_directory-exclude_prefixes">exclude_prefixes</a>, <a href="#copy_to_directory-include_external_repositories">include_external_repositories</a>,
<a href="#copy_to_directory-include_prefixes">include_prefixes</a>, <a href="#copy_to_directory-out">out</a>, <a href="#copy_to_directory-replace_prefixes">replace_prefixes</a>, <a href="#copy_to_directory-root_paths">root_paths</a>, <a href="#copy_to_directory-srcs">srcs</a>)
</pre>
@ -24,11 +24,12 @@ copy_to_directory(<a href="#copy_to_directory-name">name</a>, <a href="#copy_to_
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="copy_to_directory-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="copy_to_directory-allow_overwrites"></a>allow_overwrites | If True, allow files to be overwritten if the same output file is copied to twice.<br><br> If set, then the order of srcs matters as the last copy of a particular file will win.<br><br> This setting has no effect on Windows where overwrites are always allowed. | Boolean | optional | False |
| <a id="copy_to_directory-exclude_prefixes"></a>exclude_prefixes | List of path prefixes to exclude from output directory.<br><br> If the output directory path for a file or directory starts with or is equal to a path in the list then that file is not copied to the output directory.<br><br> Exclude prefixes are matched *before* replace_prefixes are applied. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_prefixes"></a>exclude_prefixes | List of path prefixes to exclude from output directory.<br><br> If the output directory path for a file or directory starts with or is equal to a path in the list then that file is not copied to the output directory.<br><br> Forward slashes (<code>/</code>) should be used as path separators. The final path segment of the key can be a partial match in the corresponding segment of the output directory path.<br><br> <code>exclude_prefixes</code> are matched on the output path after <code>root_paths</code> are considered.<br><br> <code>exclude_prefixes</code> are matched *after* <code>include_prefixes</code> and *before* <code>replace_prefixes</code> are applied.<br><br> NB: Prefixes that nest into source directories or generated directories (TreeArtifacts) targets are not supported since matches are performed in Starlark. To use <code>exclude_prefixes</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. | List of strings | optional | [] |
| <a id="copy_to_directory-include_external_repositories"></a>include_external_repositories | List of external repository names to include in the output directory.<br><br> Files from external repositories are not copied into the output directory unless the external repository they come from is listed here.<br><br> When copied from an external repository, the file path in the output directory defaults to the file's path within the external repository. The external repository name is _not_ included in that path.<br><br> For example, the following copies <code>@external_repo//path/to:file</code> to <code>path/to/file</code> within the output directory.<br><br> <pre><code> copy_to_directory( name = "dir", include_external_repositories = ["external_repo"], srcs = ["@external_repo//path/to:file"], ) </code></pre><br><br> Files from external repositories are subject to <code>root_paths</code>, <code>exclude_prefixes</code> and <code>replace_prefixes</code> in the same way as files form the main repository. | List of strings | optional | [] |
| <a id="copy_to_directory-include_prefixes"></a>include_prefixes | List of path prefixes to include in output directory.<br><br> If empty then all paths are included and subject to <code>exclude_prefixes</code> and <code>replace_prefixes</code> which are matched *after* <code>include_prefixes</code>.<br><br> If not empty, a file is only copied to the output directory if the output directory path for a file or directory starts with or is equal to a path in the list.<br><br> Forward slashes (<code>/</code>) should be used as path separators. The final path segment of the key can be a partial match in the corresponding segment of the output directory path.<br><br> <code>include_prefixes</code> are matched on the output path after <code>root_paths</code> are considered.<br><br> <code>include_prefixes</code> are matched *before* <code>exclude_prefixes</code> and <code>replace_prefixes</code> are applied.<br><br> NB: Prefixes that nest into source directories or generated directories (TreeArtifacts) targets are not supported since matches are performed in Starlark. To use <code>include_prefixes</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. | List of strings | optional | [] |
| <a id="copy_to_directory-out"></a>out | Path of the output directory, relative to this package.<br><br> If not set, the name of the target is used. | String | optional | "" |
| <a id="copy_to_directory-replace_prefixes"></a>replace_prefixes | Map of paths prefixes to replace in the output directory path when copying files.<br><br> If the output directory path for a file or directory starts with or is equal to a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key.<br><br> Forward slashes (<code>/</code>) should be used as path separators. The final path segment of the key can be a partial match in the corresponding segment of the output directory path.<br><br> If there are multiple keys that match, the longest match wins. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="copy_to_directory-root_paths"></a>root_paths | List of paths that are roots in the output directory.<br><br> "." values indicate the targets package path.<br><br> If a file or directory being copied is in one of the listed paths or one of its subpaths, the output directory path is the path relative to the root path instead of the path relative to the file's workspace.<br><br> Forward slashes (<code>/</code>) should be used as path separators. Partial matches on the final path segment of a root path against the corresponding segment in the full workspace relative path of a file are not matched.<br><br> If there are multiple root paths that match, the longest match wins.<br><br> Defaults to [package_name()] so that the output directory path of files in the target's package and and sub-packages are relative to the target's package and files outside of that retain their full workspace relative paths. | List of strings | optional | ["."] |
| <a id="copy_to_directory-replace_prefixes"></a>replace_prefixes | Map of paths prefixes to replace in the output directory path when copying files.<br><br> If the output directory path for a file or directory starts with or is equal to a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key.<br><br> Forward slashes (<code>/</code>) should be used as path separators. The final path segment of the key can be a partial match in the corresponding segment of the output directory path.<br><br> <code>replace_prefixes</code> are matched on the output path after <code>root_paths</code> are considered.<br><br> <code>replace_prefixes</code> are matched *after* <code>include_prefixes</code> and <code>exclude_prefixes</code> are applied.<br><br> If there are multiple keys that match, the longest match wins.<br><br> NB: Prefixes that nest into source directories or generated directories (TreeArtifacts) targets are not supported since matches are performed in Starlark. To use <code>replace_prefixes</code> on files within directories you can use the <code>make_directory_paths</code> helper to specify individual files inside directories in <code>srcs</code>. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="copy_to_directory-root_paths"></a>root_paths | List of paths that are roots in the output directory.<br><br> "." values indicate the target's package path.<br><br> If a file or directory being copied is in one of the listed paths or one of its subpaths, the output directory path is the path relative to the root path instead of the path relative to the file's workspace.<br><br> Forward slashes (<code>/</code>) should be used as path separators. Partial matches on the final path segment of a root path against the corresponding segment in the full workspace relative path of a file are not matched.<br><br> If there are multiple root paths that match, the longest match wins.<br><br> Defaults to ["."] so that the output directory path of files in the target's package and and sub-packages are relative to the target's package and files outside of that retain their full workspace relative paths. | List of strings | optional | ["."] |
| <a id="copy_to_directory-srcs"></a>srcs | Files and/or directories or targets that provide DirectoryPathInfo to copy into the output directory. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
@ -38,8 +39,8 @@ copy_to_directory(<a href="#copy_to_directory-name">name</a>, <a href="#copy_to_
<pre>
copy_to_directory_action(<a href="#copy_to_directory_action-ctx">ctx</a>, <a href="#copy_to_directory_action-srcs">srcs</a>, <a href="#copy_to_directory_action-dst">dst</a>, <a href="#copy_to_directory_action-additional_files">additional_files</a>, <a href="#copy_to_directory_action-root_paths">root_paths</a>,
<a href="#copy_to_directory_action-include_external_repositories">include_external_repositories</a>, <a href="#copy_to_directory_action-exclude_prefixes">exclude_prefixes</a>, <a href="#copy_to_directory_action-replace_prefixes">replace_prefixes</a>,
<a href="#copy_to_directory_action-allow_overwrites">allow_overwrites</a>, <a href="#copy_to_directory_action-is_windows">is_windows</a>)
<a href="#copy_to_directory_action-include_external_repositories">include_external_repositories</a>, <a href="#copy_to_directory_action-include_prefixes">include_prefixes</a>, <a href="#copy_to_directory_action-exclude_prefixes">exclude_prefixes</a>,
<a href="#copy_to_directory_action-replace_prefixes">replace_prefixes</a>, <a href="#copy_to_directory_action-allow_overwrites">allow_overwrites</a>, <a href="#copy_to_directory_action-is_windows">is_windows</a>)
</pre>
Helper function to copy files to a directory.
@ -59,6 +60,7 @@ other rule implementations where additional_files can also be passed in.
| <a id="copy_to_directory_action-additional_files"></a>additional_files | Additional files to copy that are not in the DefaultInfo or DirectoryPathInfo of srcs | <code>[]</code> |
| <a id="copy_to_directory_action-root_paths"></a>root_paths | List of paths that are roots in the output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>["."]</code> |
| <a id="copy_to_directory_action-include_external_repositories"></a>include_external_repositories | List of external repository names to include in the output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>[]</code> |
| <a id="copy_to_directory_action-include_prefixes"></a>include_prefixes | List of path prefixes to include in output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>[]</code> |
| <a id="copy_to_directory_action-exclude_prefixes"></a>exclude_prefixes | List of path prefixes to exclude from output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>[]</code> |
| <a id="copy_to_directory_action-replace_prefixes"></a>replace_prefixes | Map of paths prefixes to replace in the output directory path when copying files.<br><br>See copy_to_directory rule documentation for more details. | <code>{}</code> |
| <a id="copy_to_directory_action-allow_overwrites"></a>allow_overwrites | If True, allow files to be overwritten if the same output file is copied to twice.<br><br>See copy_to_directory rule documentation for more details. | <code>False</code> |

View File

@ -22,7 +22,7 @@ _copy_to_directory_attr = {
default = ["."],
doc = """List of paths that are roots in the output directory.
"." values indicate the targets package path.
"." values indicate the target's package path.
If a file or directory being copied is in one of the listed paths or one of its subpaths,
the output directory path is the path relative to the root path instead of the path
@ -34,7 +34,7 @@ _copy_to_directory_attr = {
If there are multiple root paths that match, the longest match wins.
Defaults to [package_name()] so that the output directory path of files in the
Defaults to ["."] so that the output directory path of files in the
target's package and and sub-packages are relative to the target's package and
files outside of that retain their full workspace relative paths.""",
),
@ -62,13 +62,47 @@ _copy_to_directory_attr = {
Files from external repositories are subject to `root_paths`, `exclude_prefixes`
and `replace_prefixes` in the same way as files form the main repository.""",
),
"include_prefixes": attr.string_list(
doc = """List of path prefixes to include in output directory.
If empty then all paths are included and subject to `exclude_prefixes` and `replace_prefixes`
which are matched *after* `include_prefixes`.
If not empty, a file is only copied to the output directory if
the output directory path for a file or directory starts with or is equal to
a path in the list.
Forward slashes (`/`) should be used as path separators. The final path segment
of the key can be a partial match in the corresponding segment of the output
directory path.
`include_prefixes` are matched on the output path after `root_paths` are considered.
`include_prefixes` are matched *before* `exclude_prefixes` and `replace_prefixes` are applied.
NB: Prefixes that nest into source directories or generated directories (TreeArtifacts) targets
are not supported since matches are performed in Starlark. To use `include_prefixes` on files
within directories you can use the `make_directory_paths` helper to specify individual files inside
directories in `srcs`.""",
),
"exclude_prefixes": attr.string_list(
doc = """List of path prefixes to exclude from output directory.
If the output directory path for a file or directory starts with or is equal to
a path in the list then that file is not copied to the output directory.
Exclude prefixes are matched *before* replace_prefixes are applied.""",
Forward slashes (`/`) should be used as path separators. The final path segment
of the key can be a partial match in the corresponding segment of the output
directory path.
`exclude_prefixes` are matched on the output path after `root_paths` are considered.
`exclude_prefixes` are matched *after* `include_prefixes` and *before* `replace_prefixes` are applied.
NB: Prefixes that nest into source directories or generated directories (TreeArtifacts) targets
are not supported since matches are performed in Starlark. To use `exclude_prefixes` on files
within directories you can use the `make_directory_paths` helper to specify individual files inside
directories in `srcs`.""",
),
"replace_prefixes": attr.string_dict(
doc = """Map of paths prefixes to replace in the output directory path when copying files.
@ -81,7 +115,16 @@ _copy_to_directory_attr = {
of the key can be a partial match in the corresponding segment of the output
directory path.
If there are multiple keys that match, the longest match wins.""",
`replace_prefixes` are matched on the output path after `root_paths` are considered.
`replace_prefixes` are matched *after* `include_prefixes` and `exclude_prefixes` are applied.
If there are multiple keys that match, the longest match wins.
NB: Prefixes that nest into source directories or generated directories (TreeArtifacts) targets
are not supported since matches are performed in Starlark. To use `replace_prefixes` on files
within directories you can use the `make_directory_paths` helper to specify individual files inside
directories in `srcs`.""",
),
"allow_overwrites": attr.bool(
doc = """If True, allow files to be overwritten if the same output file is copied to twice.
@ -93,11 +136,18 @@ _copy_to_directory_attr = {
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}
def _first_match(subject, tests, allow_partial = False):
for test in tests:
starts_with_test = test if allow_partial or test.endswith("/") else test + "/"
if subject == test or subject.startswith(starts_with_test):
return test
return None
def _longest_match(subject, tests, allow_partial = False):
match = None
high_score = 0
for test in tests:
starts_with_test = test if allow_partial else test + "/"
starts_with_test = test if allow_partial or test.endswith("/") else test + "/"
if subject == test or subject.startswith(starts_with_test):
score = len(test)
if score > high_score:
@ -110,6 +160,7 @@ def _copy_paths(
src,
root_paths,
include_external_repositories,
include_prefixes,
exclude_prefixes,
replace_prefixes):
if type(src) == "File":
@ -134,8 +185,15 @@ def _copy_paths(
strip_depth = len(root_path.split("/"))
output_path = "/".join(output_path.split("/")[strip_depth:])
if include_prefixes:
# check if this file matches an include_prefix if they are specified
match = _first_match(output_path, include_prefixes, True)
if not match:
# file is excluded as it does not match any specified include_prefix
return None, None, None
# check if this file matches an exclude_prefix
match = _longest_match(output_path, exclude_prefixes, True)
match = _first_match(output_path, exclude_prefixes, True)
if match:
# file is excluded due to match in exclude_prefix
return None, None, None
@ -293,6 +351,7 @@ def _copy_to_directory_impl(ctx):
dst = dst,
root_paths = ctx.attr.root_paths,
include_external_repositories = ctx.attr.include_external_repositories,
include_prefixes = ctx.attr.include_prefixes,
exclude_prefixes = ctx.attr.exclude_prefixes,
replace_prefixes = ctx.attr.replace_prefixes,
allow_overwrites = ctx.attr.allow_overwrites,
@ -313,6 +372,7 @@ def copy_to_directory_action(
additional_files = [],
root_paths = ["."],
include_external_repositories = [],
include_prefixes = [],
exclude_prefixes = [],
replace_prefixes = {},
allow_overwrites = False,
@ -339,6 +399,10 @@ def copy_to_directory_action(
See copy_to_directory rule documentation for more details.
include_prefixes: List of path prefixes to include in output directory.
See copy_to_directory rule documentation for more details.
exclude_prefixes: List of path prefixes to exclude from output directory.
See copy_to_directory rule documentation for more details.
@ -367,6 +431,7 @@ def copy_to_directory_action(
src = src,
root_paths = root_paths,
include_external_repositories = include_external_repositories,
include_prefixes = include_prefixes,
exclude_prefixes = exclude_prefixes,
replace_prefixes = replace_prefixes,
)
@ -379,6 +444,7 @@ def copy_to_directory_action(
src = src_file,
root_paths = root_paths,
include_external_repositories = include_external_repositories,
include_prefixes = include_prefixes,
exclude_prefixes = exclude_prefixes,
replace_prefixes = replace_prefixes,
)
@ -390,6 +456,7 @@ def copy_to_directory_action(
src = additional_file,
root_paths = root_paths,
include_external_repositories = include_external_repositories,
include_prefixes = include_prefixes,
exclude_prefixes = exclude_prefixes,
replace_prefixes = replace_prefixes,
)

View File

@ -23,6 +23,7 @@ load("//lib:directory_path.bzl", "make_directory_paths")
"expected_8",
"expected_9",
"expected_10",
"expected_13_14",
]
]
@ -31,9 +32,9 @@ case_srcs = [
":b",
":c",
":d",
":e/e",
":e/e1",
":e/e2",
"//lib/tests/copy_to_directory/f/f2:f",
"//lib/tests/copy_to_directory/f/f2:f1",
"//lib/tests/copy_to_directory/f/f2:f2",
"@external_test_repo//:test_a",
"@external_test_repo//:test_b",
@ -68,7 +69,7 @@ copy_to_directory(
# put e into new/e except for e/e2 which goes into new/e2
"e": "new/e",
"e/e2": "new/e2/e2",
# f/f2/f => new/ff and f/f2/f2 => new/f2/f2
# f/f2/f1 => new/ff1 and f/f2/f2 => new/f2/f2
"f/f2/": "////new/////f",
"f/f2/f2": "////new/////f2///f2",
# flatten test_a & test_b to the root
@ -183,10 +184,10 @@ copy_to_directory(
srcs = [
":c",
":d",
":e/e",
":e/e1",
":e/e2",
"//lib/tests/copy_to_directory/f/f2",
"//lib/tests/copy_to_directory/f/f2:f",
"//lib/tests/copy_to_directory/f/f2:f1",
"//lib/tests/copy_to_directory/f/f2:f2",
"@external_test_repo//:test_c",
"@external_test_repo//:test_d",
] + make_directory_paths(
@ -211,7 +212,7 @@ copy_to_directory(
# put e into new/e except for e/e2 which goes into new/e2
"e": "new/e",
"e/e2": "new/e2/e2",
# f/f2/f => new/ff and f/f2/f2 => new/f2/f2
# f/f2/f1 => new/ff1 and f/f2/f2 => new/f2/f2
"f/f2/": "////new/////f",
"f/f2/f2": "////new/////f2///f2",
# flatten test_a & test_b to the root
@ -300,3 +301,49 @@ copy_to_directory(
"{}/g1'_out".format(package_name()),
],
)
# Case 13: include prefixes
copy_to_directory(
name = "case_13",
srcs = case_srcs,
include_prefixes = [
# include a & b
"a",
"b",
# this partial match should include all of e
"e/e",
# this full match should include just f/f2/f2
"f/f2/f2",
],
)
diff_test(
name = "case_13_test",
file1 = "case_13",
file2 = ":expected_13_14",
)
# Case 14: include & exclude prefixes
copy_to_directory(
name = "case_14",
srcs = case_srcs,
exclude_prefixes = [
# this full match should exclude just f/f2/f1
"f/f2/f1",
],
include_prefixes = [
# include a & b
"a",
"b",
# this partial match should include all of e
"e/e",
# include all of f/f2
"f/f2",
],
)
diff_test(
name = "case_14_test",
file1 = "case_14",
file2 = ":expected_13_14",
)

View File

@ -8,7 +8,7 @@ cd $(dirname $TEST_BINARY)
cd case_6
# A path that should be in the directory we have in data[]
path="f/f2/f"
path="f/f2/f2"
if [ ! -f "$path" ]; then
echo >&2 "Expected $path to exist in runfiles"
exit 1

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -1,4 +1,4 @@
exports_files([
"f",
"f1",
"f2",
])

View File

@ -0,0 +1 @@
foobar