Skip to content

Commit

Permalink
feat: copy_to_directory copies files and directories to an output dir…
Browse files Browse the repository at this point in the history
…ectory
  • Loading branch information
gregmagolan committed Nov 17, 2021
1 parent 905cbb5 commit 0d2981f
Show file tree
Hide file tree
Showing 115 changed files with 849 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# Take care to document any settings that you expect users to apply.
# Settings that apply only to CI are in .github/workflows/ci.bazelrc

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

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
Expand Down
6 changes: 6 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")

register_unittest_toolchains()

# An external repository for test to use
local_repository(
name = "external_test_repo",
path = "./lib/tests/external_test_repo",
)

############################################
# Gazelle, for generating bzl_library targets
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
Expand Down
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# so that the dependency on stardoc doesn't leak to them.
load("//lib:docs.bzl", "stardoc_with_diff_test", "update_docs")

stardoc_with_diff_test(
bzl_library_target = "//lib/private:copy_to_directory",
out_label = "//docs:copy_to_directory.md",
)

stardoc_with_diff_test(
bzl_library_target = "//lib:docs",
out_label = "//docs:docs.md",
Expand Down
50 changes: 50 additions & 0 deletions docs/copy_to_directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

Copy files and directories to an output directory

<a id="#copy_to_directory"></a>

## copy_to_directory

<pre>
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>)
</pre>

Copies files and directories to an output directory.

Files and directories can be arranged as needed in the output directory using
the `root_paths` and `replace_prefixes` attributes.

NB: This rule is not yet implemented for Windows


**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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-is_windows"></a>is_windows | - | Boolean | required | |
| <a id="copy_to_directory-replace_prefixes"></a>replace_prefixes | Map of paths prefixes to replace in the output directory path when copying files.<br><br>If the output directory path for a file or directory starts with or is equal to a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key.<br><br>Forward slashes (<code>/</code>) should be used as path separators. The final path segment of the key can be a partial match in the corresponding segment of the output directory path.<br><br>If there are multiple keys that match, the longest match wins. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="copy_to_directory-root_paths"></a>root_paths | List of paths that are roots in the output directory. If a file or directory being copied is in one of the listed paths or one of its subpaths, the output directory path is the path relative to the root path instead of the path relative to the file's workspace.<br><br>Forward slashes (<code>/</code>) should be used as path separators. Partial matches on the final path segment of a root path against the corresponding segment in the full workspace relative path of a file are not matched.<br><br>If there are multiple root paths that match, the longest match wins.<br><br>Defaults to [package_name()] so that the output directory path of files in the target's package and and sub-packages are relative to the target's package and files outside of that retain their full workspace relative paths. | List of strings | optional | [] |
| <a id="copy_to_directory-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 | [] |


<a id="#copy_to_directory_lib.impl"></a>

## copy_to_directory_lib.impl

<pre>
copy_to_directory_lib.impl(<a href="#copy_to_directory_lib.impl-ctx">ctx</a>)
</pre>



**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="copy_to_directory_lib.impl-ctx"></a>ctx | <p align="center"> - </p> | none |


36 changes: 34 additions & 2 deletions docs/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ The relative path from frm_file to to_file, including the file name
to_manifest_path(<a href="#to_manifest_path-ctx">ctx</a>, <a href="#to_manifest_path-file">file</a>)
</pre>

The runfiles manifest entry for a file
The runfiles manifest entry path for a file

This is the full runfiles path of a file including its workspace name as
the first segment. We refert to it as the manifest path as it is the path
flavor that is used for in the runfiles MANIFEST file.

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

**RETURNS**

a key that can lookup the path from the runfiles manifest
The runfiles manifest entry path for a file


<a id="#to_workspace_path"></a>

## to_workspace_path

<pre>
to_workspace_path(<a href="#to_workspace_path-ctx">ctx</a>, <a href="#to_workspace_path-file">file</a>)
</pre>

The workspace relative path for a file

This is the full runfiles path of a file excluding its workspace name.
This differs from root path and manifest path as it does not include the
repository name if the file is from an external repository.


**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="to_workspace_path-ctx"></a>ctx | starlark rule execution context | none |
| <a id="to_workspace_path-file"></a>file | a File object | none |

**RETURNS**

The workspace relative path for a file


9 changes: 7 additions & 2 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# For stardoc to reference the files
exports_files(glob(["*.bzl"]))
exports_files(
glob(["*.bzl"]),
visibility = [
"//docs:__pkg__",
"//lib:__subpackages__",
],
)

filegroup(
name = "package_content",
Expand Down
35 changes: 35 additions & 0 deletions lib/copy_file.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# LOCAL MODIFICATIONS
# this has two PRs patched in on top of the original
# https://github.com/bazelbuild/bazel-skylib/blob/7b859037a673db6f606661323e74c5d4751595e6/rules/private/copy_file_private.bzl
# 1) https://github.com/bazelbuild/bazel-skylib/pull/323
# 2) https://github.com/bazelbuild/bazel-skylib/pull/324

"""A rule that copies a file to another place.
native.genrule() is sometimes used to copy files (often wishing to rename them).
The 'copy_file' rule does this with a simpler interface than genrule.
The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
on Windows (no Bash is required).
"""

load(
"//lib/private:copy_file.bzl",
_copy_file = "copy_file",
)

copy_file = _copy_file
25 changes: 25 additions & 0 deletions lib/copy_to_directory.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"Public API for copy_to_directory"

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,
)

def copy_to_directory(name, root_paths = None, **kwargs):
if root_paths == None:
root_paths = [native.package_name()]
_copy_to_directory(
name = name,
root_paths = root_paths,
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)
3 changes: 2 additions & 1 deletion lib/paths.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ load("//lib/private:paths.bzl", "paths")

relative_file = paths.relative_file
to_manifest_path = paths.to_manifest_path
to_workspace_path = paths.to_workspace_path

# Bash helper function for looking up runfiles.
# See windows_utils.bzl for the cmd.exe equivalent.
# Vendored from
# Vendored from
# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash
BASH_RLOCATION_FUNCTION = r"""
# --- begin runfiles.bash initialization v2 ---
Expand Down
15 changes: 15 additions & 0 deletions lib/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

exports_files(
glob(["*.bzl"]),
visibility = ["//docs:__pkg__"],
)

filegroup(
name = "package_content",
srcs = glob([
Expand All @@ -9,6 +14,16 @@ filegroup(
visibility = ["//lib:__pkg__"],
)

bzl_library(
name = "copy_to_directory",
srcs = ["copy_to_directory.bzl"],
visibility = ["//docs:__pkg__"],
deps = [
":paths",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "params_file",
srcs = ["params_file.bzl"],
Expand Down
Loading

0 comments on commit 0d2981f

Please sign in to comment.