Skip to content

Commit 9fc3565

Browse files
committed
Add gazelle:python_proto_naming_convention directive, and a bunch of tests
1 parent dd6550f commit 9fc3565

File tree

15 files changed

+148
-3
lines changed

15 files changed

+148
-3
lines changed

gazelle/python/configure.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (py *Configurer) KnownDirectives() []string {
6363
pythonconfig.LibraryNamingConvention,
6464
pythonconfig.BinaryNamingConvention,
6565
pythonconfig.TestNamingConvention,
66+
pythonconfig.ProtoNamingConvention,
6667
pythonconfig.DefaultVisibilty,
6768
pythonconfig.Visibility,
6869
pythonconfig.TestFilePattern,
@@ -179,6 +180,8 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
179180
config.SetBinaryNamingConvention(strings.TrimSpace(d.Value))
180181
case pythonconfig.TestNamingConvention:
181182
config.SetTestNamingConvention(strings.TrimSpace(d.Value))
183+
case pythonconfig.ProtoNamingConvention:
184+
config.SetProtoNamingConvention(strings.TrimSpace(d.Value))
182185
case pythonconfig.DefaultVisibilty:
183186
switch directiveArg := strings.TrimSpace(d.Value); directiveArg {
184187
case "NONE":

gazelle/python/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
227227
result.Gen = make([]*rule.Rule, 0)
228228

229229
if cfg.GenerateProto() {
230-
generateProtoLibraries(args, pythonProjectRoot, visibility, &result)
230+
generateProtoLibraries(args, cfg, pythonProjectRoot, visibility, &result)
231231
}
232232

233233
collisionErrors := singlylinkedlist.New()
@@ -556,7 +556,7 @@ func ensureNoCollision(file *rule.File, targetName, kind string) error {
556556
return nil
557557
}
558558

559-
func generateProtoLibraries(args language.GenerateArgs, pythonProjectRoot string, visibility []string, res *language.GenerateResult) {
559+
func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config, pythonProjectRoot string, visibility []string, res *language.GenerateResult) {
560560
// First, enumerate all the proto_library in this package.
561561
var protoRuleNames []string
562562
for _, r := range args.OtherGen {
@@ -580,7 +580,7 @@ func generateProtoLibraries(args language.GenerateArgs, pythonProjectRoot string
580580
emptySiblings := treeset.Set{}
581581
// Generate a py_proto_library for each proto_library.
582582
for _, protoRuleName := range protoRuleNames {
583-
pyProtoLibraryName := strings.TrimSuffix(protoRuleName, "_proto") + "_py_pb2"
583+
pyProtoLibraryName := cfg.RenderProtoName(protoRuleName)
584584
pyProtoLibrary := newTargetBuilder(pyProtoLibraryKind, pyProtoLibraryName, pythonProjectRoot, args.Rel, &emptySiblings).
585585
addVisibility(visibility).
586586
addResolvedDependency(":" + protoRuleName).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Directive: `python_proto_naming_convention`
2+
3+
This test case asserts that the `# gazelle:python_proto_naming_convention` directive
4+
correctly:
5+
6+
1. Has no effect on pre-existing `py_proto_library` when `gazelle:python_generate_proto` is disabled.
7+
2. Uses the default value when proto generation is on and `python_proto_naming_convention` is not set.
8+
2. Uses the provided naming convention when proto generation is on and `python_proto_naming_convention` is set.
9+
10+
[gh-3081]: https://github.com/bazel-contrib/rules_python/issues/3081
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
expect:
3+
exit_code: 0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
# gazelle:python_generate_proto false
5+
# gazelle:python_proto_naming_convention some_$proto_name$_value
6+
7+
proto_library(
8+
name = "foo_proto",
9+
srcs = ["foo.proto"],
10+
visibility = ["//:__subpackages__"],
11+
)
12+
13+
py_proto_library(
14+
name = "foo_proto_custom_name",
15+
visibility = ["//:__subpackages__"],
16+
deps = [":foo_proto"],
17+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
# gazelle:python_generate_proto false
5+
# gazelle:python_proto_naming_convention some_$proto_name$_value
6+
7+
proto_library(
8+
name = "foo_proto",
9+
srcs = ["foo.proto"],
10+
visibility = ["//:__subpackages__"],
11+
)
12+
13+
py_proto_library(
14+
name = "foo_proto_custom_name",
15+
visibility = ["//:__subpackages__"],
16+
deps = [":foo_proto"],
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package foo.bar;
4+
5+
message Foo {
6+
string bar = 1;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
3+
# gazelle:python_generate_proto true
4+
5+
proto_library(
6+
name = "foo_proto",
7+
srcs = ["foo.proto"],
8+
visibility = ["//:__subpackages__"],
9+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
4+
# gazelle:python_generate_proto true
5+
6+
proto_library(
7+
name = "foo_proto",
8+
srcs = ["foo.proto"],
9+
visibility = ["//:__subpackages__"],
10+
)
11+
12+
py_proto_library(
13+
name = "foo_py_pb2",
14+
visibility = ["//:__subpackages__"],
15+
deps = [":foo_proto"],
16+
)

0 commit comments

Comments
 (0)