github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/language/test_load_for_packed_rules/lang.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 `test_load_for_packed_rules` generates packed
    17  // rule of `selects.config_setting_group`.
    18  //
    19  // This extension is experimental and subject to change. It is not included
    20  // in the default Gazelle binary.
    21  package test_load_for_packed_rules
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/bazelbuild/bazel-gazelle/config"
    27  	"github.com/bazelbuild/bazel-gazelle/label"
    28  	"github.com/bazelbuild/bazel-gazelle/language"
    29  	"github.com/bazelbuild/bazel-gazelle/repo"
    30  	"github.com/bazelbuild/bazel-gazelle/resolve"
    31  	"github.com/bazelbuild/bazel-gazelle/rule"
    32  )
    33  
    34  const testLoadForPackedRulesName = "test_load_for_packed_rules"
    35  
    36  type testLoadForPackedRulesLang struct {
    37  	language.BaseLang
    38  
    39  	Initialized, RulesGenerated, DepsResolved bool
    40  }
    41  
    42  var (
    43  	_ language.Language         = (*testLoadForPackedRulesLang)(nil)
    44  	_ language.LifecycleManager = (*testLoadForPackedRulesLang)(nil)
    45  )
    46  
    47  func NewLanguage() language.Language {
    48  	return &testLoadForPackedRulesLang{}
    49  }
    50  
    51  var kinds = map[string]rule.KindInfo{
    52  	"selects.config_setting_group": {
    53  		NonEmptyAttrs: map[string]bool{"name": true},
    54  		MergeableAttrs: map[string]bool{
    55  			"match_all": true,
    56  			"match_any": true,
    57  		},
    58  	},
    59  }
    60  
    61  var loads = []rule.LoadInfo{
    62  	{
    63  		Name: "@bazel_skylib//lib:selects.bzl",
    64  		Symbols: []string{
    65  			"selects",
    66  		},
    67  	},
    68  }
    69  
    70  func (*testLoadForPackedRulesLang) Name() string {
    71  	return testLoadForPackedRulesName
    72  }
    73  
    74  func (*testLoadForPackedRulesLang) Kinds() map[string]rule.KindInfo {
    75  	return kinds
    76  }
    77  
    78  func (*testLoadForPackedRulesLang) Loads() []rule.LoadInfo {
    79  	return loads
    80  }
    81  
    82  func (l *testLoadForPackedRulesLang) Before(ctx context.Context) {
    83  	l.Initialized = true
    84  }
    85  
    86  func (l *testLoadForPackedRulesLang) GenerateRules(args language.GenerateArgs) language.GenerateResult {
    87  	if !l.Initialized {
    88  		panic("GenerateRules must not be called before Before")
    89  	}
    90  	if l.RulesGenerated {
    91  		panic("GenerateRules must not be called after DoneGeneratingRules")
    92  	}
    93  
    94  	r := rule.NewRule("selects.config_setting_group", "all_configs_group")
    95  
    96  	match := []string{
    97  		"//:config_a",
    98  		"//:config_b",
    99  	}
   100  
   101  	r.SetAttr("match_all", match)
   102  
   103  	return language.GenerateResult{
   104  		Gen:     []*rule.Rule{r},
   105  		Imports: []interface{}{nil},
   106  	}
   107  }
   108  
   109  func (l *testLoadForPackedRulesLang) DoneGeneratingRules() {
   110  	l.RulesGenerated = true
   111  }
   112  
   113  func (l *testLoadForPackedRulesLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, imports interface{}, from label.Label) {
   114  	if !l.RulesGenerated {
   115  		panic("Expected a call to DoneGeneratingRules before Resolve")
   116  	}
   117  	if l.DepsResolved {
   118  		panic("Resolve must be called before calling AfterResolvingDeps")
   119  	}
   120  }
   121  
   122  func (l *testLoadForPackedRulesLang) AfterResolvingDeps(ctx context.Context) {
   123  	l.DepsResolved = true
   124  }