-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Support "flat" style of of BUILD file organization
It supports the pattern 2 discussed in #16 (comment)
- Loading branch information
Showing
12 changed files
with
404 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package generator | ||
|
||
import ( | ||
bzl "github.com/bazelbuild/buildifier/core" | ||
) | ||
|
||
const ( | ||
// goRulesBzl is the label of the Skylark file which provides Go rules | ||
goRulesBzl = "@io_bazel_rules_go//go:def.bzl" | ||
) | ||
|
||
type fileBuilder interface { | ||
addRules(rel string, rules []*bzl.Rule) | ||
files() []*bzl.File | ||
} | ||
|
||
func generateLoad(f *bzl.File) bzl.Expr { | ||
var list []string | ||
for _, kind := range []string{ | ||
"go_prefix", | ||
"go_library", | ||
"go_binary", | ||
"go_test", | ||
// TODO(yugui): Support cgo_library | ||
} { | ||
if len(f.Rules(kind)) > 0 { | ||
list = append(list, kind) | ||
} | ||
} | ||
if len(list) == 0 { | ||
return nil | ||
} | ||
return loadExpr(goRulesBzl, list...) | ||
} | ||
|
||
func loadExpr(ruleFile string, rules ...string) bzl.Expr { | ||
var list []bzl.Expr | ||
for _, r := range append([]string{ruleFile}, rules...) { | ||
list = append(list, &bzl.StringExpr{Value: r}) | ||
} | ||
|
||
return &bzl.CallExpr{ | ||
X: &bzl.LiteralExpr{Token: "load"}, | ||
List: list, | ||
ForceCompact: true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package generator | ||
|
||
import ( | ||
bzl "github.com/bazelbuild/buildifier/core" | ||
) | ||
|
||
type flatFileBuilder struct { | ||
f bzl.File | ||
} | ||
|
||
func (b *flatFileBuilder) addRules(rel string, rules []*bzl.Rule) { | ||
for _, r := range rules { | ||
b.f.Stmt = append(b.f.Stmt, r.Call) | ||
} | ||
} | ||
|
||
func (b *flatFileBuilder) files() []*bzl.File { | ||
b.f.Path = "BUILD" | ||
if load := generateLoad(&b.f); load != nil { | ||
b.f.Stmt = append([]bzl.Expr{load}, b.f.Stmt...) | ||
} | ||
return []*bzl.File{&b.f} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package generator | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
bzl "github.com/bazelbuild/buildifier/core" | ||
) | ||
|
||
type structuredFileBuilder struct { | ||
fs []*bzl.File | ||
} | ||
|
||
func (b *structuredFileBuilder) addRules(rel string, rules []*bzl.Rule) { | ||
f := &bzl.File{Path: filepath.Join(rel, "BUILD")} | ||
for _, r := range rules { | ||
f.Stmt = append(f.Stmt, r.Call) | ||
} | ||
if load := generateLoad(f); load != nil { | ||
f.Stmt = append([]bzl.Expr{load}, f.Stmt...) | ||
} | ||
|
||
b.fs = append(b.fs, f) | ||
} | ||
|
||
func (b *structuredFileBuilder) files() []*bzl.File { | ||
return b.fs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.