github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/go/update.go (about) 1 /* Copyright 2019 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 golang 17 18 import ( 19 "path/filepath" 20 "sort" 21 "strings" 22 23 "github.com/bazelbuild/bazel-gazelle/language" 24 "github.com/bazelbuild/bazel-gazelle/rule" 25 "golang.org/x/sync/errgroup" 26 ) 27 28 // UpdateRepos generates go_repository rules corresponding to modules in 29 // args.Imports. Each module argument may specify a version with an '@' suffix 30 // (in the same format as 'go get'). If no version is specified, @latest 31 // is requested. 32 func (*goLang) UpdateRepos(args language.UpdateReposArgs) language.UpdateReposResult { 33 gen := make([]*rule.Rule, len(args.Imports)) 34 var eg errgroup.Group 35 for i := range args.Imports { 36 i := i 37 eg.Go(func() error { 38 arg := args.Imports[i] 39 modPath, query := arg, "latest" 40 if i := strings.IndexByte(arg, '@'); i >= 0 { 41 modPath, query = arg[:i], arg[i+1:] 42 } 43 name, version, sum, err := args.Cache.ModVersion(modPath, query) 44 if err != nil { 45 return err 46 } 47 gen[i] = rule.NewRule("go_repository", name) 48 gen[i].SetAttr("importpath", modPath) 49 gen[i].SetAttr("version", version) 50 gen[i].SetAttr("sum", sum) 51 setBuildAttrs(getGoConfig(args.Config), gen[i]) 52 return nil 53 }) 54 } 55 if err := eg.Wait(); err != nil { 56 return language.UpdateReposResult{Error: err} 57 } 58 return language.UpdateReposResult{Gen: gen} 59 } 60 61 var repoImportFuncs = map[string]func(args language.ImportReposArgs) language.ImportReposResult{ 62 "go.mod": importReposFromModules, 63 "go.work": importReposFromWork, 64 } 65 66 func (*goLang) CanImport(path string) bool { 67 return repoImportFuncs[filepath.Base(path)] != nil 68 } 69 70 func (*goLang) ImportRepos(args language.ImportReposArgs) language.ImportReposResult { 71 res := repoImportFuncs[filepath.Base(args.Path)](args) 72 for _, r := range res.Gen { 73 setBuildAttrs(getGoConfig(args.Config), r) 74 } 75 if args.Prune { 76 genNamesSet := make(map[string]bool) 77 for _, r := range res.Gen { 78 genNamesSet[r.Name()] = true 79 } 80 for _, r := range args.Config.Repos { 81 if name := r.Name(); r.Kind() == "go_repository" && !genNamesSet[name] { 82 res.Empty = append(res.Empty, rule.NewRule("go_repository", name)) 83 } 84 } 85 } 86 sortRules(res.Gen) 87 return res 88 } 89 90 func setBuildAttrs(gc *goConfig, r *rule.Rule) { 91 if gc.buildDirectivesAttr != "" { 92 buildDirectives := strings.Split(gc.buildDirectivesAttr, ",") 93 r.SetAttr("build_directives", buildDirectives) 94 } 95 if gc.buildExternalAttr != "" { 96 r.SetAttr("build_external", gc.buildExternalAttr) 97 } 98 if gc.buildExtraArgsAttr != "" { 99 extraArgs := strings.Split(gc.buildExtraArgsAttr, ",") 100 r.SetAttr("build_extra_args", extraArgs) 101 } 102 if gc.buildFileGenerationAttr != "" { 103 r.SetAttr("build_file_generation", gc.buildFileGenerationAttr) 104 } 105 if gc.buildFileNamesAttr != "" { 106 r.SetAttr("build_file_name", gc.buildFileNamesAttr) 107 } 108 if gc.buildFileProtoModeAttr != "" { 109 r.SetAttr("build_file_proto_mode", gc.buildFileProtoModeAttr) 110 } 111 if gc.buildTagsAttr != "" { 112 buildTags := strings.Split(gc.buildTagsAttr, ",") 113 r.SetAttr("build_tags", buildTags) 114 } 115 } 116 117 func sortRules(rules []*rule.Rule) { 118 sort.SliceStable(rules, func(i, j int) bool { 119 if cmp := strings.Compare(rules[i].Name(), rules[j].Name()); cmp != 0 { 120 return cmp < 0 121 } 122 return rules[i].AttrString("importpath") < rules[j].AttrString("importpath") 123 }) 124 }