Skip to content

Commit edcf03d

Browse files
committed
Re-include generated examples
1 parent bca0548 commit edcf03d

20 files changed

+1588
-36
lines changed

MODULE.bazel

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ use_repo(
314314
# Configuration: Protobuf Deps
315315
# -------------------------------------------------------------------
316316

317+
proto_language_directives = [
318+
"gazelle:proto_language %s enable true" % name
319+
for name in ["cpp", "descriptor", "go", "java", "python", "scala"]
320+
]
321+
317322
proto_repository = use_extension("@build_stack_rules_proto//extensions:proto_repository.bzl", "proto_repository", dev_dependency = True)
318323

319324
proto_repository.archive(
@@ -324,13 +329,7 @@ proto_repository.archive(
324329
"gazelle:exclude google/protobuf/util/internal/testdata",
325330
"gazelle:exclude testdata",
326331
"gazelle:proto_go_modules_index_kind proto_go_library",
327-
"gazelle:proto_language cpp enable true",
328-
"gazelle:proto_language descriptor enable true",
329-
"gazelle:proto_language go enable true",
330-
"gazelle:proto_language java enable true",
331-
"gazelle:proto_language python enable true",
332-
"gazelle:proto_language scala enable true",
333-
],
332+
] + proto_language_directives,
334333
build_file_proto_mode = "file",
335334
build_file_generation = "clean",
336335
cfgs = ["@//:rules_proto_config.yaml"],
@@ -375,13 +374,7 @@ proto_repository.archive(
375374
"gazelle:exclude google/example",
376375
"gazelle:exclude google/maps/weather/v1",
377376
"gazelle:proto_go_modules_index_kind proto_go_library",
378-
"gazelle:proto_language cpp enable true",
379-
"gazelle:proto_language descriptor enable true",
380-
"gazelle:proto_language go enable true",
381-
"gazelle:proto_language java enable true",
382-
"gazelle:proto_language python enable true",
383-
"gazelle:proto_language scala enable true",
384-
],
377+
] + proto_language_directives,
385378
build_file_proto_mode = "file",
386379
cfgs = ["//:rules_proto_config.yaml"],
387380
imports = ["@protobufapis//:imports.csv"],

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ golden_test:
1414
test:
1515
bazel test ...
1616

