Skip to content

Commit

Permalink
Add per-file mode for proto_library generation (bazel-contrib#1033)
Browse files Browse the repository at this point in the history
* Add per-file mode for proto_library generation
  • Loading branch information
wolfd authored Oct 28, 2021
1 parent 67a3e22 commit a31d764
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 132 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ the one above, the
add [tests](https://github.com/bazelbuild/bazel-gazelle/tree/master/tests).
Run the existing tests with `bazel test //...`. Update
[README.rst](https://github.com/bazelbuild/bazel-gazelle/blob/master/README.rst)
if appropriate.
if appropriate.
1. [Create a pull request](https://help.github.com/articles/creating-a-pull-request/).
This will start the code review process. **All submissions, including
submissions by project members, require review.**
Expand Down
257 changes: 129 additions & 128 deletions README.rst

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions language/proto/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const (
// PackageMode generates a proto_library for each set of .proto files with
// the same package name in each directory.
PackageMode

// FileMode generates a proto_library for each .proto file.
FileMode
)

func ModeFromString(s string) (Mode, error) {
Expand All @@ -111,6 +114,8 @@ func ModeFromString(s string) (Mode, error) {
return LegacyMode, nil
case "package":
return PackageMode, nil
case "file":
return FileMode, nil
default:
return 0, fmt.Errorf("unrecognized proto mode: %q", s)
}
Expand All @@ -128,6 +133,8 @@ func (m Mode) String() string {
return "legacy"
case PackageMode:
return "package"
case FileMode:
return "file"
default:
log.Panicf("unknown mode %d", m)
return ""
Expand Down
13 changes: 10 additions & 3 deletions language/proto/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,25 @@ func buildPackages(pc *ProtoConfig, dir, rel string, protoFiles, genFiles []stri
for _, name := range protoFiles {
info := protoFileInfo(dir, name)
key := info.PackageName
if pc.groupOption != "" {

if pc.Mode == FileMode {
key = strings.TrimSuffix(name, ".proto")
} else if pc.groupOption != "" { // implicitly PackageMode
for _, opt := range info.Options {
if opt.Key == pc.groupOption {
key = opt.Value
break
}
}
}

if packageMap[key] == nil {
packageMap[key] = newPackage(info.PackageName)
}
packageMap[key].addFile(info)
if key != info.PackageName {
packageMap[key].RuleName = key
}
}

switch pc.Mode {
Expand All @@ -147,7 +154,7 @@ func buildPackages(pc *ProtoConfig, dir, rel string, protoFiles, genFiles []stri
}
return []*Package{pkg}

case PackageMode:
case PackageMode, FileMode:
pkgs := make([]*Package, 0, len(packageMap))
for _, pkg := range packageMap {
pkgs = append(pkgs, pkg)
Expand Down Expand Up @@ -212,7 +219,7 @@ func generateProto(pc *ProtoConfig, rel string, pkg *Package, shouldSetVisibilit
if pc.Mode == DefaultMode {
name = RuleName(goPackageName(pkg), pc.GoPrefix, rel)
} else {
name = RuleName(pkg.Options[pc.groupOption], pkg.Name, rel)
name = RuleName(pkg.RuleName, pkg.Name, rel)
}
r := rule.NewRule("proto_library", name)
srcs := make([]string, 0, len(pkg.Files))
Expand Down
1 change: 1 addition & 0 deletions language/proto/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "path/filepath"
// same package name. This translates to a proto_library rule.
type Package struct {
Name string
RuleName string // if not set, defaults to Name
Files map[string]FileInfo
Imports map[string]bool
Options map[string]string
Expand Down
35 changes: 35 additions & 0 deletions language/proto/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,41 @@ proto_library(
name = "dep_proto",
deps = ["//foo:foo_proto"],
)
`,
}, {
desc: "test single file resolution in file mode",
index: []buildFile{{
rel: "somedir",
content: `
# gazelle:proto file
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
)
proto_library(
name = "bar_proto",
srcs = ["bar.proto"],
)
proto_library(
name = "baz_proto",
srcs = ["baz.proto"],
)
`,
}},
old: `
proto_library(
name = "other_proto",
_imports = ["somedir/bar.proto"],
)
`,
want: `
proto_library(
name = "other_proto",
deps = ["//somedir:bar_proto"],
)
`,
},
} {
Expand Down
1 change: 1 addition & 0 deletions language/proto/testdata/file_mode/BUILD.old
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:proto file
13 changes: 13 additions & 0 deletions language/proto/testdata/file_mode/BUILD.want
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "bar_proto",
srcs = ["bar.proto"],
visibility = ["//visibility:public"],
)

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)
3 changes: 3 additions & 0 deletions language/proto/testdata/file_mode/bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
syntax = "proto3";

package file_mode;
3 changes: 3 additions & 0 deletions language/proto/testdata/file_mode/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
syntax = "proto3";

package file_mode;

0 comments on commit a31d764

Please sign in to comment.