Skip to content

Commit 0d2981f

Browse files
committed
feat: copy_to_directory copies files and directories to an output directory
1 parent 905cbb5 commit 0d2981f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+849
-15
lines changed

.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# Take care to document any settings that you expect users to apply.
33
# Settings that apply only to CI are in .github/workflows/ci.bazelrc
44

5+
# Allow the Bazel server to check directory sources for changes
6+
# Avoids warning spam with rules_nodejs feature that models node_modules as directories
7+
# See https://github.com/bazelbuild/rules_nodejs/releases/tag/3.6.0
8+
startup --host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1
59

610
# Load any settings specific to the current user.
711
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members

WORKSPACE

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")
1515

1616
register_unittest_toolchains()
1717

18+
# An external repository for test to use
19+
local_repository(
20+
name = "external_test_repo",
21+
path = "./lib/tests/external_test_repo",
22+
)
23+
1824
############################################
1925
# Gazelle, for generating bzl_library targets
2026
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

docs/BUILD.bazel

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# so that the dependency on stardoc doesn't leak to them.
33
load("//lib:docs.bzl", "stardoc_with_diff_test", "update_docs")
44

5+
stardoc_with_diff_test(
6+
bzl_library_target = "//lib/private:copy_to_directory",
7+
out_label = "//docs:copy_to_directory.md",
8+
)
9+
510
stardoc_with_diff_test(
611
bzl_library_target = "//lib:docs",
712
out_label = "//docs:docs.md",

docs/copy_to_directory.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2+
3+
Copy files and directories to an output directory
4+
5+
<a id="#copy_to_directory"></a>
6+
7+
## copy_to_directory
8+
9+
<pre>
10+
copy_to_directory(<a href="#copy_to_directory-name">name</a>, <a href="#copy_to_directory-is_windows">is_windows</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>)
11+
</pre>
12+
13+
Copies files and directories to an output directory.
14+
15+
Files and directories can be arranged as needed in the output directory using
16+
the `root_paths` and `replace_prefixes` attributes.
17+
18+
NB: This rule is not yet implemented for Windows
19+
20+
21+
**ATTRIBUTES**
22+
23+
24+
| Name | Description | Type | Mandatory | Default |
25+
| :------------- | :------------- | :------------- | :------------- | :------------- |
26+
| <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 | |
27+
| <a id="copy_to_directory-is_windows"></a>is_windows | - | Boolean | required | |
28+
| <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 | {} |
29+
| <a id="copy_to_directory-root_paths"></a>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.<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 | [] |
30+
| <a id="copy_to_directory-srcs"></a>srcs | Files and/or directories to copy into the output directory | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
31+
32+
33+
<a id="#copy_to_directory_lib.impl"></a>
34+
35+
## copy_to_directory_lib.impl
36+
37+
<pre>
38+
copy_to_directory_lib.impl(<a href="#copy_to_directory_lib.impl-ctx">ctx</a>)
39+
</pre>
40+
41+
42+
43+
**PARAMETERS**
44+
45+
46+
| Name | Description | Default Value |
47+
| :------------- | :------------- | :------------- |
48+
| <a id="copy_to_directory_lib.impl-ctx"></a>ctx | <p align="center"> - </p> | none |
49+
50+

docs/paths.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ The relative path from frm_file to to_file, including the file name
3333
to_manifest_path(<a href="#to_manifest_path-ctx">ctx</a>, <a href="#to_manifest_path-file">file</a>)
3434
</pre>
3535

36-
The runfiles manifest entry for a file
36+
The runfiles manifest entry path for a file
37+
38+
This is the full runfiles path of a file including its workspace name as
39+
the first segment. We refert to it as the manifest path as it is the path
40+
flavor that is used for in the runfiles MANIFEST file.
3741

3842
We must avoid using non-normalized paths (workspace/../other_workspace/path)
3943
in order to locate entries by their key.
@@ -49,6 +53,34 @@ in order to locate entries by their key.
4953

5054
**RETURNS**
5155

52-
a key that can lookup the path from the runfiles manifest
56+
The runfiles manifest entry path for a file
57+
58+
59+
<a id="#to_workspace_path"></a>
60+
61+
## to_workspace_path
62+
63+
<pre>
64+
to_workspace_path(<a href="#to_workspace_path-ctx">ctx</a>, <a href="#to_workspace_path-file">file</a>)
65+
</pre>
66+
67+
The workspace relative path for a file
68+
69+
This is the full runfiles path of a file excluding its workspace name.
70+
This differs from root path and manifest path as it does not include the
71+
repository name if the file is from an external repository.
72+
73+
74+
**PARAMETERS**
75+
76+
77+
| Name | Description | Default Value |
78+
| :------------- | :------------- | :------------- |
79+
| <a id="to_workspace_path-ctx"></a>ctx | starlark rule execution context | none |
80+
| <a id="to_workspace_path-file"></a>file | a File object | none |
81+
82+
**RETURNS**
83+
84+
The workspace relative path for a file
5385

5486

lib/BUILD.bazel

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22

3-
# For stardoc to reference the files
4-
exports_files(glob(["*.bzl"]))
3+
exports_files(
4+
glob(["*.bzl"]),
5+
visibility = [
6+
"//docs:__pkg__",
7+
"//lib:__subpackages__",
8+
],
9+
)
510

611
filegroup(
712
name = "package_content",

lib/copy_file.bzl

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# LOCAL MODIFICATIONS
16+
# this has two PRs patched in on top of the original
17+
# https://github.com/bazelbuild/bazel-skylib/blob/7b859037a673db6f606661323e74c5d4751595e6/rules/private/copy_file_private.bzl
18+
# 1) https://github.com/bazelbuild/bazel-skylib/pull/323
19+
# 2) https://github.com/bazelbuild/bazel-skylib/pull/324
20+
21+
"""A rule that copies a file to another place.
22+
23+
native.genrule() is sometimes used to copy files (often wishing to rename them).
24+
The 'copy_file' rule does this with a simpler interface than genrule.
25+
26+
The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
27+
on Windows (no Bash is required).
28+
"""
29+
30+
load(
31+
"//lib/private:copy_file.bzl",
32+
_copy_file = "copy_file",
33+
)
34+
35+
copy_file = _copy_file

lib/copy_to_directory.bzl

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"Public API for copy_to_directory"
2+
3+
load(
4+
"//lib/private:copy_to_directory.bzl",
5+
lib = "copy_to_directory_lib",
6+
)
7+
8+
_copy_to_directory = rule(
9+
implementation = lib.impl,
10+
provides = lib.provides,
11+
attrs = lib.attrs,
12+
)
13+
14+
def copy_to_directory(name, root_paths = None, **kwargs):
15+
if root_paths == None:
16+
root_paths = [native.package_name()]
17+
_copy_to_directory(
18+
name = name,
19+
root_paths = root_paths,
20+
is_windows = select({
21+
"@bazel_tools//src/conditions:host_windows": True,
22+
"//conditions:default": False,
23+
}),
24+
**kwargs
25+
)

lib/paths.bzl

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ load("//lib/private:paths.bzl", "paths")
44

55
relative_file = paths.relative_file
66
to_manifest_path = paths.to_manifest_path
7+
to_workspace_path = paths.to_workspace_path
78

89
# Bash helper function for looking up runfiles.
910
# See windows_utils.bzl for the cmd.exe equivalent.
10-
# Vendored from
11+
# Vendored from
1112
# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash
1213
BASH_RLOCATION_FUNCTION = r"""
1314
# --- begin runfiles.bash initialization v2 ---

lib/private/BUILD.bazel

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22

3+
exports_files(
4+
glob(["*.bzl"]),
5+
visibility = ["//docs:__pkg__"],
6+
)
7+
38
filegroup(
49
name = "package_content",
510
srcs = glob([
@@ -9,6 +14,16 @@ filegroup(
914
visibility = ["//lib:__pkg__"],
1015
)
1116

17+
bzl_library(
18+
name = "copy_to_directory",
19+
srcs = ["copy_to_directory.bzl"],
20+
visibility = ["//docs:__pkg__"],
21+
deps = [
22+
":paths",
23+
"@bazel_skylib//lib:paths",
24+
],
25+
)
26+
1227
bzl_library(
1328
name = "params_file",
1429
srcs = ["params_file.bzl"],

0 commit comments

Comments
 (0)