github.com/wolfd/bazel-gazelle@v0.14.0/internal/language/lang.go (about)

     1  /* Copyright 2018 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  
    16  package language
    17  
    18  import (
    19  	"github.com/bazelbuild/bazel-gazelle/internal/config"
    20  	"github.com/bazelbuild/bazel-gazelle/internal/resolve"
    21  	"github.com/bazelbuild/bazel-gazelle/internal/rule"
    22  )
    23  
    24  // Language describes an extension for Gazelle that provides support for
    25  // a set of Bazel rules.
    26  //
    27  // Languages are used primarily by the fix and update commands. The order
    28  // in which languages are used matters, since languages may depend on
    29  // one another. For example, go depends on proto, since go_proto_libraries
    30  // are generated from metadata stored in proto_libraries.
    31  //
    32  // A single instance of Language is created for each fix / update run. Some
    33  // state may be stored in this instance, but stateless behavior is encouraged,
    34  // especially since some operations may be concurrent in the future.
    35  //
    36  // Tasks languages are used for
    37  //
    38  // * Configuration (embedded interface config.Configurer). Languages may
    39  // define command line flags and alter the configuration in a directory
    40  // based on directives in build files.
    41  //
    42  // * Fixing deprecated usage of rules in build files.
    43  //
    44  // * Generating rules from source files in a directory.
    45  //
    46  // * Resolving library imports (embedded interface resolve.Resolver). For
    47  // example, import strings like "github.com/foo/bar" in Go can be resolved
    48  // into Bazel labels like "@com_github_foo_bar//:go_default_library".
    49  //
    50  // Tasks languages support
    51  //
    52  // * Generating load statements: languages list files and symbols that may
    53  // be loaded.
    54  //
    55  // * Merging generated rules into existing rules: languages provide metadata
    56  // that helps with rule matching, merging, and deletion.
    57  type Language interface {
    58  	config.Configurer
    59  	resolve.Resolver
    60  
    61  	// Kinds returns a map of maps rule names (kinds) and information on how to
    62  	// match and merge attributes that may be found in rules of those kinds. All
    63  	// kinds of rules generated for this language may be found here.
    64  	Kinds() map[string]rule.KindInfo
    65  
    66  	// Loads returns .bzl files and symbols they define. Every rule generated by
    67  	// GenerateRules, now or in the past, should be loadable from one of these
    68  	// files.
    69  	Loads() []rule.LoadInfo
    70  
    71  	// GenerateRules extracts build metadata from source files in a directory.
    72  	// GenerateRules is called in each directory where an update is requested
    73  	// in depth-first post-order.
    74  	//
    75  	// c is the configuration for the current directory.
    76  	// dir is the absolute path to the directory to scan.
    77  	// rel is the relative path to the directory from the repository root.
    78  	// f is the build file. It may be nil. It should not be modified.
    79  	// subdirs is a list of subdirectory names.
    80  	// regularFiles is a list of normal files in the directory.
    81  	// genFiles is a list of generated files, found in outputs of rules.
    82  	// otherEmpty and otherGen are lists of empty and generated rules created
    83  	// by other languages processed before this language.
    84  	//
    85  	// empty is a list of empty rules that may be deleted after merge.
    86  	// gen is a list of generated rules that may be updated or added.
    87  	//
    88  	// Any non-fatal errors this function encounters should be logged using
    89  	// log.Print.
    90  	GenerateRules(c *config.Config, dir, rel string, f *rule.File, subdirs, regularFiles, genFiles []string, otherEmpty, otherGen []*rule.Rule) (empty, gen []*rule.Rule)
    91  
    92  	// Fix repairs deprecated usage of language-specific rules in f. This is
    93  	// called before the file is indexed. Unless c.ShouldFix is true, fixes
    94  	// that delete or rename rules should not be performed.
    95  	Fix(c *config.Config, f *rule.File)
    96  }