Rename import symbol bzl to bf in all tools (#589)
This is in response to a review comment on an earlier Gazelle PR. The
package is "github.com/bazelbuild/buildtools/build", but "build" is
easy to confuse with "go/build". "bzl" is the suffix for Skylark files
(containing rule and macro definitions), so it doesn't seem
appropriate. "bf" can stand for "BUILD file" or "Bazel format".
diff --git a/go/tools/gazelle/gazelle/diff.go b/go/tools/gazelle/gazelle/diff.go
index 765e361..293c410 100644
--- a/go/tools/gazelle/gazelle/diff.go
+++ b/go/tools/gazelle/gazelle/diff.go
@@ -20,18 +20,18 @@
"os"
"os/exec"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
)
-func diffFile(c *config.Config, file *bzl.File) error {
+func diffFile(c *config.Config, file *bf.File) error {
f, err := ioutil.TempFile("", c.DefaultBuildFileName())
if err != nil {
return err
}
defer os.Remove(f.Name())
defer f.Close()
- if _, err := f.Write(bzl.Format(file)); err != nil {
+ if _, err := f.Write(bf.Format(file)); err != nil {
return err
}
if err := f.Sync(); err != nil {
diff --git a/go/tools/gazelle/gazelle/fix.go b/go/tools/gazelle/gazelle/fix.go
index 4994584..7a9f5e0 100644
--- a/go/tools/gazelle/gazelle/fix.go
+++ b/go/tools/gazelle/gazelle/fix.go
@@ -18,12 +18,12 @@
import (
"io/ioutil"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
)
-func fixFile(c *config.Config, file *bzl.File) error {
- if err := ioutil.WriteFile(file.Path, bzl.Format(file), 0644); err != nil {
+func fixFile(c *config.Config, file *bf.File) error {
+ if err := ioutil.WriteFile(file.Path, bf.Format(file), 0644); err != nil {
return err
}
return nil
diff --git a/go/tools/gazelle/gazelle/fix_test.go b/go/tools/gazelle/gazelle/fix_test.go
index 85a18f6..9b90573 100644
--- a/go/tools/gazelle/gazelle/fix_test.go
+++ b/go/tools/gazelle/gazelle/fix_test.go
@@ -22,7 +22,7 @@
"path/filepath"
"testing"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
)
@@ -52,16 +52,16 @@
}
defer os.RemoveAll(dir)
- stubFile := &bzl.File{
+ stubFile := &bf.File{
Path: filepath.Join(dir, "BUILD.bazel"),
- Stmt: []bzl.Expr{
- &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: "foo_rule"},
- List: []bzl.Expr{
- &bzl.BinaryExpr{
- X: &bzl.LiteralExpr{Token: "name"},
+ Stmt: []bf.Expr{
+ &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: "foo_rule"},
+ List: []bf.Expr{
+ &bf.BinaryExpr{
+ X: &bf.LiteralExpr{Token: "name"},
Op: "=",
- Y: &bzl.StringExpr{Value: "bar"},
+ Y: &bf.StringExpr{Value: "bar"},
},
},
},
@@ -79,7 +79,7 @@
t.Errorf("ioutil.ReadFile(%q) failed with %v; want success", stubFile.Path, err)
return
}
- if got, want := string(buf), bzl.FormatString(stubFile); got != want {
+ if got, want := string(buf), bf.FormatString(stubFile); got != want {
t.Errorf("buf = %q; want %q", got, want)
}
}
diff --git a/go/tools/gazelle/gazelle/main.go b/go/tools/gazelle/gazelle/main.go
index 6a61c65..fa77540 100644
--- a/go/tools/gazelle/gazelle/main.go
+++ b/go/tools/gazelle/gazelle/main.go
@@ -27,7 +27,7 @@
"path/filepath"
"strings"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
"github.com/bazelbuild/rules_go/go/tools/gazelle/merger"
"github.com/bazelbuild/rules_go/go/tools/gazelle/packages"
@@ -35,7 +35,7 @@
"github.com/bazelbuild/rules_go/go/tools/gazelle/wspace"
)
-type emitFunc func(*config.Config, *bzl.File) error
+type emitFunc func(*config.Config, *bf.File) error
var modeFromName = map[string]emitFunc{
"print": printFile,
@@ -51,7 +51,7 @@
if c.RepoRoot == dir {
shouldProcessRoot = true
}
- packages.Walk(c, dir, func(pkg *packages.Package, oldFile *bzl.File) {
+ packages.Walk(c, dir, func(pkg *packages.Package, oldFile *bf.File) {
if pkg.Rel == "" {
didProcessRoot = true
}
@@ -62,7 +62,7 @@
// We did not process a package at the repository root. We need to put
// a go_prefix rule there, even if there are no .go files in that directory.
pkg := &packages.Package{Dir: c.RepoRoot}
- var oldFile *bzl.File
+ var oldFile *bf.File
var oldData []byte
oldPath, err := findBuildFile(c, c.RepoRoot)
if os.IsNotExist(err) {
@@ -77,7 +77,7 @@
log.Print(err)
return
}
- oldFile, err = bzl.Parse(oldPath, oldData)
+ oldFile, err = bf.Parse(oldPath, oldData)
if err != nil {
log.Print(err)
return
@@ -88,12 +88,12 @@
}
}
-func processPackage(c *config.Config, g rules.Generator, emit emitFunc, pkg *packages.Package, oldFile *bzl.File) {
+func processPackage(c *config.Config, g rules.Generator, emit emitFunc, pkg *packages.Package, oldFile *bf.File) {
genFile := g.Generate(pkg)
if oldFile == nil {
// No existing file, so no merge required.
- bzl.Rewrite(genFile, nil) // have buildifier 'format' our rules.
+ bf.Rewrite(genFile, nil) // have buildifier 'format' our rules.
if err := emit(c, genFile); err != nil {
log.Print(err)
}
@@ -102,7 +102,7 @@
// Existing file, so merge and replace the old one.
mergedFile := merger.MergeWithExisting(genFile, oldFile)
- bzl.Rewrite(mergedFile, nil) // have buildifier 'format' our rules.
+ bf.Rewrite(mergedFile, nil) // have buildifier 'format' our rules.
if err := emit(c, mergedFile); err != nil {
log.Print(err)
return
@@ -267,16 +267,16 @@
if err != nil {
return "", err
}
- f, err := bzl.Parse(p, b)
+ f, err := bf.Parse(p, b)
if err != nil {
return "", err
}
for _, s := range f.Stmt {
- c, ok := s.(*bzl.CallExpr)
+ c, ok := s.(*bf.CallExpr)
if !ok {
continue
}
- l, ok := c.X.(*bzl.LiteralExpr)
+ l, ok := c.X.(*bf.LiteralExpr)
if !ok {
continue
}
@@ -286,7 +286,7 @@
if len(c.List) != 1 {
return "", fmt.Errorf("found go_prefix(%v) with too many args", c.List)
}
- v, ok := c.List[0].(*bzl.StringExpr)
+ v, ok := c.List[0].(*bf.StringExpr)
if !ok {
return "", fmt.Errorf("found go_prefix(%v) which is not a string", c.List)
}
diff --git a/go/tools/gazelle/gazelle/print.go b/go/tools/gazelle/gazelle/print.go
index ba31d44..b0ac7ac 100644
--- a/go/tools/gazelle/gazelle/print.go
+++ b/go/tools/gazelle/gazelle/print.go
@@ -18,11 +18,11 @@
import (
"os"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
)
-func printFile(c *config.Config, f *bzl.File) error {
- _, err := os.Stdout.Write(bzl.Format(f))
+func printFile(c *config.Config, f *bf.File) error {
+ _, err := os.Stdout.Write(bf.Format(f))
return err
}
diff --git a/go/tools/gazelle/merger/merger.go b/go/tools/gazelle/merger/merger.go
index 598b99f..c5e6f3a 100644
--- a/go/tools/gazelle/merger/merger.go
+++ b/go/tools/gazelle/merger/merger.go
@@ -22,7 +22,7 @@
"sort"
"strings"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
)
const (
@@ -47,7 +47,7 @@
// If "oldFile" is nil, "genFile" will be returned. If "oldFile" contains
// a "# gazelle:ignore" comment, nil will be returned. If an error occurs,
// it will be logged, and nil will be returned.
-func MergeWithExisting(genFile, oldFile *bzl.File) *bzl.File {
+func MergeWithExisting(genFile, oldFile *bf.File) *bf.File {
if oldFile == nil {
return genFile
}
@@ -56,14 +56,14 @@
}
mergedFile := *oldFile
- mergedFile.Stmt = make([]bzl.Expr, len(oldFile.Stmt))
+ mergedFile.Stmt = make([]bf.Expr, len(oldFile.Stmt))
for i := range oldFile.Stmt {
mergedFile.Stmt[i] = oldFile.Stmt[i]
}
- var newStmt []bzl.Expr
+ var newStmt []bf.Expr
for _, s := range genFile.Stmt {
- genRule, ok := s.(*bzl.CallExpr)
+ genRule, ok := s.(*bf.CallExpr)
if !ok {
log.Panicf("got %v expected only CallExpr in %q", s, genFile.Path)
}
@@ -73,7 +73,7 @@
continue
}
- var mergedRule bzl.Expr
+ var mergedRule bf.Expr
if kind(oldRule) == "load" {
mergedRule = mergeLoad(genRule, oldRule, oldFile)
} else {
@@ -88,19 +88,19 @@
// merge combines information from gen and old and returns an updated rule.
// Both rules must be non-nil and must have the same kind and same name.
-func mergeRule(gen, old *bzl.CallExpr) *bzl.CallExpr {
- genRule := bzl.Rule{Call: gen}
- oldRule := bzl.Rule{Call: old}
+func mergeRule(gen, old *bf.CallExpr) *bf.CallExpr {
+ genRule := bf.Rule{Call: gen}
+ oldRule := bf.Rule{Call: old}
merged := *old
merged.List = nil
- mergedRule := bzl.Rule{Call: &merged}
+ mergedRule := bf.Rule{Call: &merged}
// Copy unnamed arguments from the old rule without merging. The only rule
// generated with unnamed arguments is go_prefix, which we currently
// leave in place.
// TODO: maybe gazelle should allow the prefix to be changed.
for _, a := range old.List {
- if b, ok := a.(*bzl.BinaryExpr); ok && b.Op == "=" {
+ if b, ok := a.(*bf.BinaryExpr); ok && b.Op == "=" {
break
}
merged.List = append(merged.List, a)
@@ -152,8 +152,8 @@
//
// An error is returned if the expressions can't be merged, for example
// because they are not in one of the above formats.
-func mergeExpr(gen, old bzl.Expr) (bzl.Expr, error) {
- if _, ok := gen.(*bzl.StringExpr); ok {
+func mergeExpr(gen, old bf.Expr) (bf.Expr, error) {
+ if _, ok := gen.(*bf.StringExpr); ok {
if shouldKeep(old) {
return old, nil
}
@@ -175,11 +175,11 @@
return nil, err
}
- var mergedSelect bzl.Expr
+ var mergedSelect bf.Expr
if mergedDict != nil {
- mergedSelect = &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: "select"},
- List: []bzl.Expr{mergedDict},
+ mergedSelect = &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: "select"},
+ List: []bf.Expr{mergedDict},
}
}
@@ -190,7 +190,7 @@
return mergedList, nil
}
mergedList.ForceMultiLine = true
- return &bzl.BinaryExpr{
+ return &bf.BinaryExpr{
X: mergedList,
Op: "+",
Y: mergedSelect,
@@ -200,36 +200,36 @@
// exprListAndDict matches an expression and attempts to extract either a list
// of expressions, a call to select with a dictionary, or both.
// An error is returned if the expression could not be matched.
-func exprListAndDict(expr bzl.Expr) (*bzl.ListExpr, *bzl.DictExpr, error) {
+func exprListAndDict(expr bf.Expr) (*bf.ListExpr, *bf.DictExpr, error) {
if expr == nil {
return nil, nil, nil
}
switch expr := expr.(type) {
- case *bzl.ListExpr:
+ case *bf.ListExpr:
return expr, nil, nil
- case *bzl.CallExpr:
- if x, ok := expr.X.(*bzl.LiteralExpr); ok && x.Token == "select" && len(expr.List) == 1 {
- if d, ok := expr.List[0].(*bzl.DictExpr); ok {
+ case *bf.CallExpr:
+ if x, ok := expr.X.(*bf.LiteralExpr); ok && x.Token == "select" && len(expr.List) == 1 {
+ if d, ok := expr.List[0].(*bf.DictExpr); ok {
return nil, d, nil
}
}
- case *bzl.BinaryExpr:
+ case *bf.BinaryExpr:
if expr.Op != "+" {
return nil, nil, fmt.Errorf("expression could not be matched: unknown operator: %s", expr.Op)
}
- l, ok := expr.X.(*bzl.ListExpr)
+ l, ok := expr.X.(*bf.ListExpr)
if !ok {
return nil, nil, fmt.Errorf("expression could not be matched: left operand not a list")
}
- call, ok := expr.Y.(*bzl.CallExpr)
+ call, ok := expr.Y.(*bf.CallExpr)
if !ok || len(call.List) != 1 {
return nil, nil, fmt.Errorf("expression could not be matched: right operand not a call with one argument")
}
- x, ok := call.X.(*bzl.LiteralExpr)
+ x, ok := call.X.(*bf.LiteralExpr)
if !ok || x.Token != "select" {
return nil, nil, fmt.Errorf("expression could not be matched: right operand not a call to select")
}
- d, ok := call.List[0].(*bzl.DictExpr)
+ d, ok := call.List[0].(*bf.DictExpr)
if !ok {
return nil, nil, fmt.Errorf("expression could not be matched: argument to right operand not a dict")
}
@@ -238,18 +238,18 @@
return nil, nil, fmt.Errorf("expression could not be matched")
}
-func mergeList(gen, old *bzl.ListExpr) *bzl.ListExpr {
+func mergeList(gen, old *bf.ListExpr) *bf.ListExpr {
if old == nil {
return gen
}
if gen == nil {
- gen = &bzl.ListExpr{List: []bzl.Expr{}}
+ gen = &bf.ListExpr{List: []bf.Expr{}}
}
// Build a list of elements from the old list with "# keep" comments. We
// must not duplicate these elements, since duplicate elements will be
// removed when we rewrite the AST.
- var merged []bzl.Expr
+ var merged []bf.Expr
kept := make(map[string]bool)
for _, v := range old.List {
if shouldKeep(v) {
@@ -270,15 +270,15 @@
if len(merged) == 0 {
return nil
}
- return &bzl.ListExpr{List: merged}
+ return &bf.ListExpr{List: merged}
}
-func mergeDict(gen, old *bzl.DictExpr) (*bzl.DictExpr, error) {
+func mergeDict(gen, old *bf.DictExpr) (*bf.DictExpr, error) {
if old == nil {
return gen, nil
}
if gen == nil {
- gen = &bzl.DictExpr{List: []bzl.Expr{}}
+ gen = &bf.DictExpr{List: []bf.Expr{}}
}
var entries []*dictEntry
@@ -319,7 +319,7 @@
// Keep the default case, even if it's empty.
haveDefault = true
if e.mergedValue == nil {
- e.mergedValue = &bzl.ListExpr{}
+ e.mergedValue = &bf.ListExpr{}
}
} else if e.mergedValue != nil {
keys = append(keys, e.key)
@@ -334,41 +334,41 @@
keys = append(keys, "//conditions:default")
}
- mergedEntries := make([]bzl.Expr, len(keys))
+ mergedEntries := make([]bf.Expr, len(keys))
for i, k := range keys {
e := entryMap[k]
- mergedEntries[i] = &bzl.KeyValueExpr{
- Key: &bzl.StringExpr{Value: e.key},
+ mergedEntries[i] = &bf.KeyValueExpr{
+ Key: &bf.StringExpr{Value: e.key},
Value: e.mergedValue,
}
}
- return &bzl.DictExpr{List: mergedEntries, ForceMultiLine: true}, nil
+ return &bf.DictExpr{List: mergedEntries, ForceMultiLine: true}, nil
}
type dictEntry struct {
key string
- oldValue, genValue, mergedValue *bzl.ListExpr
+ oldValue, genValue, mergedValue *bf.ListExpr
}
-func dictEntryKeyValue(e bzl.Expr) (string, *bzl.ListExpr, error) {
- kv, ok := e.(*bzl.KeyValueExpr)
+func dictEntryKeyValue(e bf.Expr) (string, *bf.ListExpr, error) {
+ kv, ok := e.(*bf.KeyValueExpr)
if !ok {
return "", nil, fmt.Errorf("dict entry was not a key-value pair: %#v", e)
}
- k, ok := kv.Key.(*bzl.StringExpr)
+ k, ok := kv.Key.(*bf.StringExpr)
if !ok {
return "", nil, fmt.Errorf("dict key was not string: %#v", kv.Key)
}
- v, ok := kv.Value.(*bzl.ListExpr)
+ v, ok := kv.Value.(*bf.ListExpr)
if !ok {
return "", nil, fmt.Errorf("dict value was not list: %#v", kv.Value)
}
return k.Value, v, nil
}
-func mergeLoad(gen, old *bzl.CallExpr, oldfile *bzl.File) *bzl.CallExpr {
- vals := make(map[string]bzl.Expr)
+func mergeLoad(gen, old *bf.CallExpr, oldfile *bf.File) *bf.CallExpr {
+ vals := make(map[string]bf.Expr)
for _, v := range gen.List[1:] {
vals[stringValue(v)] = v
}
@@ -394,7 +394,7 @@
// shouldIgnore checks whether "gazelle:ignore" appears at the beginning of
// a comment before or after any top-level statement in the file.
-func shouldIgnore(oldFile *bzl.File) bool {
+func shouldIgnore(oldFile *bf.File) bool {
for _, s := range oldFile.Stmt {
for _, c := range s.Comment().After {
if strings.HasPrefix(c.Token, gazelleIgnore) {
@@ -412,12 +412,12 @@
// shouldKeep returns whether an expression from the original file should be
// preserved. This is true if it has a trailing comment that starts with "keep".
-func shouldKeep(e bzl.Expr) bool {
+func shouldKeep(e bf.Expr) bool {
c := e.Comment()
return len(c.Suffix) > 0 && strings.HasPrefix(c.Suffix[0].Token, keep)
}
-func ruleUsed(rule string, oldfile *bzl.File) bool {
+func ruleUsed(rule string, oldfile *bf.File) bool {
return len(oldfile.Rules(rule)) != 0
}
@@ -425,7 +425,7 @@
// i.e. two 'go_library(name = "foo", ...)' are considered matches
// despite the values of the other fields.
// exception: if c is a 'load' statement, the match is done on the first value.
-func match(f *bzl.File, c *bzl.CallExpr) (int, *bzl.CallExpr) {
+func match(f *bf.File, c *bf.CallExpr) (int, *bf.CallExpr) {
var m matcher
if kind := kind(c); kind == "load" {
if len(c.List) == 0 {
@@ -436,7 +436,7 @@
m = &nameMatcher{kind, name(c)}
}
for i, s := range f.Stmt {
- other, ok := s.(*bzl.CallExpr)
+ other, ok := s.(*bf.CallExpr)
if !ok {
continue
}
@@ -448,14 +448,14 @@
}
type matcher interface {
- match(c *bzl.CallExpr) bool
+ match(c *bf.CallExpr) bool
}
type nameMatcher struct {
kind, name string
}
-func (m *nameMatcher) match(c *bzl.CallExpr) bool {
+func (m *nameMatcher) match(c *bf.CallExpr) bool {
return m.kind == kind(c) && m.name == name(c)
}
@@ -463,20 +463,20 @@
load string
}
-func (m *loadMatcher) match(c *bzl.CallExpr) bool {
+func (m *loadMatcher) match(c *bf.CallExpr) bool {
return kind(c) == "load" && len(c.List) > 0 && m.load == stringValue(c.List[0])
}
-func kind(c *bzl.CallExpr) string {
- return (&bzl.Rule{c}).Kind()
+func kind(c *bf.CallExpr) string {
+ return (&bf.Rule{c}).Kind()
}
-func name(c *bzl.CallExpr) string {
- return (&bzl.Rule{c}).Name()
+func name(c *bf.CallExpr) string {
+ return (&bf.Rule{c}).Name()
}
-func stringValue(e bzl.Expr) string {
- s, ok := e.(*bzl.StringExpr)
+func stringValue(e bf.Expr) string {
+ s, ok := e.(*bf.StringExpr)
if !ok {
return ""
}
diff --git a/go/tools/gazelle/merger/merger_test.go b/go/tools/gazelle/merger/merger_test.go
index f18a76a..bf070f9 100644
--- a/go/tools/gazelle/merger/merger_test.go
+++ b/go/tools/gazelle/merger/merger_test.go
@@ -3,7 +3,7 @@
import (
"testing"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
)
// should fix
@@ -428,12 +428,12 @@
func TestMergeWithExisting(t *testing.T) {
for _, tc := range testCases {
- genFile, err := bzl.Parse("current", []byte(tc.current))
+ genFile, err := bf.Parse("current", []byte(tc.current))
if err != nil {
t.Errorf("%s: %v", tc.desc, err)
continue
}
- oldFile, err := bzl.Parse("previous", []byte(tc.previous))
+ oldFile, err := bf.Parse("previous", []byte(tc.previous))
if err != nil {
t.Errorf("%s: %v", tc.desc, err)
continue
@@ -455,15 +455,15 @@
want = want[1:]
}
- if got := string(bzl.Format(mergedFile)); got != want {
+ if got := string(bf.Format(mergedFile)); got != want {
t.Errorf("%s: got %s; want %s", tc.desc, got, want)
}
}
}
func TestMergeWithExistingDifferentName(t *testing.T) {
- oldFile := &bzl.File{Path: "BUILD"}
- genFile := &bzl.File{Path: "BUILD.bazel"}
+ oldFile := &bf.File{Path: "BUILD"}
+ genFile := &bf.File{Path: "BUILD.bazel"}
mergedFile := MergeWithExisting(genFile, oldFile)
if got, want := mergedFile.Path, oldFile.Path; got != want {
t.Errorf("got %q; want %q", got, want)
diff --git a/go/tools/gazelle/packages/walk.go b/go/tools/gazelle/packages/walk.go
index d374152..24f9430 100644
--- a/go/tools/gazelle/packages/walk.go
+++ b/go/tools/gazelle/packages/walk.go
@@ -25,12 +25,12 @@
"sort"
"strings"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
)
// A WalkFunc is a callback called by Walk for each package.
-type WalkFunc func(pkg *Package, oldFile *bzl.File)
+type WalkFunc func(pkg *Package, oldFile *bf.File)
// Walk walks through directories under "root".
// It calls back "f" for each package. If an existing BUILD file is present
@@ -56,7 +56,7 @@
visit = func(path string) bool {
// Look for an existing BUILD file. Directives in this file may influence
// the rest of the process.
- var oldFile *bzl.File
+ var oldFile *bf.File
haveError := false
for _, base := range c.ValidBuildFileNames {
oldPath := filepath.Join(path, base)
@@ -76,7 +76,7 @@
haveError = true
continue
}
- oldFile, err = bzl.Parse(oldPath, oldData)
+ oldFile, err = bf.Parse(oldPath, oldData)
if err != nil {
log.Print(err)
haveError = true
@@ -154,7 +154,7 @@
// name matches the directory base name will be returned. If there is no such
// package or if an error occurs, an error will be logged, and nil will be
// returned.
-func buildPackage(c *config.Config, dir string, oldFile *bzl.File, goFiles, genGoFiles, otherFiles []string, hasTestdata bool) *Package {
+func buildPackage(c *config.Config, dir string, oldFile *bf.File, goFiles, genGoFiles, otherFiles []string, hasTestdata bool) *Package {
rel, err := filepath.Rel(c.RepoRoot, dir)
if err != nil {
log.Print(err)
@@ -282,16 +282,16 @@
return name
}
-func findGenGoFiles(f *bzl.File, excluded map[string]bool) []string {
+func findGenGoFiles(f *bf.File, excluded map[string]bool) []string {
var strs []string
for _, r := range f.Rules("") {
for _, key := range []string{"out", "outs"} {
switch e := r.Attr(key).(type) {
- case *bzl.StringExpr:
+ case *bf.StringExpr:
strs = append(strs, e.Value)
- case *bzl.ListExpr:
+ case *bf.ListExpr:
for _, elem := range e.List {
- if s, ok := elem.(*bzl.StringExpr); ok {
+ if s, ok := elem.(*bf.StringExpr); ok {
strs = append(strs, s.Value)
}
}
@@ -310,7 +310,7 @@
const gazelleExclude = "# gazelle:exclude " // marker in a BUILD file to exclude source files.
-func findExcludedFiles(f *bzl.File) map[string]bool {
+func findExcludedFiles(f *bf.File) map[string]bool {
excluded := make(map[string]bool)
for _, s := range f.Stmt {
comments := append(s.Comment().Before, s.Comment().After...)
diff --git a/go/tools/gazelle/packages/walk_test.go b/go/tools/gazelle/packages/walk_test.go
index 041df4f..8de3bc4 100644
--- a/go/tools/gazelle/packages/walk_test.go
+++ b/go/tools/gazelle/packages/walk_test.go
@@ -23,7 +23,7 @@
"strings"
"testing"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
"github.com/bazelbuild/rules_go/go/tools/gazelle/packages"
)
@@ -81,7 +81,7 @@
ValidBuildFileNames: config.DefaultValidBuildFileNames,
}
var pkgs []*packages.Package
- packages.Walk(c, dir, func(pkg *packages.Package, _ *bzl.File) {
+ packages.Walk(c, dir, func(pkg *packages.Package, _ *bf.File) {
pkgs = append(pkgs, pkg)
})
return pkgs
diff --git a/go/tools/gazelle/rules/construct.go b/go/tools/gazelle/rules/construct.go
index 95135c5..4e333db 100644
--- a/go/tools/gazelle/rules/construct.go
+++ b/go/tools/gazelle/rules/construct.go
@@ -21,7 +21,7 @@
"reflect"
"sort"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/packages"
)
@@ -35,69 +35,69 @@
excludes []string
}
-func newRule(kind string, args []interface{}, kwargs []keyvalue) *bzl.Rule {
- var list []bzl.Expr
+func newRule(kind string, args []interface{}, kwargs []keyvalue) *bf.Rule {
+ var list []bf.Expr
for _, arg := range args {
list = append(list, newValue(arg))
}
for _, arg := range kwargs {
expr := newValue(arg.value)
- list = append(list, &bzl.BinaryExpr{
- X: &bzl.LiteralExpr{Token: arg.key},
+ list = append(list, &bf.BinaryExpr{
+ X: &bf.LiteralExpr{Token: arg.key},
Op: "=",
Y: expr,
})
}
- return &bzl.Rule{
- Call: &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: kind},
+ return &bf.Rule{
+ Call: &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: kind},
List: list,
},
}
}
// newValue converts a Go value into the corresponding expression in Bazel BUILD file.
-func newValue(val interface{}) bzl.Expr {
+func newValue(val interface{}) bf.Expr {
rv := reflect.ValueOf(val)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- return &bzl.LiteralExpr{Token: fmt.Sprintf("%d", val)}
+ return &bf.LiteralExpr{Token: fmt.Sprintf("%d", val)}
case reflect.Float32, reflect.Float64:
- return &bzl.LiteralExpr{Token: fmt.Sprintf("%f", val)}
+ return &bf.LiteralExpr{Token: fmt.Sprintf("%f", val)}
case reflect.String:
- return &bzl.StringExpr{Value: val.(string)}
+ return &bf.StringExpr{Value: val.(string)}
case reflect.Slice, reflect.Array:
- var list []bzl.Expr
+ var list []bf.Expr
for i := 0; i < rv.Len(); i++ {
elem := newValue(rv.Index(i).Interface())
list = append(list, elem)
}
- return &bzl.ListExpr{List: list}
+ return &bf.ListExpr{List: list}
case reflect.Map:
rkeys := rv.MapKeys()
sort.Sort(byString(rkeys))
- args := make([]bzl.Expr, len(rkeys))
+ args := make([]bf.Expr, len(rkeys))
for i, rk := range rkeys {
- k := &bzl.StringExpr{Value: rk.String()}
+ k := &bf.StringExpr{Value: rk.String()}
v := newValue(rv.MapIndex(rk).Interface())
- if l, ok := v.(*bzl.ListExpr); ok {
+ if l, ok := v.(*bf.ListExpr); ok {
l.ForceMultiLine = true
}
- args[i] = &bzl.KeyValueExpr{Key: k, Value: v}
+ args[i] = &bf.KeyValueExpr{Key: k, Value: v}
}
- args = append(args, &bzl.KeyValueExpr{
- Key: &bzl.StringExpr{Value: "//conditions:default"},
- Value: &bzl.ListExpr{},
+ args = append(args, &bf.KeyValueExpr{
+ Key: &bf.StringExpr{Value: "//conditions:default"},
+ Value: &bf.ListExpr{},
})
- sel := &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: "select"},
- List: []bzl.Expr{&bzl.DictExpr{List: args, ForceMultiLine: true}},
+ sel := &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: "select"},
+ List: []bf.Expr{&bf.DictExpr{List: args, ForceMultiLine: true}},
}
return sel
@@ -105,16 +105,16 @@
switch val := val.(type) {
case globvalue:
patternsValue := newValue(val.patterns)
- globArgs := []bzl.Expr{patternsValue}
+ globArgs := []bf.Expr{patternsValue}
if len(val.excludes) > 0 {
excludesValue := newValue(val.excludes)
- globArgs = append(globArgs, &bzl.KeyValueExpr{
- Key: &bzl.StringExpr{Value: "excludes"},
+ globArgs = append(globArgs, &bf.KeyValueExpr{
+ Key: &bf.StringExpr{Value: "excludes"},
Value: excludesValue,
})
}
- return &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: "glob"},
+ return &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: "glob"},
List: globArgs,
}
@@ -129,10 +129,10 @@
return sel
}
- if genList, ok := gen.(*bzl.ListExpr); ok {
+ if genList, ok := gen.(*bf.ListExpr); ok {
genList.ForceMultiLine = true
}
- return &bzl.BinaryExpr{X: gen, Op: "+", Y: sel}
+ return &bf.BinaryExpr{X: gen, Op: "+", Y: sel}
}
}
diff --git a/go/tools/gazelle/rules/generator.go b/go/tools/gazelle/rules/generator.go
index 36d40b5..193f464 100644
--- a/go/tools/gazelle/rules/generator.go
+++ b/go/tools/gazelle/rules/generator.go
@@ -21,7 +21,7 @@
"path/filepath"
"strings"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
"github.com/bazelbuild/rules_go/go/tools/gazelle/packages"
)
@@ -30,7 +30,7 @@
// goRulesBzl is the label of the Skylark file which provides Go rules
goRulesBzl = "@io_bazel_rules_go//go:def.bzl"
// defaultLibName is the name of the default go_library rule in a Go
- // package directory. It must be consistent to DEFAULT_LIB in go/private/common.bzl.
+ // package directory. It must be consistent to DEFAULT_LIB in go/private/common.bf.
defaultLibName = "go_default_library"
// defaultTestName is a name of an internal test corresponding to
// defaultLibName. It does not need to be consistent to something but it
@@ -53,7 +53,7 @@
// "load" statements necessary for the rule constructors. If this is the
// top-level package in the repository, the file will contain a
// "go_prefix" rule.
- Generate(pkg *packages.Package) *bzl.File
+ Generate(pkg *packages.Package) *bf.File
}
func NewGenerator(c *config.Config) Generator {
@@ -89,8 +89,8 @@
r labelResolver
}
-func (g *generator) Generate(pkg *packages.Package) *bzl.File {
- f := &bzl.File{
+func (g *generator) Generate(pkg *packages.Package) *bf.File {
+ f := &bf.File{
Path: filepath.Join(pkg.Dir, g.c.DefaultBuildFileName()),
}
rs := g.generateRules(pkg)
@@ -103,8 +103,8 @@
return f
}
-func (g *generator) generateRules(pkg *packages.Package) []*bzl.Rule {
- var rules []*bzl.Rule
+func (g *generator) generateRules(pkg *packages.Package) []*bf.Rule {
+ var rules []*bf.Rule
if pkg.Rel == "" {
rules = append(rules, newRule("go_prefix", []interface{}{g.c.GoPrefix}, nil))
}
@@ -138,7 +138,7 @@
return rules
}
-func (g *generator) generateBin(pkg *packages.Package, library string) *bzl.Rule {
+func (g *generator) generateBin(pkg *packages.Package, library string) *bf.Rule {
if !pkg.IsCommand() || pkg.Binary.Sources.IsEmpty() && library == "" {
return nil
}
@@ -147,7 +147,7 @@
return g.generateRule(pkg.Rel, "go_binary", name, visibility, library, false, pkg.Binary)
}
-func (g *generator) generateLib(pkg *packages.Package, cgoName string) (string, *bzl.Rule) {
+func (g *generator) generateLib(pkg *packages.Package, cgoName string) (string, *bf.Rule) {
if !pkg.Library.HasGo() && cgoName == "" {
return "", nil
}
@@ -165,7 +165,7 @@
return name, rule
}
-func (g *generator) generateCgoLib(pkg *packages.Package) (string, *bzl.Rule) {
+func (g *generator) generateCgoLib(pkg *packages.Package) (string, *bf.Rule) {
if !pkg.CgoLibrary.HasGo() {
return "", nil
}
@@ -190,7 +190,7 @@
// filegroup is a small hack for directories with pre-generated .pb.go files
// and also source .proto files. This creates a filegroup for the .proto in
// addition to the usual go_library for the .pb.go files.
-func (g *generator) filegroup(pkg *packages.Package) *bzl.Rule {
+func (g *generator) filegroup(pkg *packages.Package) *bf.Rule {
if !pkg.HasPbGo || len(pkg.Protos) == 0 {
return nil
}
@@ -201,7 +201,7 @@
})
}
-func (g *generator) generateTest(pkg *packages.Package, library string) *bzl.Rule {
+func (g *generator) generateTest(pkg *packages.Package, library string) *bf.Rule {
if !pkg.Test.HasGo() {
return nil
}
@@ -216,7 +216,7 @@
return g.generateRule(pkg.Rel, "go_test", name, "", library, pkg.HasTestdata, pkg.Test)
}
-func (g *generator) generateXTest(pkg *packages.Package, library string) *bzl.Rule {
+func (g *generator) generateXTest(pkg *packages.Package, library string) *bf.Rule {
if !pkg.XTest.HasGo() {
return nil
}
@@ -231,8 +231,8 @@
return g.generateRule(pkg.Rel, "go_test", name, "", "", pkg.HasTestdata, pkg.XTest)
}
-func (g *generator) generateRule(rel, kind, name, visibility, library string, hasTestdata bool, target packages.Target) *bzl.Rule {
- // Construct attrs in the same order that bzl.Rewrite uses. See
+func (g *generator) generateRule(rel, kind, name, visibility, library string, hasTestdata bool, target packages.Target) *bf.Rule {
+ // Construct attrs in the same order that bf.Rewrite uses. See
// namePriority in github.com/bazelbuild/buildtools/build/rewrite.go.
attrs := []keyvalue{
{"name", name},
@@ -263,7 +263,7 @@
return newRule(kind, nil, attrs)
}
-func (g *generator) generateLoad(rs []*bzl.Rule) bzl.Expr {
+func (g *generator) generateLoad(rs []*bf.Rule) bf.Expr {
loadableKinds := []string{
// keep sorted
"cgo_library",
@@ -277,18 +277,18 @@
for _, r := range rs {
kinds[r.Kind()] = true
}
- args := make([]bzl.Expr, 0, len(kinds)+1)
- args = append(args, &bzl.StringExpr{Value: goRulesBzl})
+ args := make([]bf.Expr, 0, len(kinds)+1)
+ args = append(args, &bf.StringExpr{Value: goRulesBzl})
for _, k := range loadableKinds {
if kinds[k] {
- args = append(args, &bzl.StringExpr{Value: k})
+ args = append(args, &bf.StringExpr{Value: k})
}
}
if len(args) == 1 {
return nil
}
- return &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: "load"},
+ return &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: "load"},
List: args,
ForceCompact: true,
}
diff --git a/go/tools/gazelle/rules/generator_test.go b/go/tools/gazelle/rules/generator_test.go
index c70a839..c6044bf 100644
--- a/go/tools/gazelle/rules/generator_test.go
+++ b/go/tools/gazelle/rules/generator_test.go
@@ -20,7 +20,7 @@
"path/filepath"
"testing"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/config"
"github.com/bazelbuild/rules_go/go/tools/gazelle/packages"
"github.com/bazelbuild/rules_go/go/tools/gazelle/rules"
@@ -41,7 +41,7 @@
func packageFromDir(c *config.Config, dir string) *packages.Package {
var pkg *packages.Package
- packages.Walk(c, dir, func(p *packages.Package, _ *bzl.File) {
+ packages.Walk(c, dir, func(p *packages.Package, _ *bf.File) {
if p.Dir == dir {
pkg = p
}
@@ -71,7 +71,7 @@
dir := filepath.Join(repoRoot, filepath.FromSlash(rel))
pkg := packageFromDir(c, dir)
f := g.Generate(pkg)
- got := string(bzl.Format(f))
+ got := string(bf.Format(f))
wantPath := filepath.Join(pkg.Dir, "BUILD.want")
wantBytes, err := ioutil.ReadFile(wantPath)
@@ -113,18 +113,18 @@
}
}
-func findGoPrefix(f *bzl.File) string {
+func findGoPrefix(f *bf.File) string {
for _, s := range f.Stmt {
- c, ok := s.(*bzl.CallExpr)
+ c, ok := s.(*bf.CallExpr)
if !ok {
continue
}
- x, ok := c.X.(*bzl.LiteralExpr)
+ x, ok := c.X.(*bf.LiteralExpr)
if !ok {
continue
}
if x.Token == "go_prefix" {
- return bzl.FormatString(s)
+ return bf.FormatString(s)
}
}
return ""
diff --git a/go/tools/wtool/main.go b/go/tools/wtool/main.go
index 033ecdb..5dadad5 100644
--- a/go/tools/wtool/main.go
+++ b/go/tools/wtool/main.go
@@ -26,7 +26,7 @@
"path/filepath"
"strings"
- bzl "github.com/bazelbuild/buildtools/build"
+ bf "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/rules_go/go/tools/gazelle/rules"
"github.com/bazelbuild/rules_go/go/tools/gazelle/wspace"
"golang.org/x/tools/go/vcs"
@@ -63,7 +63,7 @@
if err != nil {
return err
}
- f, err := bzl.Parse(p, b)
+ f, err := bf.Parse(p, b)
if err != nil {
return err
}
@@ -76,8 +76,8 @@
f.Stmt = append(f.Stmt, imp)
}
updateLoad(f)
- bzl.Rewrite(f, nil)
- return ioutil.WriteFile(f.Path, bzl.Format(f), 0644)
+ bf.Rewrite(f, nil)
+ return ioutil.WriteFile(f.Path, bf.Format(f), 0644)
}
func nameAndImportpath(name string) (string, string, error) {
@@ -97,7 +97,7 @@
return name, strings.Join([]string{s[1] + "." + s[0], s[2], rest}, "/"), nil
}
-func findImport(nameIn string) (bzl.Expr, error) {
+func findImport(nameIn string) (bf.Expr, error) {
name, importpath, err := nameAndImportpath(nameIn)
if err != nil {
return nil, err
@@ -117,9 +117,9 @@
if err != nil {
return nil, err
}
- return &bzl.CallExpr{
- X: &bzl.LiteralExpr{Token: "go_repository"},
- List: []bzl.Expr{
+ return &bf.CallExpr{
+ X: &bf.LiteralExpr{Token: "go_repository"},
+ List: []bf.Expr{
attr("name", name),
attr("importpath", importpath),
attr("commit", commit),
@@ -127,11 +127,11 @@
}, nil
}
-func attr(key, val string) *bzl.BinaryExpr {
- return &bzl.BinaryExpr{
- X: &bzl.LiteralExpr{Token: key},
+func attr(key, val string) *bf.BinaryExpr {
+ return &bf.BinaryExpr{
+ X: &bf.LiteralExpr{Token: key},
Op: "=",
- Y: &bzl.StringExpr{Value: val},
+ Y: &bf.StringExpr{Value: val},
}
}
@@ -158,27 +158,27 @@
return strings.Split(b.Text(), "\t")[0], nil
}
-func updateLoad(f *bzl.File) {
+func updateLoad(f *bf.File) {
for _, s := range f.Stmt {
- call, ok := s.(*bzl.CallExpr)
+ call, ok := s.(*bf.CallExpr)
if !ok || len(call.List) == 0 {
continue
}
- if x, ok := call.X.(*bzl.LiteralExpr); !ok || x.Token != "load" {
+ if x, ok := call.X.(*bf.LiteralExpr); !ok || x.Token != "load" {
continue
}
- if label, ok := call.List[0].(*bzl.StringExpr); !ok || label.Value != "@io_bazel_rules_go//go:def.bzl" {
+ if label, ok := call.List[0].(*bf.StringExpr); !ok || label.Value != "@io_bazel_rules_go//go:def.bzl" {
continue
}
haveGoRepository := false
for _, arg := range call.List[1:] {
- if sym, ok := arg.(*bzl.StringExpr); ok && sym.Value == "go_repository" {
+ if sym, ok := arg.(*bf.StringExpr); ok && sym.Value == "go_repository" {
haveGoRepository = true
break
}
}
if !haveGoRepository {
- call.List = append(call.List, &bzl.StringExpr{Value: "go_repository"})
+ call.List = append(call.List, &bf.StringExpr{Value: "go_repository"})
}
}
}