Skip to content

Commit

Permalink
Copy rules_directory's globs to bazel-skylib. (#511)
Browse files Browse the repository at this point in the history
Original implementation is at https://github.com/matts1/rules_directory
  • Loading branch information
matts1 authored May 29, 2024
1 parent a464f69 commit f3c0026
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ s = shell.quote(p)
* [common_settings](docs/common_settings_doc.md)
* [directories](docs/copy_directory_doc.md)
* [directory](docs/directory_doc.md)
* [directory_glob](docs/directory_glob.md)
* [subdirectory](docs/subdirectory_doc.md)
* [copy_directory](docs/copy_directory_doc.md)
* [copy_file](docs/copy_file_doc.md)
Expand Down
6 changes: 6 additions & 0 deletions docs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ stardoc_with_diff_test(
out_label = "//docs:directory_doc.md",
)

stardoc_with_diff_test(
name = "directory_glob",
bzl_library_target = "//rules/directory:glob",
out_label = "//docs:directory_glob_doc.md",
)

stardoc_with_diff_test(
name = "directory_providers",
bzl_library_target = "//rules/directory:providers",
Expand Down
Empty file modified docs/directory_doc.md
100644 → 100755
Empty file.
39 changes: 39 additions & 0 deletions docs/directory_glob_doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

Rules to filter files from a directory.

<a id="directory_glob"></a>

## directory_glob

<pre>
directory_glob(<a href="#directory_glob-name">name</a>, <a href="#directory_glob-srcs">srcs</a>, <a href="#directory_glob-data">data</a>, <a href="#directory_glob-allow_empty">allow_empty</a>, <a href="#directory_glob-directory">directory</a>, <a href="#directory_glob-exclude">exclude</a>)
</pre>

globs files from a directory by relative path.

Usage:

```
directory_glob(
name = "foo",
directory = ":directory",
srcs = ["foo/bar"],
data = ["foo/**"],
exclude = ["foo/**/*.h"]
)
```

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="directory_glob-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="directory_glob-srcs"></a>srcs | A list of globs to files within the directory to put in the files.<br><br>For example, `srcs = ["foo/**"]` would collect the file at `<directory>/foo` into the files. | List of strings | optional | `[]` |
| <a id="directory_glob-data"></a>data | A list of globs to files within the directory to put in the runfiles.<br><br>For example, `data = ["foo/**"]` would collect all files contained within `<directory>/foo` into the runfiles. | List of strings | optional | `[]` |
| <a id="directory_glob-allow_empty"></a>allow_empty | If true, allows globs to not match anything. | Boolean | optional | `False` |
| <a id="directory_glob-directory"></a>directory | - | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="directory_glob-exclude"></a>exclude | A list of globs to files within the directory to exclude from the files and runfiles. | List of strings | optional | `[]` |


4 changes: 3 additions & 1 deletion docs/directory_providers_doc.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Skylib module containing providers for directories.
## DirectoryInfo

<pre>
DirectoryInfo(<a href="#DirectoryInfo-entries">entries</a>, <a href="#DirectoryInfo-transitive_files">transitive_files</a>, <a href="#DirectoryInfo-path">path</a>, <a href="#DirectoryInfo-human_readable">human_readable</a>, <a href="#DirectoryInfo-get_path">get_path</a>, <a href="#DirectoryInfo-get_file">get_file</a>, <a href="#DirectoryInfo-get_subdirectory">get_subdirectory</a>)
DirectoryInfo(<a href="#DirectoryInfo-entries">entries</a>, <a href="#DirectoryInfo-transitive_files">transitive_files</a>, <a href="#DirectoryInfo-path">path</a>, <a href="#DirectoryInfo-human_readable">human_readable</a>, <a href="#DirectoryInfo-get_path">get_path</a>, <a href="#DirectoryInfo-get_file">get_file</a>, <a href="#DirectoryInfo-get_subdirectory">get_subdirectory</a>,
<a href="#DirectoryInfo-glob">glob</a>)
</pre>

Information about a directory
Expand All @@ -24,6 +25,7 @@ Information about a directory
| <a id="DirectoryInfo-get_path"></a>get_path | (Function(str) -> DirectoryInfo\|File) A function to return the entry corresponding to the joined path. |
| <a id="DirectoryInfo-get_file"></a>get_file | (Function(str) -> File) A function to return the entry corresponding to the joined path. |
| <a id="DirectoryInfo-get_subdirectory"></a>get_subdirectory | (Function(str) -> DirectoryInfo) A function to return the entry corresponding to the joined path. |
| <a id="DirectoryInfo-glob"></a>glob | (Function(include, exclude, allow_empty=False)) A function that works the same as native.glob. |


<a id="create_directory_info"></a>
Expand Down
Empty file modified docs/directory_subdirectory_doc.md
100644 → 100755
Empty file.
92 changes: 92 additions & 0 deletions docs/directory_utils_doc.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,76 @@

Skylib module containing utility functions related to directories.

<a id="directory_glob"></a>

## directory_glob

<pre>
directory_glob(<a href="#directory_glob-directory">directory</a>, <a href="#directory_glob-include">include</a>, <a href="#directory_glob-allow_empty">allow_empty</a>)
</pre>

native.glob, but for DirectoryInfo.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="directory_glob-directory"></a>directory | (DirectoryInfo) The directory to look relative from. | none |
| <a id="directory_glob-include"></a>include | (List[string]) A list of globs to match. | none |
| <a id="directory_glob-allow_empty"></a>allow_empty | (bool) Whether to allow a glob to not match any files. | `False` |

**RETURNS**

depset[File] A set of files that match.


<a id="directory_glob_chunk"></a>

## directory_glob_chunk

<pre>
directory_glob_chunk(<a href="#directory_glob_chunk-directory">directory</a>, <a href="#directory_glob_chunk-chunk">chunk</a>)
</pre>

Given a directory and a chunk of a glob, returns possible candidates.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="directory_glob_chunk-directory"></a>directory | (DirectoryInfo) The directory to look relative from. | none |
| <a id="directory_glob_chunk-chunk"></a>chunk | (string) A chunk of a glob to look at. | none |

**RETURNS**

List[Either[DirectoryInfo, File]]] The candidate next entries for the chunk.


<a id="directory_single_glob"></a>

## directory_single_glob

<pre>
directory_single_glob(<a href="#directory_single_glob-directory">directory</a>, <a href="#directory_single_glob-glob">glob</a>)
</pre>

Calculates all files that are matched by a glob on a directory.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="directory_single_glob-directory"></a>directory | (DirectoryInfo) The directory to look relative from. | none |
| <a id="directory_single_glob-glob"></a>glob | (string) A glob to match. | none |

**RETURNS**

List[File] A list of files that match.


<a id="get_child"></a>

## get_child
Expand Down Expand Up @@ -52,3 +122,25 @@ Gets a subdirectory contained within a tree of another directory.
(File|DirectoryInfo) The directory contained within.


<a id="transitive_entries"></a>

## transitive_entries

<pre>
transitive_entries(<a href="#transitive_entries-directory">directory</a>)
</pre>

Returns the files and directories contained within a directory transitively.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="transitive_entries-directory"></a>directory | (DirectoryInfo) The directory to look at | none |

**RETURNS**

List[Either[DirectoryInfo, File]] The entries contained within.


10 changes: 10 additions & 0 deletions rules/directory/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ bzl_library(
],
)

bzl_library(
name = "glob",
srcs = ["glob.bzl"],
visibility = ["//visibility:public"],
deps = [
":providers",
],
)

bzl_library(
name = "providers",
srcs = ["providers.bzl"],
visibility = ["//visibility:public"],
deps = [
"//rules/directory/private:glob",
"//rules/directory/private:paths",
],
)
Expand Down
73 changes: 73 additions & 0 deletions rules/directory/glob.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2024 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.
"""Rules to filter files from a directory."""

load(":providers.bzl", "DirectoryInfo")

def _directory_glob_impl(ctx):
directory = ctx.attr.directory[DirectoryInfo]
srcs = directory.glob(
ctx.attr.srcs,
exclude = ctx.attr.exclude,
allow_empty = ctx.attr.allow_empty,
)
data = directory.glob(
ctx.attr.data,
exclude = ctx.attr.exclude,
allow_empty = ctx.attr.allow_empty,
)

return DefaultInfo(
files = srcs,
runfiles = ctx.runfiles(transitive_files = depset(transitive = [srcs, data])),
)

directory_glob = rule(
implementation = _directory_glob_impl,
attrs = {
"allow_empty": attr.bool(
doc = "If true, allows globs to not match anything.",
),
"data": attr.string_list(
doc = """A list of globs to files within the directory to put in the runfiles.
For example, `data = ["foo/**"]` would collect all files contained within `<directory>/foo` into the
runfiles.""",
),
"directory": attr.label(providers = [DirectoryInfo], mandatory = True),
"exclude": attr.string_list(
doc = "A list of globs to files within the directory to exclude from the files and runfiles.",
),
"srcs": attr.string_list(
doc = """A list of globs to files within the directory to put in the files.
For example, `srcs = ["foo/**"]` would collect the file at `<directory>/foo` into the
files.""",
),
},
doc = """globs files from a directory by relative path.
Usage:
```
directory_glob(
name = "foo",
directory = ":directory",
srcs = ["foo/bar"],
data = ["foo/**"],
exclude = ["foo/**/*.h"]
)
```
""",
)
6 changes: 6 additions & 0 deletions rules/directory/private/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ exports_files(
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "glob",
srcs = ["glob.bzl"],
visibility = ["//visibility:public"],
)

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

0 comments on commit f3c0026

Please sign in to comment.