github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/bazel/visibility/config.go (about)

     1  /* Copyright 2023 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 visibility
    17  
    18  import (
    19  	"flag"
    20  	"strings"
    21  
    22  	"github.com/bazelbuild/bazel-gazelle/config"
    23  	"github.com/bazelbuild/bazel-gazelle/rule"
    24  )
    25  
    26  const (
    27  	_directiveName = "default_visibility"
    28  )
    29  
    30  type visConfig struct {
    31  	visibilityTargets []string
    32  }
    33  
    34  // getVisConfig directly returns the internal configuration struct rather
    35  // than a pointer because we explicitly want pass-by-value symantics so
    36  // configurations down a directory tree don't accidentially update upstream.
    37  func getVisConfig(c *config.Config) visConfig {
    38  	cfg := c.Exts[_extName]
    39  	if cfg == nil {
    40  		return visConfig{}
    41  	}
    42  	return cfg.(visConfig)
    43  }
    44  
    45  // RegisterFlags noops because we only parameterize behavior with a directive.
    46  func (*visibilityExtension) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {}
    47  
    48  // CheckFlags noops because no flags are referenced.
    49  func (*visibilityExtension) CheckFlags(fs *flag.FlagSet, c *config.Config) error {
    50  	return nil
    51  }
    52  
    53  // KnownDirectives returns the only directive this extension operates on.
    54  func (*visibilityExtension) KnownDirectives() []string {
    55  	return []string{_directiveName}
    56  }
    57  
    58  // Configure identifies the visibility targets from the directive value, if it exists.
    59  //
    60  // To set multiple visibility targets, either multiple directives can be used, or a
    61  // list can be provided with comma-separated values.
    62  func (*visibilityExtension) Configure(c *config.Config, _ string, f *rule.File) {
    63  	cfg := getVisConfig(c)
    64  	if f == nil {
    65  		return
    66  	}
    67  
    68  	var newVisTargets []string
    69  	for _, d := range f.Directives {
    70  		switch d.Key {
    71  		case _directiveName:
    72  			for _, target := range strings.Split(d.Value, ",") {
    73  				newVisTargets = append(newVisTargets, target)
    74  			}
    75  		}
    76  	}
    77  
    78  	// if visibility targets were specified, overwrite the config
    79  	if len(newVisTargets) != 0 {
    80  		cfg.visibilityTargets = newVisTargets
    81  	}
    82  
    83  	c.Exts[_extName] = cfg
    84  }
    85  
    86  // /Configurator embed