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