feat: improved copy_to_directory performance & globbing support using copy_to_directory_bin_action (#311)

This commit is contained in:
Greg Magolan 2023-01-07 11:26:27 -08:00 committed by GitHub
parent b13fd20bee
commit fb3ef6e45d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 446 additions and 178 deletions

View File

@ -13,7 +13,7 @@ the `root_paths`, `include_srcs_patters`, `exclude_srcs_patters` and `replace_pr
<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-exclude_srcs_packages">exclude_srcs_packages</a>,
<a href="#copy_to_directory-exclude_srcs_patterns">exclude_srcs_patterns</a>, <a href="#copy_to_directory-include_external_repositories">include_external_repositories</a>, <a href="#copy_to_directory-include_srcs_packages">include_srcs_packages</a>,
<a href="#copy_to_directory-include_srcs_patterns">include_srcs_patterns</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>)
<a href="#copy_to_directory-include_srcs_patterns">include_srcs_patterns</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>, <a href="#copy_to_directory-verbose">verbose</a>)
</pre>
@ -25,16 +25,17 @@ 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 (with glob support) to exclude from output directory.<br><br> DEPRECATED: use <code>exclude_srcs_patterns</code> instead<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if their output directory path, after applying <code>root_paths</code>, starts with or fully matches one of the patterns specified.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>include_srcs_patterns</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>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Files and directories that do not have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_packages"></a>exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A <code>"."</code> value means exclude srcs that are in the target's package. It expands to the target's package path (<code>ctx.label.package</code>). This will be an empty string <code>""</code> if the target is in the root package.<br><br> A <code>"./**"</code> value means exclude srcs that are in subpackages of the target's package. It expands to the target's package path followed by a slash and a globstar (<code>"{}/**".format(ctx.label.package)</code>). If the target's package is the root package, <code>"./**"</code> expands to <code>["?*", "?*/**"]</code> instead.<br><br> Files and directories that have do not have matching Bazel packages are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_patterns"></a>exclude_srcs_patterns | List of paths (with glob support) to exclude from output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are not copied to the output directory if their output directory path, after applying <code>root_paths</code>, matches one of the patterns specified.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>include_srcs_patterns</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>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Files and directories that do not have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-include_external_repositories"></a>include_external_repositories | List of external repository names (with glob support) to include in the output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files from external repositories are only copied into the output directory if the external repository they come from matches one of the external repository patterns specified.<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_*"], srcs = ["@external_repo//path/to:file"], ) </code></pre><br><br> Files and directories that come from matching external are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be. The external repository name of the file or directory from an external repository is not included in the output directory path and is considered in subsequent filters and transformations.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-include_srcs_packages"></a>include_srcs_packages | List of Bazel packages (with glob support) to include in output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are only copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A <code>"."</code> value means include srcs that are in the target's package. It expands to the target's package path (<code>ctx.label.package</code>). This will be an empty string <code>""</code> if the target is in the root package.<br><br> A <code>"./**"</code> value means include srcs that are in subpackages of the target's package. It expands to the target's package path followed by a slash and a globstar (<code>"{}/**".format(ctx.label.package)</code>). If the target's package is the root package, <code>"./**"</code> expands to <code>["?*", "?*/**"]</code> instead.<br><br> Defaults to <code>["**"]</code> which includes sources from all packages.<br><br> Files and directories that have matching Bazel packages are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] |
| <a id="copy_to_directory-include_srcs_patterns"></a>include_srcs_patterns | List of paths (with glob support) to include in output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> Files and directories in srcs are only copied to the output directory if their output directory path, after applying <code>root_paths</code>, matches one of the patterns specified.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use <code>include_srcs_patterns</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>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Defaults to ["**"] which includes all sources.<br><br> Files and directories that have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] |
| <a id="copy_to_directory-exclude_prefixes"></a>exclude_prefixes | List of path prefixes (with glob support) to exclude from output directory.<br><br> DEPRECATED: use <code>exclude_srcs_patterns</code> instead<br><br> Files in srcs are not copied to the output directory if their output directory path, after applying <code>root_paths</code>, starts with or fully matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Files that do not have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_packages"></a>exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.<br><br> Files in srcs are not copied to the output directory if the Bazel package of the file matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators. A first character of <code>"."</code> will be replaced by the target's package path.<br><br> Files that have do not have matching Bazel packages are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-exclude_srcs_patterns"></a>exclude_srcs_patterns | List of paths (with glob support) to exclude from output directory.<br><br> Files in srcs are not copied to the output directory if their output directory path, after applying <code>root_paths</code>, matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Files that do not have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-include_external_repositories"></a>include_external_repositories | List of external repository names (with glob support) to include in the output directory.<br><br> Files from external repositories are only copied into the output directory if the external repository they come from matches one of the external repository patterns specified.<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_*"], srcs = ["@external_repo//path/to:file"], ) </code></pre><br><br> Files that come from matching external are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be. The external repository name of the file from an external repository is not included in the output directory path and is considered in subsequent filters and transformations.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | [] |
| <a id="copy_to_directory-include_srcs_packages"></a>include_srcs_packages | List of Bazel packages (with glob support) to include in output directory.<br><br> Files in srcs are only copied to the output directory if the Bazel package of the file matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators. A first character of <code>"."</code> will be replaced by the target's package path.<br><br> Defaults to <code>["**"]</code> which includes sources from all packages.<br><br> Files that have matching Bazel packages are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["**"] |
| <a id="copy_to_directory-include_srcs_patterns"></a>include_srcs_patterns | List of paths (with glob support) to include in output directory.<br><br> Files in srcs are only copied to the output directory if their output directory path, after applying <code>root_paths</code>, matches one of the patterns specified.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> Defaults to ["**"] which includes all sources.<br><br> Files that have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | 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 (with glob support) to replace in the output directory path when copying files.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported but the pattern must not end with a <code>**</code> glob expression. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> If the output directory path for a file or directory starts with or fully matches a a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key. The final path segment matched can be a partial match of that segment and only the matching portion will be replaced. If there are multiple keys that match, the longest match wins.<br><br> Patterns do not look into files within source directory or generated directory (TreeArtifact) targets 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>. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.<br><br> Forward slashes (<code>/</code>) should be used as path separators. <br><br> Replace prefix transformation are the final step in the list of filters and transformations. The final output path of a file or directory being copied into the output directory is determined at this step.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | <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 (with glob support) that are roots in the output directory.<br><br> Glob patterns <code>**</code>, <code>*</code> and <code>?</code> are supported. See <code>glob_match</code> documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.<br><br> If any parent directory of a file or directory being copied matches one of the root paths patterns specified, the output directory path will be the path relative to the root path instead of the path relative to the file's or directory's workspace. If there are multiple root paths that match, the longest match wins.<br><br> Matching is done on the parent directory of the output file path so a trailing '**' glob patterm will match only up to the last path segment of the dirname and will not include the basename. Only complete path segments are matched. Partial matches on the last segment of the root path are ignored.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A "." value expands to the target's package path (<code>ctx.label.package</code>).<br><br> Defaults to ["."] which results in 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.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | List of strings | optional | ["."] |
| <a id="copy_to_directory-replace_prefixes"></a>replace_prefixes | Map of paths prefixes (with glob support) to replace in the output directory path when copying files.<br><br> If the output directory path for a file starts with or fully matches a a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key. The final path segment matched can be a partial match of that segment and only the matching portion will be replaced. If there are multiple keys that match, the longest match wins.<br><br> Forward slashes (<code>/</code>) should be used as path separators. <br><br> Replace prefix transformation are the final step in the list of filters and transformations. The final output path of a file being copied into the output directory is determined at this step.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | <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 (with glob support) that are roots in the output directory.<br><br> If any parent directory of a file being copied matches one of the root paths patterns specified, the output directory path will be the path relative to the root path instead of the path relative to the file's workspace. If there are multiple root paths that match, the longest match wins.<br><br> Matching is done on the parent directory of the output file path so a trailing '**' glob patterm will match only up to the last path segment of the dirname and will not include the basename. Only complete path segments are matched. Partial matches on the last segment of the root path are ignored.<br><br> Forward slashes (<code>/</code>) should be used as path separators.<br><br> A "." value expands to the target's package path (<code>ctx.label.package</code>).<br><br> Defaults to ["."] which results in 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.<br><br> Glob patterns are supported. Standard wildcards (globbing patterns) plus the <code>**</code> doublestar (aka. super-asterisk) are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns for more information on supported globbing patterns.<br><br> Filters and transformations are applied in the following order:<br><br>1. <code>include_external_repositories</code><br><br>2. <code>include_srcs_packages</code><br><br>3. <code>exclude_srcs_packages</code><br><br>4. <code>root_paths</code><br><br>5. <code>include_srcs_patterns</code><br><br>6. <code>exclude_srcs_patterns</code><br><br>7. <code>replace_prefixes</code><br><br>For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute. | 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 | [] |
| <a id="copy_to_directory-verbose"></a>verbose | If true, prints out verbose logs to stdout | Boolean | optional | False |
<a id="copy_to_directory_action"></a>
@ -48,7 +49,13 @@ copy_to_directory_action(<a href="#copy_to_directory_action-ctx">ctx</a>, <a hre
<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.
Legacy helper function to copy files to a directory.
This helper calculates copy paths in Starlark during analysis and performs the copies in a
bash/bat script. For improved analysis and runtime performance, it is recommended the switch
to `copy_to_directory_bin_action` which calculates copy paths and performs copies with a tool
binary, typically the `@aspect_bazel_lib//tools/copy_to_directory` `go_binary` either built
from source or provided by a toolchain.
This helper is used by copy_to_directory. It is exposed as a public API so it can be used within
other rule implementations where additional_files can also be passed in.
@ -75,6 +82,49 @@ other rule implementations where additional_files can also be passed in.
| <a id="copy_to_directory_action-is_windows"></a>is_windows | Deprecated and unused | <code>None</code> |
<a id="copy_to_directory_bin_action"></a>
## copy_to_directory_bin_action
<pre>
copy_to_directory_bin_action(<a href="#copy_to_directory_bin_action-ctx">ctx</a>, <a href="#copy_to_directory_bin_action-name">name</a>, <a href="#copy_to_directory_bin_action-dst">dst</a>, <a href="#copy_to_directory_bin_action-copy_to_directory_bin">copy_to_directory_bin</a>, <a href="#copy_to_directory_bin_action-files">files</a>, <a href="#copy_to_directory_bin_action-targets">targets</a>, <a href="#copy_to_directory_bin_action-root_paths">root_paths</a>,
<a href="#copy_to_directory_bin_action-include_external_repositories">include_external_repositories</a>, <a href="#copy_to_directory_bin_action-include_srcs_packages">include_srcs_packages</a>,
<a href="#copy_to_directory_bin_action-exclude_srcs_packages">exclude_srcs_packages</a>, <a href="#copy_to_directory_bin_action-include_srcs_patterns">include_srcs_patterns</a>, <a href="#copy_to_directory_bin_action-exclude_srcs_patterns">exclude_srcs_patterns</a>,
<a href="#copy_to_directory_bin_action-exclude_prefixes">exclude_prefixes</a>, <a href="#copy_to_directory_bin_action-replace_prefixes">replace_prefixes</a>, <a href="#copy_to_directory_bin_action-allow_overwrites">allow_overwrites</a>, <a href="#copy_to_directory_bin_action-verbose">verbose</a>)
</pre>
Helper function to copy files to a directory using a tool binary.
The tool binary will typically be the `@aspect_bazel_lib//tools/copy_to_directory` `go_binary`
either built from source or provided by a toolchain.
This helper is used by copy_to_directory. It is exposed as a public API so it can be used within
other rule implementations where additional_files can also be passed in.
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="copy_to_directory_bin_action-ctx"></a>ctx | The rule context. | none |
| <a id="copy_to_directory_bin_action-name"></a>name | Name of target creating this action used for config file generation. | none |
| <a id="copy_to_directory_bin_action-dst"></a>dst | The directory to copy to. Must be a TreeArtifact. | none |
| <a id="copy_to_directory_bin_action-copy_to_directory_bin"></a>copy_to_directory_bin | Copy to directory tool binary. | none |
| <a id="copy_to_directory_bin_action-files"></a>files | List of files to copy into the output directory. | <code>[]</code> |
| <a id="copy_to_directory_bin_action-targets"></a>targets | List of targets that provide DirectoryPathInfo to copy into the output directory. | <code>[]</code> |
| <a id="copy_to_directory_bin_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_bin_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_bin_action-include_srcs_packages"></a>include_srcs_packages | List of Bazel packages to include in output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>["**"]</code> |
| <a id="copy_to_directory_bin_action-exclude_srcs_packages"></a>exclude_srcs_packages | List of Bazel packages (with glob support) to exclude from output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>[]</code> |
| <a id="copy_to_directory_bin_action-include_srcs_patterns"></a>include_srcs_patterns | List of paths (with glob support) to include in output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>["**"]</code> |
| <a id="copy_to_directory_bin_action-exclude_srcs_patterns"></a>exclude_srcs_patterns | List of paths (with glob support) to exclude from output directory.<br><br>See copy_to_directory rule documentation for more details. | <code>[]</code> |
| <a id="copy_to_directory_bin_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_bin_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_bin_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> |
| <a id="copy_to_directory_bin_action-verbose"></a>verbose | If true, prints out verbose logs to stdout | <code>False</code> |
<a id="copy_to_directory_lib.impl"></a>
## copy_to_directory_lib.impl

View File

@ -1,7 +1,7 @@
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test")
# Test special cases of using "." and "./**" patterns in the root package
# Test special cases of using "./**" and "./*/**" patterns in the root package
copy_to_directory(
name = "dir1",
@ -10,7 +10,6 @@ copy_to_directory(
"//subpkg:b",
],
include_srcs_packages = [
".",
"./**",
],
)
@ -57,7 +56,7 @@ copy_to_directory(
"//subpkg:b",
],
exclude_srcs_packages = [
"./**",
"./*/**",
],
)
@ -84,7 +83,7 @@ copy_to_directory(
"//subpkg:b",
],
include_srcs_packages = [
"./**",
"./*/**",
],
)

