...

Source file src/github.com/bazelbuild/buildtools/warn/multifile.go

Documentation: github.com/bazelbuild/buildtools/warn

     1  /*
     2  Copyright 2020 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package warn
    18  
    19  import (
    20  	"github.com/bazelbuild/buildtools/build"
    21  )
    22  
    23  // FileReader is a class that can read an arbitrary Starlark file
    24  // from the repository and cache the results.
    25  type FileReader struct {
    26  	cache    map[string]*build.File
    27  	readFile func(string) ([]byte, error)
    28  }
    29  
    30  // NewFileReader creates and initializes a FileReader instance with a
    31  // custom readFile function that can read an arbitrary file in the
    32  // repository using a path relative to the workspace root
    33  // (OS-independent, with forward slashes).
    34  func NewFileReader(readFile func(string) ([]byte, error)) *FileReader {
    35  	return &FileReader{
    36  		cache:    make(map[string]*build.File),
    37  		readFile: readFile,
    38  	}
    39  }
    40  
    41  // retrieveFile reads a Starlark file using only the readFile method
    42  // (without using the cache).
    43  func (fr *FileReader) retrieveFile(filename string) *build.File {
    44  	contents, err := fr.readFile(filename)
    45  	if err != nil {
    46  		return nil
    47  	}
    48  
    49  	file, err := build.Parse(filename, contents)
    50  	if err != nil {
    51  		return nil
    52  	}
    53  	file.Path = filename
    54  
    55  	return file
    56  }
    57  
    58  // GetFile reads a Starlark file from the repository or the cache.
    59  // Returns nil if the file is not found or not valid.
    60  func (fr *FileReader) GetFile(pkg, label string) *build.File {
    61  	filename := label
    62  	if pkg != "" {
    63  		filename = pkg + "/" + label
    64  	}
    65  
    66  	// Try to retrieve from the cache
    67  	if file, ok := fr.cache[filename]; ok {
    68  		return file
    69  	}
    70  	file := fr.retrieveFile(filename)
    71  	if file != nil {
    72  		file.Pkg = pkg
    73  		file.Label = label
    74  	}
    75  	fr.cache[filename] = file
    76  	return file
    77  }
    78  

View as plain text