Skip to content

Commit

Permalink
support resolving imports to third party go_module rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Bice committed Mar 25, 2021
1 parent 713737d commit 873431d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 15 deletions.
38 changes: 24 additions & 14 deletions domain/wollemi/service_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,10 @@ func (this *Service) GoFormat(config wollemi.Config, paths []string) error {
} {
Imports:
for _, godep := range imports {
var prefix bool

goroot, ok := this.gofmt.isGoroot[godep]

if !ok {
if prefix = this.isInternal(godep); prefix {
goroot = false
} else {
goroot = this.golang.IsGoroot(godep)
}
goroot = this.golang.IsGoroot(godep)

this.gofmt.isGoroot[godep] = goroot
}
Expand All @@ -137,7 +132,7 @@ func (this *Service) GoFormat(config wollemi.Config, paths []string) error {

path := godep

if prefix || this.isInternal(path) {
if this.isInternal(path) {
path = strings.TrimPrefix(path, this.gopkg+"/")
} else {
path = filepath.Join("third_party/go", path)
Expand Down Expand Up @@ -192,6 +187,23 @@ func (this *Service) GoFormat(config wollemi.Config, paths []string) error {

dir.Build.GetRules(func(rule please.Rule) {
switch rule.Kind() {
case "go_module":
module := rule.AttrString("module")
target := &please.Target{
Name: rule.AttrString("name"),
Path: dir.Path,
}

for _, install := range rule.AttrStrings("install") {
install = strings.TrimSuffix(install, "/...")
path := module

if install != "." {
path = filepath.Join(path, install)
}

external[path] = append(external[path], target.String())
}
case "go_get", "go_get_with_sources":
get := strings.TrimSuffix(rule.AttrString("get"), "/...")
if get == "" {
Expand All @@ -201,19 +213,17 @@ func (this *Service) GoFormat(config wollemi.Config, paths []string) error {
}
}

name := rule.AttrString("name")

target := dir.Path
if filepath.Base(dir.Path) != name {
target += ":" + name
target := &please.Target{
Name: rule.AttrString("name"),
Path: dir.Path,
}

if rule.Kind() == "go_get_with_sources" {
get = rule.AttrStrings("outs")[0]
}

if get != "" && rule.AttrLiteral("binary") != "True" {
external[get] = append(external[get], "//"+target)
external[get] = append(external[get], target.String())
}
default:
name := rule.AttrString("name")
Expand Down
114 changes: 113 additions & 1 deletion domain/wollemi/service_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,57 @@ func (t *ServiceSuite) TestService_GoFormat() {
},
},
},
}, { // TEST_CASE ------------------------------------------------------------
Title: "supports resolving third party go_module rules",
Data: &GoFormatTestData{
Gosrc: gosrc,
Gopkg: gopkg,
Paths: []string{"app/server"},
Parse: t.WithThirdPartyGoModules(map[string]*please.BuildFile{
"app/server/BUILD.plz": &please.BuildFile{
Stmt: []please.Expr{
please.NewCallExpr("go_library", []please.Expr{
please.NewAssignExpr("=", "name", "server"),
please.NewAssignExpr("=", "srcs", []string{"server.go"}),
please.NewAssignExpr("=", "visibility", []string{"PUBLIC"}),
}),
},
},
}),
ImportDir: map[string]*golang.Package{
"app/server": &golang.Package{
GoFiles: []string{"server.go"},
GoFileImports: map[string][]string{
"server.go": []string{
"database/sql",
"encoding/json",
"github.com/golang/protobuf/proto/ptypes/wrappers",
"github.com/example/app/protos",
"google.golang.org/grpc",
"google.golang.org/grpc/credentials",
"strconv",
"strings",
},
},
},
},
Write: map[string]*please.BuildFile{
"app/server/BUILD.plz": &please.BuildFile{
Stmt: []please.Expr{
please.NewCallExpr("go_library", []please.Expr{
please.NewAssignExpr("=", "name", "server"),
please.NewAssignExpr("=", "srcs", []string{"server.go"}),
please.NewAssignExpr("=", "visibility", []string{"PUBLIC"}),
please.NewAssignExpr("=", "deps", []string{
"//app/protos",
"//third_party/go:github.com__golang__protobuf",
"//third_party/go:google.golang.org__grpc",
}),
}),
},
},
},
},
}} {
focus := ""

Expand Down Expand Up @@ -1509,7 +1560,68 @@ func (d *GoFormatTestData) Prepare() {
}

// WithThirdPartyGo merges the provided extra build files into a default set of
// third party go build files.
// third party go_module rules.
func (t *ServiceSuite) WithThirdPartyGoModules(extra map[string]*please.BuildFile) map[string]*please.BuildFile {
files := map[string]*please.BuildFile{
"third_party/go/BUILD.plz": &please.BuildFile{
Stmt: []please.Expr{
please.NewCallExpr("go_module", []please.Expr{
please.NewAssignExpr("=", "name", "github.com__spf13__cobra"),
please.NewAssignExpr("=", "module", "github.com/spf13/cobra"),
please.NewAssignExpr("=", "install", []string{"."}),
please.NewAssignExpr("=", "version", "v1.0.0"),
}),
please.NewCallExpr("go_module", []please.Expr{
please.NewAssignExpr("=", "name", "github.com__spf13__pflag"),
please.NewAssignExpr("=", "module", "github.com/spf13/pflag"),
please.NewAssignExpr("=", "install", []string{"."}),
please.NewAssignExpr("=", "version", "v1.0.5"),
}),
please.NewCallExpr("go_module", []please.Expr{
please.NewAssignExpr("=", "name", "github.com__golang__protobuf"),
please.NewAssignExpr("=", "module", "github.com/golang/protobuf"),
please.NewAssignExpr("=", "install", []string{"proto/ptypes/wrappers"}),
please.NewAssignExpr("=", "version", "v1.3.2"),
}),
please.NewCallExpr("go_module", []please.Expr{
please.NewAssignExpr("=", "name", "github.com__golang__mock"),
please.NewAssignExpr("=", "module", "github.com/golang/mock"),
please.NewAssignExpr("=", "version", "v1.3.2"),
please.NewAssignExpr("=", "install", []string{
"mockgen/model",
"gomock",
}),
}),
please.NewCallExpr("go_module", []please.Expr{
please.NewAssignExpr("=", "name", "github.com__stretchr__testify"),
please.NewAssignExpr("=", "module", "github.com/stretchr/testify"),
please.NewAssignExpr("=", "version", "v1.4.0"),
please.NewAssignExpr("=", "install", []string{
"assert",
"require",
"vendor/github.com/davecgh/go-spew/spew",
"vendor/github.com/pmezard/go-difflib/difflib",
}),
}),
please.NewCallExpr("go_module", []please.Expr{
please.NewAssignExpr("=", "name", "google.golang.org__grpc"),
please.NewAssignExpr("=", "module", "google.golang.org/grpc"),
please.NewAssignExpr("=", "install", []string{".", "credentials"}),
please.NewAssignExpr("=", "version", "v1.26.0"),
}),
},
},
}

for k, v := range extra {
files[k] = v
}

return files
}

// WithThirdPartyGo merges the provided extra build files into a default set of
// third party go_get rules.
func (t *ServiceSuite) WithThirdPartyGo(extra map[string]*please.BuildFile) map[string]*please.BuildFile {
files := map[string]*please.BuildFile{
"third_party/go/github.com/spf13/BUILD.plz": &please.BuildFile{
Expand Down

0 comments on commit 873431d

Please sign in to comment.