Add utility for checking file existence.

Avoids continued copy-paste of this like https://github.com/aspect-build/rules_ts/blob/main/ts/defs.bzl#L35-L44
This commit is contained in:
Alex Eagle 2022-07-13 21:04:58 -07:00
parent f2e5f4675f
commit 546758589d
4 changed files with 48 additions and 2 deletions

24
docs/utils.md generated
View File

@ -2,6 +2,28 @@
Public API
<a id="file_exists"></a>
## file_exists
<pre>
file_exists(<a href="#file_exists-path">path</a>)
</pre>
Check whether a file exists.
Useful in macros to set defaults for a configuration file if it is present.
This can only be called during the loading phase, not from a rule implementation.
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="file_exists-path"></a>path | a label, or a string which is a path relative to this package | none |
<a id="glob_directories"></a>
## glob_directories
@ -51,7 +73,7 @@ a bool
path_to_workspace_root()
</pre>
Retuns the path to the workspace root under bazel
Returns the path to the workspace root under bazel
**RETURNS**

View File

@ -84,7 +84,7 @@ def _is_external_label(param):
# Path to the root of the workspace
def _path_to_workspace_root():
""" Retuns the path to the workspace root under bazel
""" Returns the path to the workspace root under bazel
Returns:
Path to the workspace root
@ -98,8 +98,24 @@ def _glob_directories(include, **kwargs):
directories = [p for p in all if p not in files]
return directories
def _file_exists(path):
"""Check whether a file exists.
Useful in macros to set defaults for a configuration file if it is present.
This can only be called during the loading phase, not from a rule implementation.
Args:
path: a label, or a string which is a path relative to this package
"""
label = _to_label(path)
file_abs = "%s/%s" % (label.package, label.name)
file_rel = file_abs[len(native.package_name()) + 1:]
file_glob = native.glob([file_rel])
return len(file_glob) > 0
utils = struct(
is_external_label = _is_external_label,
file_exists = _file_exists,
glob_directories = _glob_directories,
path_to_workspace_root = _path_to_workspace_root,
propagate_well_known_tags = _propagate_well_known_tags,

View File

@ -75,6 +75,7 @@ propagate_well_known_tags_test = unittest.make(
_propagate_well_known_tags_test_impl,
)
# buildifier: disable=function-docstring
def utils_test_suite():
to_label_test(name = "to_label_tests", relative_asserts = {
utils.to_label(":utils_test.bzl"): "//lib/tests:utils_test.bzl",
@ -89,3 +90,9 @@ def utils_test_suite():
propagate_well_known_tags_test(
name = "propagate_well_known_tags_tests",
)
# Tests that must run in the loading phase
if utils.file_exists("does-not-exist"):
fail("does-not-exist does not exist")
if not utils.file_exists("utils_test.bzl"):
fail("utils_test.bzl does exist")

View File

@ -7,3 +7,4 @@ glob_directories = utils.glob_directories
path_to_workspace_root = utils.path_to_workspace_root
propagate_well_known_tags = utils.propagate_well_known_tags
to_label = utils.to_label
file_exists = utils.file_exists