Skip to content

Commit

Permalink
gazelle: Add ability to read/write to BUILD.bazel instead of BUILD (#219
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pmbethe09 authored and wlynch committed Dec 13, 2016
1 parent 9dbc5d1 commit fee2a86
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go/tools/gazelle/gazelle/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func diffFile(file *bzl.File) error {
f, err := ioutil.TempFile("", "BUILD")
f, err := ioutil.TempFile("", *buildName)
if err != nil {
return err
}
Expand Down
36 changes: 30 additions & 6 deletions go/tools/gazelle/gazelle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ import (
)

var (
goPrefix = flag.String("go_prefix", "", "go_prefix of the target workspace")
repoRoot = flag.String("repo_root", "", "path to a directory which corresponds to go_prefix, otherwise gazelle searches for it.")
mode = flag.String("mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff")
goPrefix = flag.String("go_prefix", "", "go_prefix of the target workspace")
repoRoot = flag.String("repo_root", "", "path to a directory which corresponds to go_prefix, otherwise gazelle searches for it.")
mode = flag.String("mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff")
buildName = flag.String("build_name", "BUILD", "name of output build files to generate, defaults to 'BUILD'")

validBuildNames = map[string]bool{
"BUILD": true,
"BUILD.bazel": true,
}
)

func init() {
Expand All @@ -51,7 +57,7 @@ var modeFromName = map[string]func(*bzl.File) error{
}

func run(dirs []string, emit func(*bzl.File) error) error {
g, err := generator.New(*repoRoot, *goPrefix)
g, err := generator.New(*repoRoot, *goPrefix, *buildName)
if err != nil {
return err
}
Expand Down Expand Up @@ -120,6 +126,10 @@ func main() {
}
}

if !validBuildNames[*buildName] {
log.Fatalf("-build_name %q must be in: %v", *buildName, validBuildNames)
}

emit := modeFromName[*mode]
if emit == nil {
log.Fatalf("unrecognized mode %s", *mode)
Expand All @@ -135,9 +145,23 @@ func main() {
}
}

func readBuild(root string) (data []byte, location string, _ error) {
for n := range validBuildNames {
p := filepath.Join(root, n)
b, err := ioutil.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, "", err
}
return b, p, nil
}
return nil, "", fmt.Errorf("no build files found at %q", root)
}

func loadGoPrefix(repo string) (string, error) {
p := filepath.Join(repo, "BUILD")
b, err := ioutil.ReadFile(p)
b, p, err := readBuild(repo)
if err != nil {
return "", err
}
Expand Down
28 changes: 15 additions & 13 deletions go/tools/gazelle/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ var (

// Generator generates BUILD files for a Go repository.
type Generator struct {
repoRoot string
goPrefix string
bctx build.Context
g rules.Generator
repoRoot string
goPrefix string
buildName string
bctx build.Context
g rules.Generator
}

// New returns a new Generator which is responsible for a Go repository.
//
// "repoRoot" is a path to the root directory of the repository.
// "goPrefix" is the go_prefix corresponding to the repository root directory.
// See also https://github.com/bazelbuild/rules_go#go_prefix.
func New(repoRoot, goPrefix string) (*Generator, error) {
func New(repoRoot, goPrefix, buildName string) (*Generator, error) {
bctx := build.Default
// Ignore source files in $GOROOT and $GOPATH
bctx.GOROOT = ""
Expand All @@ -66,10 +67,11 @@ func New(repoRoot, goPrefix string) (*Generator, error) {
return nil, err
}
return &Generator{
repoRoot: filepath.Clean(repoRoot),
goPrefix: goPrefix,
bctx: bctx,
g: rules.NewGenerator(goPrefix),
repoRoot: filepath.Clean(repoRoot),
goPrefix: goPrefix,
buildName: buildName,
bctx: bctx,
g: rules.NewGenerator(goPrefix),
}, nil
}

Expand Down Expand Up @@ -99,7 +101,7 @@ func (g *Generator) Generate(dir string) ([]*bzl.File, error) {
if len(files) == 0 && rel != "" {
// "dir" was not a buildable Go package but still need a BUILD file
// for go_prefix.
files = append(files, emptyToplevel(g.goPrefix))
files = append(files, emptyToplevel(g.goPrefix, g.buildName))
}

file, err := g.generateOne(rel, pkg)
Expand All @@ -116,9 +118,9 @@ func (g *Generator) Generate(dir string) ([]*bzl.File, error) {
return files, nil
}

func emptyToplevel(goPrefix string) *bzl.File {
func emptyToplevel(goPrefix, buildName string) *bzl.File {
return &bzl.File{
Path: "BUILD",
Path: buildName,
Stmt: []bzl.Expr{
loadExpr("go_prefix"),
&bzl.CallExpr{
Expand All @@ -137,7 +139,7 @@ func (g *Generator) generateOne(rel string, pkg *build.Package) (*bzl.File, erro
return nil, err
}

file := &bzl.File{Path: filepath.Join(rel, "BUILD")}
file := &bzl.File{Path: filepath.Join(rel, g.buildName)}
for _, r := range rs {
file.Stmt = append(file.Stmt, r.Call)
}
Expand Down
28 changes: 18 additions & 10 deletions go/tools/gazelle/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ var (
)

func TestGenerator(t *testing.T) {
testGenerator(t, "BUILD")
}

func TestGeneratorDotBazel(t *testing.T) {
testGenerator(t, "BUILD.bazel")
}

func testGenerator(t *testing.T, buildName string) {
stub := stubRuleGen{
goFiles: make(map[string][]string),
cFiles: make(map[string][]string),
Expand Down Expand Up @@ -153,7 +161,7 @@ func TestGenerator(t *testing.T) {
}

repo := filepath.Join(testdata.Dir(), "repo")
g, err := New(repo, "example.com/repo")
g, err := New(repo, "example.com/repo", buildName)
if err != nil {
t.Errorf(`New(%q, "example.com/repo") failed with %v; want success`, repo, err)
return
Expand All @@ -172,7 +180,7 @@ func TestGenerator(t *testing.T) {

want := []*bzl.File{
{
Path: "BUILD",
Path: buildName,
Stmt: []bzl.Expr{
loadExpr("go_prefix"),
&bzl.CallExpr{
Expand All @@ -184,38 +192,38 @@ func TestGenerator(t *testing.T) {
},
},
{
Path: "lib/BUILD",
Path: "lib/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_prefix", "go_library"),
stub.fixtures["lib"][0].Call,
stub.fixtures["lib"][1].Call,
},
},
{
Path: "lib/internal/deep/BUILD",
Path: "lib/internal/deep/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library", "go_test"),
stub.fixtures["lib/internal/deep"][0].Call,
stub.fixtures["lib/internal/deep"][1].Call,
},
},
{
Path: "lib/relativeimporter/BUILD",
Path: "lib/relativeimporter/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library"),
stub.fixtures["lib/relativeimporter"][0].Call,
},
},
{
Path: "bin/BUILD",
Path: "bin/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library", "go_binary"),
stub.fixtures["bin"][0].Call,
stub.fixtures["bin"][1].Call,
},
},
{
Path: "bin_with_tests/BUILD",
Path: "bin_with_tests/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library", "go_binary", "go_test"),
stub.fixtures["bin_with_tests"][0].Call,
Expand All @@ -224,7 +232,7 @@ func TestGenerator(t *testing.T) {
},
},
{
Path: "cgolib/BUILD",
Path: "cgolib/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library", "go_test", "cgo_library"),
stub.fixtures["cgolib"][0].Call,
Expand All @@ -233,7 +241,7 @@ func TestGenerator(t *testing.T) {
},
},
{
Path: "cgolib_with_build_tags/BUILD",
Path: "cgolib_with_build_tags/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library", "go_test", "cgo_library"),
stub.fixtures["cgolib"][0].Call,
Expand All @@ -242,7 +250,7 @@ func TestGenerator(t *testing.T) {
},
},
{
Path: "allcgolib/BUILD",
Path: "allcgolib/" + buildName,
Stmt: []bzl.Expr{
loadExpr("go_library", "go_test", "cgo_library"),
stub.fixtures["cgolib"][0].Call,
Expand Down

0 comments on commit fee2a86

Please sign in to comment.