1*bcb5dc79SHONG Yifan# Copyright 2024 The Bazel Authors. All rights reserved. 2*bcb5dc79SHONG Yifan# 3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License"); 4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License. 5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at 6*bcb5dc79SHONG Yifan# 7*bcb5dc79SHONG Yifan# http://www.apache.org/licenses/LICENSE-2.0 8*bcb5dc79SHONG Yifan# 9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software 10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS, 11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and 13*bcb5dc79SHONG Yifan# limitations under the License. 14*bcb5dc79SHONG Yifan 15*bcb5dc79SHONG Yifan"""Skylib module containing providers for directories.""" 16*bcb5dc79SHONG Yifan 17*bcb5dc79SHONG Yifanload("//rules/directory/private:glob.bzl", "glob") 18*bcb5dc79SHONG Yifanload("//rules/directory/private:paths.bzl", "DIRECTORY", "FILE", "get_path") 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifandef _init_directory_info(**kwargs): 21*bcb5dc79SHONG Yifan self = struct(**kwargs) 22*bcb5dc79SHONG Yifan kwargs.update( 23*bcb5dc79SHONG Yifan get_path = lambda path: get_path(self, path, require_type = None), 24*bcb5dc79SHONG Yifan get_file = lambda path: get_path(self, path, require_type = FILE), 25*bcb5dc79SHONG Yifan get_subdirectory = lambda path: get_path(self, path, require_type = DIRECTORY), 26*bcb5dc79SHONG Yifan glob = lambda include, exclude = [], allow_empty = False: glob(self, include, exclude, allow_empty), 27*bcb5dc79SHONG Yifan ) 28*bcb5dc79SHONG Yifan return kwargs 29*bcb5dc79SHONG Yifan 30*bcb5dc79SHONG Yifan# TODO: Once bazel 5 no longer needs to be supported, remove this function, and add 31*bcb5dc79SHONG Yifan# init = _init_directory_info to the provider below 32*bcb5dc79SHONG Yifan# buildifier: disable=function-docstring 33*bcb5dc79SHONG Yifandef create_directory_info(**kwargs): 34*bcb5dc79SHONG Yifan return DirectoryInfo(**_init_directory_info(**kwargs)) 35*bcb5dc79SHONG Yifan 36*bcb5dc79SHONG YifanDirectoryInfo = provider( 37*bcb5dc79SHONG Yifan doc = "Information about a directory", 38*bcb5dc79SHONG Yifan # @unsorted-dict-items 39*bcb5dc79SHONG Yifan fields = { 40*bcb5dc79SHONG Yifan "entries": "(Dict[str, Either[File, DirectoryInfo]]) The entries contained directly within. Ordered by filename", 41*bcb5dc79SHONG Yifan "transitive_files": "(depset[File]) All files transitively contained within this directory.", 42*bcb5dc79SHONG Yifan "path": "(string) Path to all files contained within this directory.", 43*bcb5dc79SHONG Yifan "human_readable": "(string) A human readable identifier for a directory. Useful for providing error messages to a user.", 44*bcb5dc79SHONG Yifan "get_path": "(Function(str) -> DirectoryInfo|File) A function to return the entry corresponding to the joined path.", 45*bcb5dc79SHONG Yifan "get_file": "(Function(str) -> File) A function to return the entry corresponding to the joined path.", 46*bcb5dc79SHONG Yifan "get_subdirectory": "(Function(str) -> DirectoryInfo) A function to return the entry corresponding to the joined path.", 47*bcb5dc79SHONG Yifan "glob": "(Function(include, exclude, allow_empty=False)) A function that works the same as native.glob.", 48*bcb5dc79SHONG Yifan }, 49*bcb5dc79SHONG Yifan) 50