From 58b2c59bb07950a38906f0f657135b10ef768f89 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Fri, 29 Apr 2022 15:26:24 -0700 Subject: [PATCH] refactor: export copy_to_directory_lib in the public API for use downstream in packaging rules (#96) --- docs/copy_to_directory.md | 43 ++++++++---- lib/copy_to_directory.bzl | 111 ++++-------------------------- lib/private/copy_to_directory.bzl | 86 ++++++++++++++++++++--- 3 files changed, 120 insertions(+), 120 deletions(-) diff --git a/docs/copy_to_directory.md b/docs/copy_to_directory.md index 3a5cf876e..f83478dd2 100644 --- a/docs/copy_to_directory.md +++ b/docs/copy_to_directory.md @@ -1,20 +1,43 @@ -Copy files and directories to an output directory +Copies files and directories to an output directory. + +Files and directories can be arranged as needed in the output directory using +the `root_paths`, `exclude_prefixes` and `replace_prefixes` attributes. + ## copy_to_directory
-copy_to_directory(name, srcs, root_paths, include_external_repositories, exclude_prefixes,
-                  replace_prefixes, kwargs)
+copy_to_directory(name, exclude_prefixes, include_external_repositories, replace_prefixes,
+                  root_paths, srcs)
 
-Copies files and directories to an output directory. -Files and directories can be arranged as needed in the output directory using -the `root_paths`, `exclude_prefixes` and `replace_prefixes` attributes. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| exclude_prefixes | 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. | List of strings | optional | [] | +| include_external_repositories | List of external repository names to include in the output directory.

Files from external repositories are not copied into the output directory unless the external repository they come from is listed here.

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.

For example, the following copies @external_repo//path/to:file to path/to/file within the output directory.

         copy_to_directory(             name = "dir",             include_external_repositories = ["external_repo"],             srcs = ["@external_repo//path/to:file"],         )         


Files from external repositories are subject to root_paths, exclude_prefixes and replace_prefixes in the same way as files form the main repository. | List of strings | optional | [] | +| replace_prefixes | Map of paths prefixes to replace in the output directory path when copying files.

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.

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.

If there are multiple keys that match, the longest match wins. | Dictionary: String -> String | optional | {} | +| root_paths | List of paths that are roots in the output directory.

"." values indicate the targets 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 relative to the file's workspace.

Forward slashes (/) 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.

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 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 | ["."] | +| srcs | Files and/or directories or targets that provide DirectoryPathInfo to copy into the output directory. | List of labels | optional | [] | + + + + +## copy_to_directory_lib.impl + +
+copy_to_directory_lib.impl(ctx)
+
+ **PARAMETERS** @@ -22,12 +45,6 @@ the `root_paths`, `exclude_prefixes` and `replace_prefixes` attributes. | Name | Description | Default Value | | :------------- | :------------- | :------------- | -| name | A unique name for this target. | none | -| srcs | Files and/or directories or targets that provide DirectoryPathInfo to copy into the output directory. | [] | -| root_paths | List of paths that are roots in the output directory.

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.

Forward slashes (/) 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.

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 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. | None | -| include_external_repositories | List of external repository names to include in the output directory.

Files from external repositories are not copied into the output directory unless the external repository they come from is listed here.

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.

For example, the following copies @external_repo//path/to:file to path/to/file within the output directory.

 copy_to_directory(     name = "dir",     include_external_repositories = ["external_repo"],     srcs = ["@external_repo//path/to:file"], ) 


Files from external repositories are subject to root_paths, exclude_prefixes and replace_prefixes in the same way as files form the main repository. | [] | -| exclude_prefixes | 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. | [] | -| replace_prefixes | Map of paths prefixes to replace in the output directory path when copying files.

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.

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.

If there are multiple keys that match, the longest match wins. | {} | -| kwargs | Other common named parameters such as tags or visibility | none | +| ctx |

-

| none | diff --git a/lib/copy_to_directory.bzl b/lib/copy_to_directory.bzl index a6e93dd1f..d7ed7e483 100644 --- a/lib/copy_to_directory.bzl +++ b/lib/copy_to_directory.bzl @@ -1,104 +1,19 @@ -"Copy files and directories to an output directory" +"""Copies files and directories to an output directory. + +Files and directories can be arranged as needed in the output directory using +the `root_paths`, `exclude_prefixes` and `replace_prefixes` attributes. +""" load( "//lib/private:copy_to_directory.bzl", - lib = "copy_to_directory_lib", -) - -_copy_to_directory = rule( - implementation = lib.impl, - provides = lib.provides, - attrs = lib.attrs, + _copy_to_directory_lib = "copy_to_directory_lib", ) -def copy_to_directory( - name, - srcs = [], - root_paths = None, - include_external_repositories = [], - exclude_prefixes = [], - replace_prefixes = {}, - **kwargs): - """Copies files and directories to an output directory. - - Files and directories can be arranged as needed in the output directory using - the `root_paths`, `exclude_prefixes` and `replace_prefixes` attributes. - - Args: - name: A unique name for this target. - - srcs: Files and/or directories or targets that provide DirectoryPathInfo to copy into the output directory. - - root_paths: List of paths that are roots in the output directory. - - 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. - - Forward slashes (`/`) 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. - - 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 - 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. - - include_external_repositories: List of external repository names to include in the output directory. - - Files from external repositories are not copied into the output directory unless - the external repository they come from is listed here. - - 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. +# export the starlark library as a public API +copy_to_directory_lib = _copy_to_directory_lib - For example, the following copies `@external_repo//path/to:file` to - `path/to/file` within the output directory. - - ``` - copy_to_directory( - name = "dir", - include_external_repositories = ["external_repo"], - srcs = ["@external_repo//path/to:file"], - ) - ``` - - Files from external repositories are subject to `root_paths`, `exclude_prefixes` - and `replace_prefixes` in the same way as files form the main repository. - - exclude_prefixes: 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. - - replace_prefixes: Map of paths prefixes to replace in the output directory path when copying files. - - 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. - - 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. - - If there are multiple keys that match, the longest match wins. - - **kwargs: Other common named parameters such as `tags` or `visibility` - """ - - if root_paths == None: - root_paths = [native.package_name()] - - _copy_to_directory( - name = name, - srcs = srcs, - root_paths = root_paths, - include_external_repositories = include_external_repositories, - exclude_prefixes = exclude_prefixes, - replace_prefixes = replace_prefixes, - **kwargs - ) +copy_to_directory = rule( + implementation = _copy_to_directory_lib.impl, + provides = _copy_to_directory_lib.provides, + attrs = _copy_to_directory_lib.attrs, +) diff --git a/lib/private/copy_to_directory.bzl b/lib/private/copy_to_directory.bzl index 30e8d5a6e..f4eb9506f 100644 --- a/lib/private/copy_to_directory.bzl +++ b/lib/private/copy_to_directory.bzl @@ -6,11 +6,76 @@ load(":paths.bzl", "paths") load(":directory_path.bzl", "DirectoryPathInfo") _copy_to_directory_attr = { - "srcs": attr.label_list(allow_files = True), - "root_paths": attr.string_list(default = []), - "include_external_repositories": attr.string_list(default = []), - "exclude_prefixes": attr.string_list(default = []), - "replace_prefixes": attr.string_dict(default = {}), + "srcs": attr.label_list( + allow_files = True, + doc = """Files and/or directories or targets that provide DirectoryPathInfo to copy + into the output directory.""", + ), + "root_paths": attr.string_list( + default = ["."], + doc = """List of paths that are roots in the output directory. + + "." values indicate the targets 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 + relative to the file's workspace. + + Forward slashes (`/`) 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. + + 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 + 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.""", + ), + "include_external_repositories": attr.string_list( + doc = """List of external repository names to include in the output directory. + + Files from external repositories are not copied into the output directory unless + the external repository they come from is listed here. + + 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. + + For example, the following copies `@external_repo//path/to:file` to + `path/to/file` within the output directory. + + ``` + copy_to_directory( + name = "dir", + include_external_repositories = ["external_repo"], + srcs = ["@external_repo//path/to:file"], + ) + ``` + + Files from external repositories are subject to `root_paths`, `exclude_prefixes` + and `replace_prefixes` in the same way as files form the main repository.""", + ), + "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.""", + ), + "replace_prefixes": attr.string_dict( + doc = """Map of paths prefixes to replace in the output directory path when copying files. + + 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. + + 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. + + If there are multiple keys that match, the longest match wins.""", + ), "_windows_constraint": attr.label(default = "@platforms//os:windows"), } @@ -27,7 +92,7 @@ def _longest_match(subject, tests, allow_partial = False): return match # src can either be a File or a target with a DirectoryPathInfo -def _copy_paths(ctx, src): +def _copy_paths(ctx, root_paths, src): if type(src) == "File": src_file = src src_path = src_file.path @@ -45,7 +110,7 @@ def _copy_paths(ctx, src): return None, None, None # strip root paths - root_path = _longest_match(output_path, ctx.attr.root_paths) + root_path = _longest_match(output_path, root_paths) if root_path: strip_depth = len(root_path.split("/")) output_path = "/".join(output_path.split("/")[strip_depth:]) @@ -169,18 +234,21 @@ def _copy_to_directory_impl(ctx): msg = "srcs must not be empty in copy_to_directory %s" % ctx.label fail(msg) + # Replace "." root paths with the package name of the target + root_paths = [p if p != "." else ctx.label.package for p in ctx.attr.root_paths] + output = ctx.actions.declare_directory(ctx.attr.name) # Gather a list of src_path, dst_path pairs copy_paths = [] for src in ctx.attr.srcs: if DirectoryPathInfo in src: - src_path, output_path, src_file = _copy_paths(ctx, src) + src_path, output_path, src_file = _copy_paths(ctx, root_paths, src) if src_path != None: dst_path = skylib_paths.normalize("/".join([output.path, output_path])) copy_paths.append((src_path, dst_path, src_file)) for src_file in ctx.files.srcs: - src_path, output_path, src_file = _copy_paths(ctx, src_file) + src_path, output_path, src_file = _copy_paths(ctx, root_paths, src_file) if src_path != None: dst_path = skylib_paths.normalize("/".join([output.path, output_path])) copy_paths.append((src_path, dst_path, src_file))