17+
.PHONY: site
18+
site:
19+
bazel build '//example/golden:*'
20+
cp -f ./bazel-bin/example/golden/*.md docs/
21+
1722
update_pnpm_lock:
1823
# nvm use 18
1924
pnpm install --lockfile-only

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,21 @@ See <https://registry.bazel.build/search?q=rules_proto> for latest version.
4747
| For help setting up the gazelle extension | [GAZELLE.md](/docs/GAZELLE.md) |
4848
| Writing custom gazelle logic in starlark | [STARLARK.md](/docs/STARLARK.md) |
4949
| For a history of this repository | [HISTORY.md](/docs/HISTORY.md) |
50+
51+
## Examples
52+
53+
> Examples are generated from golden tests
54+
55+
| Description | Link |
56+
|------------------|-----------------------------------------------|
57+
| commonjs | [commonjs](/docs/commonjs.md) |
58+
| cpp | [cpp](/docs/cpp.md) |
59+
| csharp | [csharp](/docs/csharp.md) |
60+
| go | [go](/docs/go.md) |
61+
| java | [java](/docs/java.md) |
62+
| objc | [objc](/docs/objc.md) |
63+
| ruby | [ruby](/docs/ruby.md) |
64+
| python | [python](/docs/python.md) |
65+
| scala | [scala](/docs/scala.md) |
66+
| proto_repository | [proto_repository](/docs/proto_repository.md) |
67+
| starlark_java | [starlark_java](/docs/starlark_java.md) |

cmd/examplegen/generator.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ func generateMarkdown(c *Config) error {
1616
}
1717
defer f.Close()
1818

19-
var workspace, buildIn, buildOut, protoFile string
19+
var workspace, buildIn, buildOut string
2020
for _, src := range c.Files {
2121
base := filepath.Base(src)
22-
ext := filepath.Ext(base)
23-
24-
if ext == ".proto" {
25-
protoFile = src
26-
continue
27-
}
2822

2923
switch base {
3024
case "BUILD.in":
@@ -52,27 +46,24 @@ func generateMarkdown(c *Config) error {
5246

5347
fmt.Fprintf(f, "# %s example\n\n", c.Name)
5448

55-
fmt.Fprintf(f, "`bazel test %s_test`\n\n", c.Label)
49+
fmt.Fprintf(f, "[`testdata files`](/example/golden/testdata/%s)\n\n", c.Name)
5650

57-
fmt.Fprintf(f, "\n## `BUILD.bazel` (after gazelle)\n\n")
58-
if err := printFileBlock("BUILD.bazel", "python", buildOut, f); err != nil {
59-
return err
60-
}
51+
fmt.Fprintf(f, "\n## `Integration Test`\n\n")
52+
fmt.Fprintf(f, "`bazel test %s_test`)\n\n", c.Label)
6153

6254
fmt.Fprintf(f, "\n## `BUILD.bazel` (before gazelle)\n\n")
63-
if err := printFileBlock("BUILD.bazel", "python", buildIn, f); err != nil {
55+
if err := printFileBlock("BUILD.bazel", "python", buildIn, "", f); err != nil {
6456
return err
6557
}
6658

67-
fmt.Fprintf(f, "\n## `MODULE.bazel`\n\n")
68-
if err := printFileBlock(filepath.Base(workspace), "python", workspace, f); err != nil {
59+
fmt.Fprintf(f, "\n## `BUILD.bazel` (after gazelle)\n\n")
60+
if err := printFileBlock("BUILD.bazel", "python", buildOut, "", f); err != nil {
6961
return err
7062
}
7163

72-
if false {
73-
if err := printFileBlock(filepath.Base(protoFile), "proto", protoFile, f); err != nil {
74-
return err
75-
}
64+
fmt.Fprintf(f, "\n## `MODULE.bazel (snippet)`\n\n")
65+
if err := printFileBlock(filepath.Base(workspace), "python", workspace, c.WorkspaceIn, f); err != nil {
66+
return err
7667
}
7768

7869
return nil
@@ -140,14 +131,19 @@ func mapFilename(in string) string {
140131
return in
141132
}
142133

143-
func printFileBlock(name, syntax, filename string, out io.Writer) error {
134+
func printFileBlock(name, syntax, filename string, extraContent string, out io.Writer) error {
144135
fmt.Fprintf(out, "~~~%s\n", syntax)
145136
data, err := os.ReadFile(filename)
146137
if err != nil {
147-
log.Panicf("%s: failed to read filename=%q: %v", name, filename, err)
138+
return fmt.Errorf("%s: failed to read filename=%q: %v", name, filename, err)
148139
}
149140
if _, err := out.Write(data); err != nil {
150-
log.Panicf("%s: write %q: %v", name, filename, err)
141+
return fmt.Errorf("%s: write %q: %v", name, filename, err)
142+
}
143+
if extraContent != "" {
144+
if _, err := out.Write([]byte(extraContent + "\n")); err != nil {
145+
return fmt.Errorf("%s: write %q: %v", name, filename, err)
146+
}
151147
}
152148
fmt.Fprintf(out, "~~~\n\n")
153149

docs/GAZELLE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ proto_repository.archive(
478478
"gazelle:proto_plugin protoc-gen-go option Msrc/main/protobuf/crash_debugging.proto=github.com/bazelbuild/bazel/src/main/protobuf/crash_debugging",
479479
"gazelle:proto_plugin protoc-gen-go option Msrc/main/protobuf/file_invalidation_data.proto=github.com/bazelbuild/bazel/src/main/protobuf/file_invalidation_data",
480480
"gazelle:proto_plugin protoc-gen-go option Msrc/main/protobuf/bazel_output_service_rev2.proto=github.com/bazelbuild/bazel/src/main/protobuf/bazel_output_service_rev2",
481+
] + [
482+
"gazelle:proto_plugin protoc-gen-go option Msrc/main/protobuf/{0}.proto=github.com/bazelbuild/bazel/src/main/protobuf/{0}" % name
483+
for name in [
484+
"stardoc_output", "strategy_policy", "execution_graph", "remote_scrubbing", "memory_pressure", "cache_salt", "crash_debugging", "file_invalidation_data", "bazel_output_service_rev2",
485+
],
481486
],
482487
deleted_files = [
483488
"src/main/protobuf/bazel_output_service.proto",

docs/commonjs.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
layout: default
3+
title: commonjs
4+
permalink: examples/commonjs
5+
parent: Examples
6+
---
7+
8+
9+
# commonjs example
10+
11+
[`testdata files`](/example/golden/testdata/commonjs)
12+
13+
14+
## `Integration Test`
15+
16+
`bazel test @@//example/golden:commonjs_test`)
17+
18+
19+
## `BUILD.bazel` (before gazelle)
20+
21+
~~~python
22+
# "proto_rule" instantiates the proto_compile rule
23+
# gazelle:proto_rule proto_compile implementation stackb:rules_proto:proto_compile
24+
25+
# "proto_plugin" instantiates the builtin commonjs plugin
26+
# gazelle:proto_plugin commonjs implementation builtin:js:common
27+
28+
# "proto_language" binds the rule(s) and plugin(s) together
29+
# gazelle:proto_language commonjs rule proto_compile
30+
# gazelle:proto_language commonjs plugin commonjs
31+
~~~
32+
33+
34+
## `BUILD.bazel` (after gazelle)
35+
36+
~~~python
37+
load("@build_stack_rules_proto//rules:proto_compile.bzl", "proto_compile")
38+
load("@rules_proto//proto:defs.bzl", "proto_library")
39+
40+
# "proto_rule" instantiates the proto_compile rule
41+
# gazelle:proto_rule proto_compile implementation stackb:rules_proto:proto_compile
42+
43+
# "proto_plugin" instantiates the builtin commonjs plugin
44+
# gazelle:proto_plugin commonjs implementation builtin:js:common
45+
46+
# "proto_language" binds the rule(s) and plugin(s) together
47+
# gazelle:proto_language commonjs rule proto_compile
48+
# gazelle:proto_language commonjs plugin commonjs
49+
50+
proto_library(
51+
name = "example_proto",
52+
srcs = ["example.proto"],
53+
visibility = ["//visibility:public"],
54+
)
55+
56+
proto_compile(
57+
name = "example_commonjs_compile",
58+
options = {"@build_stack_rules_proto//plugin/builtin:commonjs": ["import_style=commonjs"]},
59+
outputs = ["example_pb.js"],
60+
plugins = ["@build_stack_rules_proto//plugin/builtin:commonjs"],
61+
proto = "example_proto",
62+
)
63+
~~~
64+
65+
66+
## `MODULE.bazel (snippet)`
67+
68+
~~~python
69+
70+
bazel_dep(name = "rules_go", version = "0.57.0", repo_name = "io_bazel_rules_go")
71+
72+
# -------------------------------------------------------------------
73+
# Configuration: Go
74+
# -------------------------------------------------------------------
75+
76+
go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
77+
go_sdk.download(version = "1.23.1")
78+
79+
# -------------------------------------------------------------------
80+
# Configuration: protobuf
81+
# -------------------------------------------------------------------
82+
83+
register_toolchains("@build_stack_rules_proto//toolchain:prebuilt")
84+
85+
~~~
86+

docs/cpp.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
layout: default
3+
title: cpp
4+
permalink: examples/cpp
5+
parent: Examples
6+
---
7+
8+
9+
# cpp example
10+
11+
[`testdata files`](/example/golden/testdata/cpp)
12+
13+
14+
## `Integration Test`
15+
16+
`bazel test @@//example/golden:cpp_test`)
17+
18+
19+
## `BUILD.bazel` (before gazelle)
20+
21+
~~~python
22+
# "proto_rule" instantiates the proto_compile rule
23+
# gazelle:proto_rule proto_compile implementation stackb:rules_proto:proto_compile
24+
25+
# "proto_plugin" instantiates the builtin cpp plugin
26+
# gazelle:proto_plugin cpp implementation builtin:cpp
27+
28+
# "proto_language" binds the rule(s) and plugin(s) together
29+
# gazelle:proto_language cpp rule proto_compile
30+
# gazelle:proto_language cpp plugin cpp
31+
~~~
32+
33+
34+
## `BUILD.bazel` (after gazelle)
35+
36+
~~~python
37+
load("@build_stack_rules_proto//rules:proto_compile.bzl", "proto_compile")
38+
load("@rules_proto//proto:defs.bzl", "proto_library")
39+
40+
# "proto_rule" instantiates the proto_compile rule
41+
# gazelle:proto_rule proto_compile implementation stackb:rules_proto:proto_compile
42+
43+
# "proto_plugin" instantiates the builtin cpp plugin
44+
# gazelle:proto_plugin cpp implementation builtin:cpp
45+
46+
# "proto_language" binds the rule(s) and plugin(s) together
47+
# gazelle:proto_language cpp rule proto_compile
48+
# gazelle:proto_language cpp plugin cpp
49+
50+
proto_library(
51+
name = "example_proto",
52+
srcs = ["example.proto"],
53+
visibility = ["//visibility:public"],
54+
)
55+
56+
proto_compile(
57+
name = "example_cpp_compile",
58+
outputs = [
59+
"example.pb.cc",
60+
"example.pb.h",
61+
],
62+
plugins = ["@build_stack_rules_proto//plugin/builtin:cpp"],
63+
proto = "example_proto",
64+
)
65+
~~~
66+
67+
68+
## `MODULE.bazel (snippet)`
69+
70+
~~~python
71+
72+
bazel_dep(name = "rules_go", version = "0.57.0", repo_name = "io_bazel_rules_go")
73+
74+
# -------------------------------------------------------------------
75+
# Configuration: Go
76+
# -------------------------------------------------------------------
77+
78+
go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
79+
go_sdk.download(version = "1.23.1")
80+
81+
# -------------------------------------------------------------------
82+
# Configuration: protobuf
83+
# -------------------------------------------------------------------
84+
85+
register_toolchains("@build_stack_rules_proto//toolchain:prebuilt")
86+
87+
~~~
88+

0 commit comments

Comments
 (0)