diff --git a/.gitignore b/.gitignore index 8deefe6..144cc59 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .idea tmp/ plz-out/ +.idea/ # Entries below this point are managed by Please (DO NOT EDIT) plz-out diff --git a/examplerepo/third_party/go/cloud.google.com/BUILD b/examplerepo/third_party/go/cloud.google.com/BUILD index 563905b..7f18295 100644 --- a/examplerepo/third_party/go/cloud.google.com/BUILD +++ b/examplerepo/third_party/go/cloud.google.com/BUILD @@ -1,9 +1,10 @@ + go_module( name = "go", - install = ["..."], module = "cloud.google.com/go", version = "v0.34.0", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/antihax/BUILD b/examplerepo/third_party/go/github.com/antihax/BUILD index 173225e..5dc1bac 100644 --- a/examplerepo/third_party/go/github.com/antihax/BUILD +++ b/examplerepo/third_party/go/github.com/antihax/BUILD @@ -1,9 +1,10 @@ + go_module( name = "optional", - install = ["..."], module = "github.com/antihax/optional", version = "v1.0.0", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/census-instrumentation/BUILD b/examplerepo/third_party/go/github.com/census-instrumentation/BUILD index 976f3b8..ae53507 100644 --- a/examplerepo/third_party/go/github.com/census-instrumentation/BUILD +++ b/examplerepo/third_party/go/github.com/census-instrumentation/BUILD @@ -1,9 +1,10 @@ + go_module( name = "opencensus-proto", - install = ["..."], module = "github.com/census-instrumentation/opencensus-proto", version = "v0.2.1", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/client9/BUILD b/examplerepo/third_party/go/github.com/client9/BUILD index 2c82699..e736c66 100644 --- a/examplerepo/third_party/go/github.com/client9/BUILD +++ b/examplerepo/third_party/go/github.com/client9/BUILD @@ -1,9 +1,10 @@ + go_module( name = "misspell", - install = ["..."], module = "github.com/client9/misspell", version = "v0.3.4", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/cncf/BUILD b/examplerepo/third_party/go/github.com/cncf/BUILD index 22d57e1..28f7015 100644 --- a/examplerepo/third_party/go/github.com/cncf/BUILD +++ b/examplerepo/third_party/go/github.com/cncf/BUILD @@ -1,12 +1,13 @@ + go_module( name = "udpa_go", - install = ["..."], module = "github.com/cncf/udpa/go", version = "v0.0.0-20191209042840-269d4d468f6f", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/envoyproxy:protoc-gen-validate", "//third_party/go/github.com/golang:protobuf", "//third_party/go/google.golang.org:grpc", ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/envoyproxy/BUILD b/examplerepo/third_party/go/github.com/envoyproxy/BUILD index b7d2145..6480f21 100644 --- a/examplerepo/third_party/go/github.com/envoyproxy/BUILD +++ b/examplerepo/third_party/go/github.com/envoyproxy/BUILD @@ -1,9 +1,8 @@ + go_module( name = "go-control-plane", - install = ["..."], module = "github.com/envoyproxy/go-control-plane", version = "v0.9.4", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/census-instrumentation:opencensus-proto", "//third_party/go/github.com/cncf:udpa_go", @@ -14,14 +13,16 @@ go_module( "//third_party/go/google.golang.org:genproto", "//third_party/go/google.golang.org:grpc", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "protoc-gen-validate", - install = ["..."], module = "github.com/envoyproxy/protoc-gen-validate", version = "v0.1.0", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/ghodss/BUILD b/examplerepo/third_party/go/github.com/ghodss/BUILD index c3e7c88..467f1ae 100644 --- a/examplerepo/third_party/go/github.com/ghodss/BUILD +++ b/examplerepo/third_party/go/github.com/ghodss/BUILD @@ -1,9 +1,10 @@ + go_module( name = "yaml", - install = ["..."], module = "github.com/ghodss/yaml", version = "v1.0.0", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/golang/BUILD b/examplerepo/third_party/go/github.com/golang/BUILD index a8def94..ed609f2 100644 --- a/examplerepo/third_party/go/github.com/golang/BUILD +++ b/examplerepo/third_party/go/github.com/golang/BUILD @@ -1,29 +1,30 @@ + go_module( name = "glog", - install = ["..."], module = "github.com/golang/glog", version = "v0.0.0-20160126235308-23def4e6c14b", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "mock", - install = ["..."], module = "github.com/golang/mock", version = "v1.1.1", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "protobuf", - install = ["..."], module = "github.com/golang/protobuf", version = "v1.3.3", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/google/BUILD b/examplerepo/third_party/go/github.com/google/BUILD index dd221f8..9a3e073 100644 --- a/examplerepo/third_party/go/github.com/google/BUILD +++ b/examplerepo/third_party/go/github.com/google/BUILD @@ -1,20 +1,21 @@ + go_module( name = "go-cmp", - install = ["..."], module = "github.com/google/go-cmp", version = "v0.4.0", - visibility = ["PUBLIC"], deps = [ "//third_party/go/golang.org/x:xerrors", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "uuid", - install = ["..."], module = "github.com/google/uuid", version = "v1.1.2", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/grpc-ecosystem/BUILD b/examplerepo/third_party/go/github.com/grpc-ecosystem/BUILD index ec56c59..bccc373 100644 --- a/examplerepo/third_party/go/github.com/grpc-ecosystem/BUILD +++ b/examplerepo/third_party/go/github.com/grpc-ecosystem/BUILD @@ -1,9 +1,8 @@ + go_module( name = "grpc-gateway", - install = ["..."], module = "github.com/grpc-ecosystem/grpc-gateway", version = "v1.16.0", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/antihax:optional", "//third_party/go/github.com/ghodss:yaml", @@ -14,4 +13,6 @@ go_module( "//third_party/go/google.golang.org:genproto", "//third_party/go/google.golang.org:grpc", ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/prometheus/BUILD b/examplerepo/third_party/go/github.com/prometheus/BUILD index 98be2d4..a09564b 100644 --- a/examplerepo/third_party/go/github.com/prometheus/BUILD +++ b/examplerepo/third_party/go/github.com/prometheus/BUILD @@ -1,10 +1,11 @@ + go_module( name = "client_model", - install = ["..."], module = "github.com/prometheus/client_model", version = "v0.0.0-20190812154241-14fe0d1b01d4", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/golang:protobuf", ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/github.com/rogpeppe/BUILD b/examplerepo/third_party/go/github.com/rogpeppe/BUILD index 3ac5fec..70506fb 100644 --- a/examplerepo/third_party/go/github.com/rogpeppe/BUILD +++ b/examplerepo/third_party/go/github.com/rogpeppe/BUILD @@ -1,9 +1,10 @@ + go_module( name = "fastuuid", - install = ["..."], module = "github.com/rogpeppe/fastuuid", version = "v1.2.0", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/golang.org/x/BUILD b/examplerepo/third_party/go/golang.org/x/BUILD index fa7bed0..8b3b9e3 100644 --- a/examplerepo/third_party/go/golang.org/x/BUILD +++ b/examplerepo/third_party/go/golang.org/x/BUILD @@ -1,108 +1,109 @@ + go_module( name = "crypto", - install = ["..."], module = "golang.org/x/crypto", version = "v0.0.0-20190308221718-c2843e01d9a2", - visibility = ["PUBLIC"], deps = [ "//third_party/go/golang.org/x:sys", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "exp", - install = ["..."], module = "golang.org/x/exp", version = "v0.0.0-20190121172915-509febef88a4", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "lint", - install = ["..."], module = "golang.org/x/lint", version = "v0.0.0-20190313153728-d0100b6bd8b3", - visibility = ["PUBLIC"], deps = [ "//third_party/go/golang.org/x:tools", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "net", - install = ["..."], module = "golang.org/x/net", version = "v0.0.0-20190311183353-d8887717615a", - visibility = ["PUBLIC"], deps = [ "//third_party/go/golang.org/x:crypto", "//third_party/go/golang.org/x:text", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "oauth2", - install = ["..."], module = "golang.org/x/oauth2", version = "v0.0.0-20200107190931-bf48bf16ab8d", - visibility = ["PUBLIC"], deps = [ "//third_party/go/cloud.google.com:go", "//third_party/go/golang.org/x:net", "//third_party/go/google.golang.org:appengine", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "sync", - install = ["..."], module = "golang.org/x/sync", version = "v0.0.0-20190423024810-112230192c58", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "sys", - install = ["..."], module = "golang.org/x/sys", version = "v0.0.0-20190215142949-d0b11bdaac8a", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "text", - install = ["..."], module = "golang.org/x/text", version = "v0.3.0", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "tools", - install = ["..."], module = "golang.org/x/tools", version = "v0.0.0-20190524140312-2c0ae7006135", - visibility = ["PUBLIC"], deps = [ "//third_party/go/golang.org/x:net", "//third_party/go/golang.org/x:sync", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "xerrors", - install = ["..."], module = "golang.org/x/xerrors", version = "v0.0.0-20191204190536-9bdfabe68543", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/google.golang.org/BUILD b/examplerepo/third_party/go/google.golang.org/BUILD index a7d6ce9..78f84ec 100644 --- a/examplerepo/third_party/go/google.golang.org/BUILD +++ b/examplerepo/third_party/go/google.golang.org/BUILD @@ -1,22 +1,21 @@ + go_module( name = "appengine", - install = ["..."], module = "google.golang.org/appengine", version = "v1.4.0", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/golang:protobuf", "//third_party/go/golang.org/x:net", "//third_party/go/golang.org/x:text", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "genproto", - install = ["..."], module = "google.golang.org/genproto", version = "v0.0.0-20200513103714-09dca8ec2884", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/golang:protobuf", "//third_party/go/golang.org/x:lint", @@ -24,14 +23,14 @@ go_module( "//third_party/go/google.golang.org:grpc", "//third_party/go/honnef.co/go:tools", ], + visibility = ["PUBLIC"], + install = ["..."], ) go_module( name = "grpc", - install = ["..."], module = "google.golang.org/grpc", version = "v1.33.1", - visibility = ["PUBLIC"], deps = [ "//third_party/go/github.com/cncf:udpa_go", "//third_party/go/github.com/envoyproxy:go-control-plane", @@ -44,4 +43,6 @@ go_module( "//third_party/go/golang.org/x:sys", "//third_party/go/google.golang.org:genproto", ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/examplerepo/third_party/go/honnef.co/go/BUILD b/examplerepo/third_party/go/honnef.co/go/BUILD index 3297f98..fc4ed95 100644 --- a/examplerepo/third_party/go/honnef.co/go/BUILD +++ b/examplerepo/third_party/go/honnef.co/go/BUILD @@ -1,9 +1,10 @@ + go_module( name = "tools", - install = ["..."], module = "honnef.co/go/tools", version = "v0.0.0-20190523083050-ea95bdfd59fc", - visibility = ["PUBLIC"], deps = [ ], + visibility = ["PUBLIC"], + install = ["..."], ) diff --git a/go.sum b/go.sum index ec87f8f..90e5fe0 100644 --- a/go.sum +++ b/go.sum @@ -25,4 +25,5 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 565e852..a4f6a7c 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( const ( moduleFlag = "module" versionFlag = "version" + singleFileFlag = "single_file" ) // This binary will accept a module name and optionally a semver or commit hash, and will add this module to a BUILD file. @@ -33,6 +34,12 @@ func main() { Aliases: []string{"v"}, Usage: "Version of the module to add", }, + &cli.BoolFlag{ + Name: singleFileFlag, + Aliases: []string{"s"}, + Usage: "Whether to export to single BUILD file", + Value: false, + }, }, Action: func(c *cli.Context) error { ctx := context.TODO() @@ -46,6 +53,8 @@ func main() { defer host.TearDownGoMod(ctx) } + module.SingleFileBuild = c.Bool(singleFileFlag) + m := &module.Module{ Path: c.String(moduleFlag), Version: c.String(versionFlag), @@ -76,4 +85,4 @@ func main() { if err != nil { log.Fatal(err) } -} +} \ No newline at end of file diff --git a/module/directory.go b/module/directory.go index 5d898c4..740b501 100644 --- a/module/directory.go +++ b/module/directory.go @@ -33,10 +33,16 @@ func NewDirectory() *Directory { // Sync is a lazy implementation to refresh all of the module dependencies to the closest semver. func (d *Directory) Sync() { + names := map[string]struct{}{} + for _, vd := range d.modules { for _, mod := range vd.versions { // Flag this module as requiring to specify the version as we have multiple versions. mod.nameWithVersion = len(vd.versions) > 1 + if _, ok := names[mod.GetName()]; ok && SingleFileBuild { + mod.nameWithHost = true + } + names[mod.GetName()] = struct{}{} for i, dep := range mod.Deps { closestMod := d.GetClosestModule(dep.Path, dep.Version) if dep != closestMod { @@ -48,6 +54,7 @@ func (d *Directory) Sync() { } } + func (d *Directory) Print() { // Sort the paths to deterministically print output. paths := make([]string, 0, len(d.modules)) @@ -160,7 +167,6 @@ func (d *Directory) SetModule(mod *Module) *Module { if vd == nil { vd = NewVersionDirectory() } - fixedMod := vd.SetVersion(mod.Version, mod) d.Set(mod.Path, vd) return fixedMod @@ -201,6 +207,9 @@ func (vd *VersionDirectory) GetClosestVersion(version string) string { return existingVers } } + if major == "v0" && existingMajor == "v1" { + return existingVers + } } return version } @@ -219,7 +228,11 @@ func (vd *VersionDirectory) SetVersion(version string, mod *Module) *Module { // then delete that version, and store this "better" version and return. // If there is an existing version with different major, or none at all, // then store this "different" version and return. - // + // If there is an existing version with major 0 and the new version is major 1 + // then delete the existing version and store this "better" version and return + // If there is an existing version with major 1 and the new version is major 0 + // then return the existing version as it is "better" + // // This should eventually lead to there only being one entry // for each major version. // A different implementation may choose to have stricter/more relaxed @@ -238,6 +251,13 @@ func (vd *VersionDirectory) SetVersion(version string, mod *Module) *Module { // If incoming is less than existing, return existing. return existingMod } + if major == "v1" && existingMajor == "v0" { + delete(vd.versions, existingVers) + vd.versions[version] = mod + return mod + } else if major == "v0" && existingMajor == "v1" { + return existingMod + } } vd.versions[version] = mod diff --git a/module/module.go b/module/module.go index 107d97f..d88f876 100644 --- a/module/module.go +++ b/module/module.go @@ -16,32 +16,34 @@ import ( "golang.org/x/mod/semver" ) +var SingleFileBuild = false + const goModuleTemplateString = ` go_module( - name = "{{ .GetName }}", - module = "{{ .Path }}", - version = "{{ .Version }}", - deps = [ - {{- range .Deps }} - "{{ .GetFullyQualifiedName }}", - {{- end }} - ], - visibility = ["PUBLIC"], - install = ["..."], + name = "{{ .GetName }}", + module = "{{ .Path }}", + version = "{{ .Version }}", + deps = [ + {{- range .Deps }} + "{{ .GetFullyQualifiedName }}", + {{- end }} + ], + visibility = ["PUBLIC"], + install = ["..."], ) ` const goModuleDownloadTemplateString = ` go_mod_download( - name = "{{ .GetDownloadName }}", - module = "{{ .Path }}", - version = "{{ .Version }}", - deps = [ - {{- range .Deps }} - "{{ .GetFullyQualifiedName }}", - {{- end }} - ], - visibility = ["PUBLIC"], + name = "{{ .GetDownloadName }}", + module = "{{ .Path }}", + version = "{{ .Version }}", + deps = [ + {{- range .Deps }} + "{{ .GetFullyQualifiedName }}", + {{- end }} + ], + visibility = ["PUBLIC"], ) go_module( @@ -74,6 +76,7 @@ type Module struct { dir string sum string goModSum string + nameWithHost bool } // String returns a string representation of the module, with the module name and version. @@ -90,6 +93,7 @@ func (m *Module) GetName() string { if m.Name != "" { return m.Name } + splitPath := strings.Split(m.Path, "/") modName := splitPath[len(splitPath)-1] if splitPath[0] == "github.com" { @@ -98,6 +102,9 @@ func (m *Module) GetName() string { if m.nameWithVersion { return modName + "_" + semver.Major(m.Version) } + if m.nameWithHost { + return strings.ReplaceAll(m.Path, "/", "-") + } return modName } @@ -108,37 +115,53 @@ func (m *Module) GetDownloadName() string { // GetBuildPath returns the path to the please BUILD file where this module is defined. func (m *Module) GetBuildPath() string { + currentDir, err := os.Getwd() + if err != nil { + panic(err) + } + if SingleFileBuild { + return fmt.Sprintf("%s/third_party/go/BUILD", currentDir) + } splitPath := strings.Split(m.Path, "/") pathMinusEnd := strings.Join(splitPath[:len(splitPath)-1], "/") if splitPath[0] == "github.com" { pathMinusEnd = strings.Join(splitPath[:2], "/") } - currentDir, err := os.Getwd() - if err != nil { - panic(err) - } + return fmt.Sprintf("%s/third_party/go/%s/BUILD", currentDir, pathMinusEnd) } // GetFullyQualifiedName returns the please build target for this module. func (m *Module) GetFullyQualifiedName() string { - splitPath := strings.Split(m.Path, "/") - pathMinusEnd := strings.Join(splitPath[:len(splitPath)-1], "/") - if splitPath[0] == "github.com" { - pathMinusEnd = strings.Join(splitPath[:2], "/") + buildDir := "" + if !SingleFileBuild { + splitPath := strings.Split(m.Path, "/") + pathMinusEnd := strings.Join(splitPath[:len(splitPath)-1], "/") + if splitPath[0] == "github.com" { + pathMinusEnd = strings.Join(splitPath[:2], "/") + } + buildDir = fmt.Sprintf("third_party/go/%s", pathMinusEnd) + } else { + buildDir = "third_party/go" } - buildDir := fmt.Sprintf("third_party/go/%s", pathMinusEnd) + return "//" + buildDir + ":" + m.GetName() } // GetFullyQualifiedDownloadName returns the please build target for this module's go_mod_download rule. func (m *Module) GetFullyQualifiedDownloadName() string { - splitPath := strings.Split(m.Path, "/") - pathMinusEnd := strings.Join(splitPath[:len(splitPath)-1], "/") - if splitPath[0] == "github.com" { - pathMinusEnd = strings.Join(splitPath[:2], "/") + buildDir := "" + if !SingleFileBuild { + splitPath := strings.Split(m.Path, "/") + pathMinusEnd := strings.Join(splitPath[:len(splitPath)-1], "/") + if splitPath[0] == "github.com" { + pathMinusEnd = strings.Join(splitPath[:2], "/") + } + buildDir = fmt.Sprintf("third_party/go/%s", pathMinusEnd) + } else { + buildDir = "third_party/go" } - buildDir := fmt.Sprintf("third_party/go/%s", pathMinusEnd) + return "//" + buildDir + ":" + m.GetDownloadName() }