github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/language/test_loads_from_flag/lang.go (about) 1 /* Copyright 2022 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_filegroup_with_config generates an "all_files" filegroup target 17 // in each package. This target globs files in the same package and 18 // depends on subpackages. 19 // 20 // These rules are used for testing with go_bazel_test. 21 // 22 // This extension is experimental and subject to change. It is not included 23 // in the default Gazelle binary. 24 package test_loads_from_flag 25 26 import ( 27 "flag" 28 "fmt" 29 "github.com/bazelbuild/bazel-gazelle/config" 30 "github.com/bazelbuild/bazel-gazelle/language" 31 "github.com/bazelbuild/bazel-gazelle/rule" 32 "strings" 33 ) 34 35 const testLoadsFromFlagName = "test_loads_from_flag" 36 37 type testLoadsFromFlag struct { 38 language.BaseLang 39 40 load Load 41 } 42 43 func NewLanguage() language.Language { 44 return &testLoadsFromFlag{} 45 } 46 47 func (l *testLoadsFromFlag) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) { 48 fs.Var(&l.load, "custom-load", "repo,symbol") 49 } 50 51 func (l *testLoadsFromFlag) CheckFlags(fs *flag.FlagSet, c *config.Config) error { 52 c.Exts[testLoadsFromFlagName] = l.load 53 return nil 54 } 55 56 func (*testLoadsFromFlag) Name() string { return testLoadsFromFlagName } 57 58 func (l *testLoadsFromFlag) Kinds() map[string]rule.KindInfo { 59 return map[string]rule.KindInfo{ 60 l.load.symbol: {}, 61 } 62 } 63 64 func (l *testLoadsFromFlag) Loads() []rule.LoadInfo { 65 return []rule.LoadInfo{ 66 { 67 Name: l.load.from, 68 Symbols: []string{l.load.symbol}, 69 }, 70 } 71 } 72 73 func (*testLoadsFromFlag) GenerateRules(args language.GenerateArgs) language.GenerateResult { 74 load := args.Config.Exts[testLoadsFromFlagName].(Load) 75 r := rule.NewRule(load.symbol, "gen") 76 return language.GenerateResult{ 77 Gen: []*rule.Rule{r}, 78 Imports: []interface{}{nil}, 79 } 80 } 81 82 type Load struct { 83 from string 84 symbol string 85 } 86 87 func (l *Load) Set(value string) error { 88 parts := strings.Split(value, ",") 89 if len(parts) != 2 { 90 return fmt.Errorf("want exactly one comma") 91 } 92 l.from = parts[0] 93 l.symbol = parts[1] 94 return nil 95 } 96 97 func (l *Load) String() string { 98 return fmt.Sprintf("load(%q, %q)", l.from, l.symbol) 99 }