1# Copyright 2024 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"""Skylib module containing providers for directories.""" 16 17load("//rules/directory/private:glob.bzl", "glob") 18load("//rules/directory/private:paths.bzl", "DIRECTORY", "FILE", "get_path") 19 20def _init_directory_info(**kwargs): 21 self = struct(**kwargs) 22 kwargs.update( 23 get_path = lambda path: get_path(self, path, require_type = None), 24 get_file = lambda path: get_path(self, path, require_type = FILE), 25 get_subdirectory = lambda path: get_path(self, path, require_type = DIRECTORY), 26 glob = lambda include, exclude = [], allow_empty = False: glob(self, include, exclude, allow_empty), 27 ) 28 return kwargs 29 30# TODO: Once bazel 5 no longer needs to be supported, remove this function, and add 31# init = _init_directory_info to the provider below 32# buildifier: disable=function-docstring 33def create_directory_info(**kwargs): 34 return DirectoryInfo(**_init_directory_info(**kwargs)) 35 36DirectoryInfo = provider( 37 doc = "Information about a directory", 38 # @unsorted-dict-items 39 fields = { 40 "entries": "(Dict[str, Either[File, DirectoryInfo]]) The entries contained directly within. Ordered by filename", 41 "transitive_files": "(depset[File]) All files transitively contained within this directory.", 42 "path": "(string) Path to all files contained within this directory.", 43 "human_readable": "(string) A human readable identifier for a directory. Useful for providing error messages to a user.", 44 "get_path": "(Function(str) -> DirectoryInfo|File) A function to return the entry corresponding to the joined path.", 45 "get_file": "(Function(str) -> File) A function to return the entry corresponding to the joined path.", 46 "get_subdirectory": "(Function(str) -> DirectoryInfo) A function to return the entry corresponding to the joined path.", 47 "glob": "(Function(include, exclude, allow_empty=False)) A function that works the same as native.glob.", 48 }, 49) 50