Skip to content

Commit

Permalink
Support go_prefix rule
Browse files Browse the repository at this point in the history
  • Loading branch information
yugui committed Aug 17, 2016
1 parent 9f6f10a commit 472cc0b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 13 additions & 2 deletions go/tools/gazelle/rules/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,33 @@ type Generator interface {
// See also https://github.com/bazelbuild/rules_go#go_prefix.
func NewGenerator(goPrefix string) Generator {
return &generator{
goPrefix: goPrefix,
// TODO(yugui): Support another resolver to cover the pattern 2 in
// https://github.com/bazelbuild/rules_go/issues/16#issuecomment-216010843
r: structuredResolver{goPrefix: goPrefix},
}
}

type generator struct {
r labelResolver
goPrefix string
r labelResolver
}

func (g *generator) Generate(rel string, pkg *build.Package) ([]*bzl.Rule, error) {
var rules []*bzl.Rule
if rel == "" {
p, err := newRule("go_prefix", []interface{}{g.goPrefix}, nil)
if err != nil {
return nil, err
}
rules = append(rules, p)
}

r, err := g.generate(rel, pkg)
if err != nil {
return nil, err
}
rules := []*bzl.Rule{r}
rules = append(rules, r)

if len(pkg.TestGoFiles) > 0 {
t, err := g.generateTest(rel, pkg, r.AttrString("name"))
Expand Down
21 changes: 21 additions & 0 deletions go/tools/gazelle/rules/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,24 @@ func TestGenerator(t *testing.T) {
}
}
}

func TestGeneratorGoPrefix(t *testing.T) {
g := rules.NewGenerator("example.com/repo/lib/deep")
want := `
go_prefix("example.com/repo/lib/deep")
go_library(
name = "go_default_library",
srcs = ["thought.go"],
)
`
pkg := packageFromDir(t, filepath.FromSlash("lib/deep"))
rules, err := g.Generate("", pkg)
if err != nil {
t.Errorf("g.Generate(%q, %#v) failed with %v; want success", "", pkg, err)
}

if got, want := format(rules), canonicalize(t, "BUILD", want); got != want {
t.Errorf("g.Generate(%q, %#v) = %s; want %s", "", pkg, got, want)
}
}

0 comments on commit 472cc0b

Please sign in to comment.