View File

@ -23,6 +23,10 @@ toolchain_type(
name = "yq_toolchain_type",
)
toolchain_type(
name = "copy_to_directory_toolchain_type",
)
bzl_library(
name = "docs",
srcs = ["docs.bzl"],
@ -182,6 +186,7 @@ bzl_library(
srcs = ["repositories.bzl"],
deps = [
":utils",
"//lib/private/docs:copy_to_directory_toolchain",
"//lib/private/docs:jq_toolchain",
"//lib/private/docs:local_config_platform",
"//lib/private/docs:yq_toolchain",

View File

@ -7,19 +7,18 @@ the `root_paths`, `include_srcs_patters`, `exclude_srcs_patters` and `replace_pr
load(
"//lib/private:copy_to_directory.bzl",
_copy_to_directory_action = "copy_to_directory_action",
# TODO: export copy_to_directory_bin_action once ready
# _copy_to_directory_bin_action = "copy_to_directory_bin_action",
_copy_to_directory_bin_action = "copy_to_directory_bin_action",
_copy_to_directory_lib = "copy_to_directory_lib",
)
# export the starlark library as a public API
copy_to_directory_lib = _copy_to_directory_lib
copy_to_directory_action = _copy_to_directory_action
# TODO: export copy_to_directory_bin_action once ready
# copy_to_directory_bin_action = _copy_to_directory_bin_action
copy_to_directory_bin_action = _copy_to_directory_bin_action
copy_to_directory = rule(
implementation = _copy_to_directory_lib.impl,
provides = _copy_to_directory_lib.provides,
attrs = _copy_to_directory_lib.attrs,
toolchains = ["@aspect_bazel_lib//lib:copy_to_directory_toolchain_type"],
)

View File

@ -2,6 +2,7 @@
load(
"@aspect_bazel_lib//lib:repositories.bzl",
"register_copy_to_directory_toolchains",
"register_jq_toolchains",
"register_yq_toolchains",
)
@ -9,6 +10,7 @@ load(
def _toolchain_extension(_):
register_yq_toolchains(register = False)
register_jq_toolchains(register = False)
register_copy_to_directory_toolchains(register = False)
# TODO: some way for users to control repo name/version of the tools installed
ext = module_extension(

View File

@ -27,6 +27,12 @@ For more information each filters / transformations applied, see
the documentation for the specific filter / transformation attribute.
"""
_glob_support_docstring = """Glob patterns are supported. Standard wildcards (globbing patterns) plus the `**` doublestar (aka. super-asterisk)
are supported with the underlying globbing library, https://github.com/bmatcuk/doublestar. This is the same
globbing library used by [gazelle](https://github.com/bazelbuild/bazel-gazelle). See https://github.com/bmatcuk/doublestar#patterns
for more information on supported globbing patterns.
"""
_copy_to_directory_attr = {
"srcs": attr.label_list(
allow_files = True,
@ -45,13 +51,9 @@ _copy_to_directory_attr = {
default = ["."],
doc = """List of paths (with glob support) that are roots in the output directory.
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
If any parent directory of a file or directory being copied matches one of the root paths
If any parent directory of a file being copied matches one of the root paths
patterns specified, the output directory path will be the path relative to the root path
instead of the path relative to the file's or directory's workspace. If there are multiple
instead of the path relative to the file's workspace. If there are multiple
root paths that match, the longest match wins.
Matching is done on the parent directory of the output file path so a trailing '**' glob patterm
@ -67,16 +69,17 @@ _copy_to_directory_attr = {
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.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"include_external_repositories": attr.string_list(
doc = """List of external repository names (with glob support) to include in the output directory.
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
Files from external repositories are only copied into the output directory if
the external repository they come from matches one of the external repository patterns
specified.
@ -96,182 +99,153 @@ _copy_to_directory_attr = {
)
```
Files and directories that come from matching external are subject to subsequent filters and
Files that come from matching external are subject to subsequent filters and
transformations to determine if they are copied and what their path in the output
directory will be. The external repository name of the file or directory from an external
directory will be. The external repository name of the file from an external
repository is not included in the output directory path and is considered in subsequent
filters and transformations.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"include_srcs_packages": attr.string_list(
default = ["**"],
doc = """List of Bazel packages (with glob support) to include in output directory.
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
Files in srcs are only copied to the output directory if
the Bazel package of the file matches one of the patterns specified.
Files and directories in srcs are only copied to the output directory if
the Bazel package of the file or directory matches one of the patterns specified.
Forward slashes (`/`) should be used as path separators.
A `"."` value means include srcs that are in the target's package.
It expands to the target's package path (`ctx.label.package`). This
will be an empty string `""` if the target is in the root package.
A `"./**"` value means include srcs that are in subpackages of the target's package.
It expands to the target's package path followed by a slash and a
globstar (`"{{}}/**".format(ctx.label.package)`). If the target's package is
the root package, `"./**"` expands to `["?*", "?*/**"]` instead.
Forward slashes (`/`) should be used as path separators. A first character of `"."`
will be replaced by the target's package path.
Defaults to `["**"]` which includes sources from all packages.
Files and directories that have matching Bazel packages are subject to subsequent filters and
Files that have matching Bazel packages are subject to subsequent filters and
transformations to determine if they are copied and what their path in the output
directory will be.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"exclude_srcs_packages": attr.string_list(
doc = """List of Bazel packages (with glob support) to exclude from output directory.
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
Files in srcs are not copied to the output directory if
the Bazel package of the file matches one of the patterns specified.
Files and directories in srcs are not copied to the output directory if
the Bazel package of the file or directory matches one of the patterns specified.
Forward slashes (`/`) should be used as path separators. A first character of `"."`
will be replaced by the target's package path.
Forward slashes (`/`) should be used as path separators.
A `"."` value means exclude srcs that are in the target's package.
It expands to the target's package path (`ctx.label.package`). This
will be an empty string `""` if the target is in the root package.
A `"./**"` value means exclude srcs that are in subpackages of the target's package.
It expands to the target's package path followed by a slash and a
globstar (`"{{}}/**".format(ctx.label.package)`). If the target's package is
the root package, `"./**"` expands to `["?*", "?*/**"]` instead.
Files and directories that have do not have matching Bazel packages are subject to subsequent
Files that have do not have matching Bazel packages are subject to subsequent
filters and transformations to determine if they are copied and what their path in the output
directory will be.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"include_srcs_patterns": attr.string_list(
default = ["**"],
doc = """List of paths (with glob support) to include in output directory.
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
Files and directories in srcs are only copied to the output directory if their output
Files in srcs are only copied to the output directory if their output
directory path, after applying `root_paths`, matches one of the patterns specified.
Patterns do not look into files within source directory or generated directory (TreeArtifact)
targets since matches are performed in Starlark. To use `include_srcs_patterns` on files
within directories you can use the `make_directory_paths` helper to specify individual files inside
directories in `srcs`. This restriction may be fixed in a future release by performing matching
inside the copy action instead of in Starlark.
Forward slashes (`/`) should be used as path separators.
Defaults to ["**"] which includes all sources.
Files and directories that have matching output directory paths are subject to subsequent
Files that have matching output directory paths are subject to subsequent
filters and transformations to determine if they are copied and what their path in the output
directory will be.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"exclude_srcs_patterns": attr.string_list(
doc = """List of paths (with glob support) to exclude from output directory.
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
Files and directories in srcs are not copied to the output directory if their output
Files in srcs are not copied to the output directory if their output
directory path, after applying `root_paths`, matches one of the patterns specified.
Patterns do not look into files within source directory or generated directory (TreeArtifact)
targets since matches are performed in Starlark. To use `include_srcs_patterns` on files
within directories you can use the `make_directory_paths` helper to specify individual files inside
directories in `srcs`. This restriction may be fixed in a future release by performing matching
inside the copy action instead of in Starlark.
Forward slashes (`/`) should be used as path separators.
Files and directories that do not have matching output directory paths are subject to subsequent
Files that do not have matching output directory paths are subject to subsequent
filters and transformations to determine if they are copied and what their path in the output
directory will be.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"exclude_prefixes": attr.string_list(
doc = """List of path prefixes (with glob support) to exclude from output directory.
DEPRECATED: use `exclude_srcs_patterns` instead
Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
Files and directories in srcs are not copied to the output directory if their output
Files in srcs are not copied to the output directory if their output
directory path, after applying `root_paths`, starts with or fully matches one of the
patterns specified.
Patterns do not look into files within source directory or generated directory (TreeArtifact)
targets since matches are performed in Starlark. To use `include_srcs_patterns` on files
within directories you can use the `make_directory_paths` helper to specify individual files inside
directories in `srcs`. This restriction may be fixed in a future release by performing matching
inside the copy action instead of in Starlark.
Forward slashes (`/`) should be used as path separators.
Files and directories that do not have matching output directory paths are subject to subsequent
Files that do not have matching output directory paths are subject to subsequent
filters and transformations to determine if they are copied and what their path in the output
directory will be.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"replace_prefixes": attr.string_dict(
doc = """Map of paths prefixes (with glob support) to replace in the output directory path when copying files.
Glob patterns `**`, `*` and `?` are supported but the pattern must not end with a `**` glob
expression. See `glob_match` documentation for more details on how to use glob patterns:
https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.
If the output directory path for a file or directory starts with or fully matches a
If the output directory path for a file starts with or fully matches a
a key in the dict then the matching portion of the output directory path is
replaced with the dict value for that key. The final path segment
matched can be a partial match of that segment and only the matching portion will
be replaced. If there are multiple keys that match, the longest match wins.
Patterns do not look into files within source directory or generated directory (TreeArtifact)
targets 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`. This restriction may be fixed in a future release by performing matching
inside the copy action instead of in Starlark.
Forward slashes (`/`) should be used as path separators.
Replace prefix transformation are the final step in the list of filters and transformations.
The final output path of a file or directory being copied into the output directory
The final output path of a file being copied into the output directory
is determined at this step.
{glob_support_docstring}
{filters_transform_order_docstring}
""".format(filters_transform_order_docstring = _filter_transforms_order_docstring),
""".format(
filters_transform_order_docstring = _filter_transforms_order_docstring,
glob_support_docstring = _glob_support_docstring,
),
),
"allow_overwrites": attr.bool(
doc = """If True, allow files to be overwritten if the same output file is copied to twice.
@ -280,10 +254,11 @@ _copy_to_directory_attr = {
This setting has no effect on Windows where overwrites are always allowed.""",
),
# TODO: flip to copy_to_directory_bin_action once ready
# "verbose": attr.bool(
# doc = """If true, prints out verbose logs to stdout""",
# ),
"verbose": attr.bool(
doc = """If true, prints out verbose logs to stdout""",
),
# use '_tool' attribute for development only; do not commit with this attribute active since it
# propagates a dependency on rules_go which would be breaking for users
# "_tool": attr.label(
# executable = True,
# cfg = "exec",
@ -597,12 +572,17 @@ if exist "{src}\\*" (
)
def _copy_to_directory_impl(ctx):
copy_to_directory_bin = ctx.toolchains["@aspect_bazel_lib//lib:copy_to_directory_toolchain_type"].copy_to_directory_info.bin
dst = ctx.actions.declare_directory(ctx.attr.out if ctx.attr.out else ctx.attr.name)
copy_to_directory_action(
copy_to_directory_bin_action(
ctx,
srcs = ctx.attr.srcs,
name = ctx.attr.name,
dst = dst,
copy_to_directory_bin = copy_to_directory_bin, # use ctx.executable._tool for development
files = ctx.files.srcs,
targets = [t for t in ctx.attr.srcs if DirectoryPathInfo in t],
root_paths = ctx.attr.root_paths,
include_external_repositories = ctx.attr.include_external_repositories,
include_srcs_packages = ctx.attr.include_srcs_packages,
@ -612,28 +592,9 @@ def _copy_to_directory_impl(ctx):
exclude_prefixes = ctx.attr.exclude_prefixes,
replace_prefixes = ctx.attr.replace_prefixes,
allow_overwrites = ctx.attr.allow_overwrites,
verbose = ctx.attr.verbose,
)
# TODO: flip to copy_to_directory_bin_action once ready
# copy_to_directory_bin_action(
# ctx,
# name = ctx.attr.name,
# dst = dst,
# copy_to_directory_bin = ctx.executable._tool,
# files = ctx.files.srcs,
# targets = [t for t in ctx.attr.srcs if DirectoryPathInfo in t],
# root_paths = ctx.attr.root_paths,
# include_external_repositories = ctx.attr.include_external_repositories,
# include_srcs_packages = ctx.attr.include_srcs_packages,
# exclude_srcs_packages = ctx.attr.exclude_srcs_packages,
# include_srcs_patterns = ctx.attr.include_srcs_patterns,
# exclude_srcs_patterns = ctx.attr.exclude_srcs_patterns,
# exclude_prefixes = ctx.attr.exclude_prefixes,
# replace_prefixes = ctx.attr.replace_prefixes,
# allow_overwrites = ctx.attr.allow_overwrites,
# verbose = ctx.attr.verbose,
# )
return [
DefaultInfo(
files = depset([dst]),
@ -644,15 +605,12 @@ def _copy_to_directory_impl(ctx):
def _expand_src_packages_patterns(patterns, package):
result = []
for pattern in patterns:
if pattern == ".":
result.append(package)
elif pattern == "./**":
if package:
result.append("{}/**".format(package))
if pattern.startswith("."):
if not package and pattern.startswith("./"):
# special case in the root package
result.append(pattern[2:])
else:
# special case for the root package so we match on subpackages but
# not on the empty root package itself
result.extend(["?*", "?*/**"])
result.append(package + pattern[1:])
else:
result.append(pattern)
return result
@ -737,7 +695,7 @@ def copy_to_directory_bin_action(
# Replace "." in root_paths with the package name of the target
root_paths = [p if p != "." else ctx.label.package for p in root_paths]
# Replace "." and "./**" patterns in in include_srcs_packages & exclude_srcs_packages
# Replace a leading "." with the package name of the target in include_srcs_packages & exclude_srcs_packages
include_srcs_packages = _expand_src_packages_patterns(include_srcs_packages, ctx.label.package)
exclude_srcs_packages = _expand_src_packages_patterns(exclude_srcs_packages, ctx.label.package)
@ -850,6 +808,7 @@ def copy_to_directory_bin_action(
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
)
# TODO(2.0): remove the legacy copy_to_directory_action helper
def copy_to_directory_action(
ctx,
srcs,
@ -865,7 +824,13 @@ def copy_to_directory_action(
replace_prefixes = {},
allow_overwrites = False,
is_windows = None):
"""Helper function to copy files to a directory.
"""Legacy helper function to copy files to a directory.
This helper calculates copy paths in Starlark during analysis and performs the copies in a
bash/bat script. For improved analysis and runtime performance, it is recommended the switch
to `copy_to_directory_bin_action` which calculates copy paths and performs copies with a tool
binary, typically the `@aspect_bazel_lib//tools/copy_to_directory` `go_binary` either built
from source or provided by a toolchain.
This helper is used by copy_to_directory. It is exposed as a public API so it can be used within
other rule implementations where additional_files can also be passed in.
@ -925,7 +890,7 @@ def copy_to_directory_action(
# Replace "." in root_paths with the package name of the target
root_paths = [p if p != "." else ctx.label.package for p in root_paths]
# Replace "." and "./**" patterns in in include_srcs_packages & exclude_srcs_packages
# Replace a leading "." with the package name of the target in include_srcs_packages & exclude_srcs_packages
include_srcs_packages = _expand_src_packages_patterns(include_srcs_packages, ctx.label.package)
exclude_srcs_packages = _expand_src_packages_patterns(exclude_srcs_packages, ctx.label.package)

View File

@ -0,0 +1,188 @@
"Setup copy_to_directory toolchain repositories and rules"
# Platform names follow the platform naming convention in @aspect_bazel_lib//:lib/private/repo_utils.bzl
COPY_TO_DIRECTORY_PLATFORMS = {
"darwin_amd64": struct(
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
),
"darwin_arm64": struct(
compatible_with = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
),
"linux_amd64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"linux_arm64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
),
"windows_amd64": struct(
compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
),
}
# https://github.com/aspect-build/bazel-lib/releases
#
# The integrity hashes can be automatically fetched for the latest copy_to_directory release by running
# `tools/copy_to_directory_mirror_release.sh`. To calculate for a specific release run
# `tools/copy_to_directory_mirror_release.sh <release_version>`
COPY_TO_DIRECTORY_VERSION = "1.19.2"
COPY_TO_DIRECTORY_INTEGRITY = {
"darwin_amd64": "sha256-TdU9sae6uxjkABWnIh5DhHr52KuNPy/F3Lo5MH/hbWc=",
"darwin_arm64": "sha256-jLM7ZWI1xqp1tHcOf8MdQOurgLlAhppvhr1zSsBwOqA=",
"linux_amd64": "sha256-38Plushs6HNUemnAGRS7nN1Bc9Fq5z209m9OOGVCrm4=",
"linux_arm64": "sha256-TbyibQbo2QORCBpX08e4YLDPbFsCQNyk1jxePwe1cFI=",
"windows_amd64": "sha256-0xgHiwX0qHi2TGn1v+cBK7rHTgaz2ulYxCnvF07cVh4=",
}
CopyToDirectoryInfo = provider(
doc = "Provide info for executing copy_to_directory",
fields = {
"bin": "Executable copy_to_directory binary",
},
)
def _copy_to_directory_toolchain_impl(ctx):
binary = ctx.attr.bin.files.to_list()[0]
default_info = DefaultInfo(
files = depset([binary]),
runfiles = ctx.runfiles(files = [binary]),
)
copy_to_directory_info = CopyToDirectoryInfo(
bin = binary,
)
# Export all the providers inside our ToolchainInfo
# so the resolved_toolchain rule can grab and re-export them.
toolchain_info = platform_common.ToolchainInfo(
copy_to_directory_info = copy_to_directory_info,
default = default_info,
)
return [default_info, toolchain_info]
copy_to_directory_toolchain = rule(
implementation = _copy_to_directory_toolchain_impl,
attrs = {
"bin": attr.label(
mandatory = True,
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
)
def _copy_to_directory_toolchains_repo_impl(rctx):
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain
# for the execution or target platform.
# Workaround for https://github.com/bazelbuild/bazel/issues/14009
starlark_content = """# @generated by @aspect_bazel_lib//lib/private:copy_to_directory_toolchain.bzl
# Forward all the providers
def _resolved_toolchain_impl(ctx):
toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:copy_to_directory_toolchain_type"]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.copy_to_directory_info,
toolchain_info.template_variables,
]
# Copied from java_toolchain_alias
# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl
resolved_toolchain = rule(
implementation = _resolved_toolchain_impl,
toolchains = ["@aspect_bazel_lib//lib:copy_to_directory_toolchain_type"],
incompatible_use_toolchain_transition = True,
)
"""
rctx.file("defs.bzl", starlark_content)
build_content = """# @generated by @aspect_bazel_lib//lib/private:copy_to_directory_toolchain.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains flag.
# By default all these toolchains are registered by the copy_to_directory_register_toolchains macro
# so you don't normally need to interact with these targets.
load(":defs.bzl", "resolved_toolchain")
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])
"""
for [platform, meta] in COPY_TO_DIRECTORY_PLATFORMS.items():
build_content += """
toolchain(
name = "{platform}_toolchain",
exec_compatible_with = {compatible_with},
toolchain = "@{user_repository_name}_{platform}//:copy_to_directory_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:copy_to_directory_toolchain_type",
)
""".format(
platform = platform,
user_repository_name = rctx.attr.user_repository_name,
compatible_with = meta.compatible_with,
)
# Base BUILD file for this repository
rctx.file("BUILD.bazel", build_content)
copy_to_directory_toolchains_repo = repository_rule(
_copy_to_directory_toolchains_repo_impl,
doc = """Creates a repository with toolchain definitions for all known platforms
which can be registered or selected.""",
attrs = {
"user_repository_name": attr.string(doc = "Base name for toolchains repository"),
},
)
def _copy_to_directory_platform_repo_impl(rctx):
is_windows = rctx.attr.platform.startswith("windows_")
meta = COPY_TO_DIRECTORY_PLATFORMS[rctx.attr.platform]
release_platform = meta.release_platform if hasattr(meta, "release_platform") else rctx.attr.platform
# https://github.com/aspect-build/bazel-lib/releases/download/v1.19.0/copy_to_directory-linux_amd64
url = "https://github.com/aspect-build/bazel-lib/releases/download/v{0}/copy_to_directory-{1}{2}".format(
COPY_TO_DIRECTORY_VERSION,
release_platform,
".exe" if is_windows else "",
)
rctx.download(
url = url,
output = "copy_to_directory.exe" if is_windows else "copy_to_directory",
executable = True,
integrity = COPY_TO_DIRECTORY_INTEGRITY[release_platform],
)
build_content = """# @generated by @aspect_bazel_lib//lib/private:copy_to_directory_toolchain.bzl
load("@aspect_bazel_lib//lib/private:copy_to_directory_toolchain.bzl", "copy_to_directory_toolchain")
exports_files(["{0}"])
copy_to_directory_toolchain(name = "copy_to_directory_toolchain", bin = "{0}", visibility = ["//visibility:public"])
""".format("copy_to_directory.exe" if is_windows else "copy_to_directory")
# Base BUILD file for this repository
rctx.file("BUILD.bazel", build_content)
copy_to_directory_platform_repo = repository_rule(
implementation = _copy_to_directory_platform_repo_impl,
doc = "Fetch external tools needed for copy_to_directory toolchain",
attrs = {
"platform": attr.string(mandatory = True, values = COPY_TO_DIRECTORY_PLATFORMS.keys()),
},
)

View File

@ -227,3 +227,8 @@ bzl_library(
name = "base64",
srcs = ["//lib/private:base64.bzl"],
)
bzl_library(
name = "copy_to_directory_toolchain",
srcs = ["//lib/private:copy_to_directory_toolchain.bzl"],
)

View File

@ -2,9 +2,11 @@
load("//lib/private:jq_toolchain.bzl", "JQ_PLATFORMS", "jq_host_alias_repo", "jq_platform_repo", "jq_toolchains_repo", _DEFAULT_JQ_VERSION = "DEFAULT_JQ_VERSION")
load("//lib/private:yq_toolchain.bzl", "YQ_PLATFORMS", "yq_host_alias_repo", "yq_platform_repo", "yq_toolchains_repo", _DEFAULT_YQ_VERSION = "DEFAULT_YQ_VERSION")
load("//lib/private:copy_to_directory_toolchain.bzl", "COPY_TO_DIRECTORY_PLATFORMS", "copy_to_directory_platform_repo", "copy_to_directory_toolchains_repo")
load("//lib/private:local_config_platform.bzl", "local_config_platform")
load("//lib:utils.bzl", "is_bazel_6_or_greater", http_archive = "maybe_http_archive")
# buildifier: disable=unnamed-macro
def aspect_bazel_lib_dependencies(override_local_config_platform = False):
"""Load dependencies required by aspect rules
@ -31,6 +33,9 @@ def aspect_bazel_lib_dependencies(override_local_config_platform = False):
name = "local_config_platform",
)
# Always register the copy_to_directory toolchain
register_copy_to_directory_toolchains()
# Re-export the default versions
DEFAULT_JQ_VERSION = _DEFAULT_JQ_VERSION
DEFAULT_YQ_VERSION = _DEFAULT_YQ_VERSION
@ -84,3 +89,24 @@ def register_yq_toolchains(name = "yq", version = DEFAULT_YQ_VERSION, register =
name = "%s_toolchains" % name,
user_repository_name = name,
)
def register_copy_to_directory_toolchains(name = "copy_to_directory", register = True):
"""Registers copy_to_directory toolchain and repositories
Args:
name: override the prefix for the generated toolchain repositories
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
"""
for [platform, meta] in COPY_TO_DIRECTORY_PLATFORMS.items():
copy_to_directory_platform_repo(
name = "%s_%s" % (name, platform),
platform = platform,
)
if register:
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))
copy_to_directory_toolchains_repo(
name = "%s_toolchains" % name,
user_repository_name = name,
)

View File

@ -82,9 +82,6 @@ copy_to_directory(
# flatten test_a & test_b to the root
"test_a": "",
"test_b": "",
# some paths that won't match
"a/": "wont_match_a_is_terminal_path",
"a/a2": "wont_match_since_a2_is_in_a_tree_artifact",
},
)
@ -100,6 +97,7 @@ copy_to_directory(
srcs = case_srcs,
include_external_repositories = ["external_test_repo"],
root_paths = [],
verbose = True,
)
diff_test(
@ -178,12 +176,18 @@ copy_to_directory(
name = "case_8",
srcs = case_srcs,
exclude_srcs_patterns = [
"a",
"b",
"a/*",
"b/*",
"e/e*",
],
)
diff_test(
name = "case_8_test",
file1 = "case_8",
file2 = ":expected_8",
)
# Case 9: directory_path srcs & replace_prefixes (with external repo)
copy_to_directory(
name = "case_9",
@ -224,8 +228,6 @@ copy_to_directory(
# flatten test_a & test_b to the root
"test_a": "",
"test_b": "",
# some paths that won't match
"a/a2": "wont_match_since_a2_is_not_included_in_the_directory_path_in_srcs",
},
)
@ -235,12 +237,6 @@ diff_test(
file2 = ":expected_9",
)
diff_test(
name = "case_8_test",
file1 = "case_8",
file2 = ":expected_8",
)
# Case 10: copy folder containing hidden file
copy_to_directory(
name = "case_10",
@ -313,8 +309,8 @@ copy_to_directory(
name = "case_13",
srcs = case_srcs,
include_srcs_patterns = [
"a",
"b",
"a/*",
"b/*",
"?/e*",
"f/f2/f2",
],
@ -334,8 +330,8 @@ copy_to_directory(
"f/f2/f1",
],
include_srcs_patterns = [
"a",
"b",
"a/*",
"b/*",
"?/e*",
"f/f?/**",
],
@ -395,7 +391,7 @@ diff_test(
copy_to_directory(
name = "case_18",
srcs = case_srcs,
exclude_srcs_packages = ["./**"],
exclude_srcs_packages = ["./*/**"],
include_external_repositories = ["e?ter*_test_repo"],
)
@ -405,12 +401,12 @@ diff_test(
file2 = ":expected_18",
)
# Case 19: include_srcs_packages with "./**" expansion
# Case 19: include_srcs_packages with "./*/**" expansion
copy_to_directory(
name = "case_19",
srcs = case_srcs,
include_external_repositories = ["externa?_*_rep*"],
include_srcs_packages = ["./**"],
include_srcs_packages = ["./*/**"],
)
diff_test(

View File

@ -18,7 +18,9 @@ copy_directory(
lib(
name = "lib",
srcs = ["1"],
srcs = [
"1",
],
# intentionally dup on "1" to make sure it is gracefully handled
others = [
"1",

View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
# Produce a dictionary for the current copy_to_directory tool release,
# suitable for adding to lib/private/copy_to_directory_toolchain.bzl
set -o errexit -o nounset -o pipefail
# Find the latest version
if [ "${1:-}" ]; then
version=$1
else
version=$(curl --silent "https://api.github.com/repos/aspect-build/bazel-lib/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
fi
# Extract the checksums and output a starlark map entry
echo "COPY_TO_DIRECTORY_VERSION = \"$version\""
echo "COPY_TO_DIRECTORY_INTEGRITY = {"
platforms=(darwin_{amd64,arm64} linux_{amd64,arm64} windows_amd64)
for release in ${platforms[@]}; do
integrity="https://github.com/aspect-build/bazel-lib/releases/download/v$version/copy_to_directory-$release"
if [[ $release == windows* ]]; then
integrity="$integrity.exe"
fi
integrity="$integrity.sha256"
curl --silent --location $integrity -o "/tmp/$release.sha256"
echo " \"$release\": \"sha256-$(cat /tmp/$release.sha256 | awk '{ print $1 }' | xxd -r -p | base64)\","
done
echo "}"
printf "\n"
echo "Paste the above into lib/private/copy_to_directory_toolchain.bzl"