github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/list_repository_tools_srcs.go (about) 1 //go:build ignore 2 // +build ignore 3 4 // Copyright 2014 The Bazel Authors. All rights reserved. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 // list_go_repository_tools prints Bazel labels for source files that 19 // gazelle and fetch_repo depend on. go_repository_tools resolves these 20 // labels so that when a source file changes, the gazelle and fetch_repo 21 // binaries are rebuilt for go_repository. 22 23 package main 24 25 import ( 26 "bytes" 27 "flag" 28 "fmt" 29 "log" 30 "os" 31 "path/filepath" 32 "strings" 33 ) 34 35 func main() { 36 dir := flag.String("dir", "", "directory to run in") 37 check := flag.String("check", "", ".bzl file to check (relative to gazelle root)") 38 generate := flag.String("generate", "", ".bzl file to generate (relative to gazelle root)") 39 flag.Parse() 40 if *check == "" && *generate == "" { 41 log.Fatal("neither -check nor -generate were set") 42 } 43 if *check != "" && *generate != "" { 44 log.Fatal("both -check and -generate were set") 45 } 46 if *dir != "" { 47 if err := os.Chdir(filepath.FromSlash(*dir)); err != nil { 48 log.Fatal(err) 49 } 50 } 51 if *check != "" { 52 *check = filepath.FromSlash(*check) 53 } 54 if *generate != "" { 55 *generate = filepath.FromSlash(*generate) 56 } 57 58 var labels []string 59 err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { 60 if err != nil { 61 return err 62 } 63 64 // Skip the tests directory since there is nothing that should be imported from there. 65 if strings.HasPrefix(path, "tests/") { 66 return filepath.SkipDir 67 } 68 69 base := filepath.Base(path) 70 if base == "bcr_tests" || base == "docs" || base == "vendor" || base == "third_party" || base == "testdata" || base == ".ijwb" { 71 return filepath.SkipDir 72 } 73 if !info.IsDir() && 74 (strings.HasSuffix(base, ".go") && !strings.HasSuffix(base, "_test.go") || 75 base == "BUILD.bazel" || base == "BUILD") { 76 label := filepath.ToSlash(path) 77 if i := strings.LastIndexByte(label, '/'); i >= 0 { 78 label = fmt.Sprintf(`Label("//%s:%s")`, label[:i], label[i+1:]) 79 } else { 80 label = fmt.Sprintf(`Label("//:%s")`, label) 81 } 82 labels = append(labels, label) 83 } 84 return nil 85 }) 86 if err != nil { 87 log.Fatal(err) 88 } 89 90 buf := &bytes.Buffer{} 91 fmt.Fprintln(buf, "# Code generated by list_repository_tools_srcs.go; DO NOT EDIT.") 92 fmt.Fprintln(buf, "# regenerate with `go run internal/list_repository_tools_srcs.go -dir $PWD -generate internal/go_repository_tools_srcs.bzl`") 93 fmt.Fprintln(buf, "GO_REPOSITORY_TOOLS_SRCS = [") 94 for _, label := range labels { 95 fmt.Fprintf(buf, " %s,\n", label) 96 } 97 fmt.Fprintln(buf, "]") 98 99 if *generate != "" { 100 if err := os.WriteFile(*generate, buf.Bytes(), 0o666); err != nil { 101 log.Fatal(err) 102 } 103 } else { 104 got, err := os.ReadFile(*check) 105 if err != nil { 106 log.Fatal(err) 107 } 108 if !bytes.Equal(got, buf.Bytes()) { 109 log.Fatalf("generated file %s is not up to date", *check) 110 } 111 } 112 }