diff --git a/examples/v2/BUILD b/examples/v2/BUILD new file mode 100644 index 0000000..a0d7ed7 --- /dev/null +++ b/examples/v2/BUILD @@ -0,0 +1,2 @@ +# Export the buf.yaml to make it available to rules in other packages. +exports_files(["buf.yaml"]) diff --git a/examples/v2/MODULE.bazel b/examples/v2/MODULE.bazel new file mode 100644 index 0000000..00bb183 --- /dev/null +++ b/examples/v2/MODULE.bazel @@ -0,0 +1,6 @@ +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### diff --git a/examples/v2/README.md b/examples/v2/README.md new file mode 100644 index 0000000..501adfd --- /dev/null +++ b/examples/v2/README.md @@ -0,0 +1,8 @@ +# Workspace + +This example demonstrates how to uses `rules_buf` with [workspaces](https://docs.buf.build/reference/workspaces). + +- Export each `buf.yaml` ([fooapis](fooapis/BUILD.bazel#L4), [barapis](barapis/BUILD.bazel#L4)) +- `proto_library` rules need an additional argument `strip_import_prefix` ([foo/v1](fooapis/foo/v1/BUILD.bazel#L7), [bar/v1](barapis/bar/v1/BUILD.bazel#L7)) + +Checkout the [gazelle example](../gazelle) for way to generate the rules. diff --git a/examples/v2/WORKSPACE b/examples/v2/WORKSPACE new file mode 100644 index 0000000..defa8cd --- /dev/null +++ b/examples/v2/WORKSPACE @@ -0,0 +1,19 @@ +local_repository( + name = "rules_buf", + path = "../..", +) + +load("@rules_buf//buf:repositories.bzl", "rules_buf_dependencies", "rules_buf_toolchains") + +rules_buf_dependencies() + +rules_buf_toolchains( + sha256 = "f7e50b227bf171158ff4a80d22f274d0195487b618e0c4a57b3b0741a52453c2", + version = "v1.32.1", +) + +load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") + +rules_proto_dependencies() + +rules_proto_toolchains() diff --git a/examples/v2/barapis/BUILD.bazel b/examples/v2/barapis/BUILD.bazel new file mode 100644 index 0000000..0376f58 --- /dev/null +++ b/examples/v2/barapis/BUILD.bazel @@ -0,0 +1,29 @@ +# Copyright 2021-2023 Buf Technologies, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_buf//buf:defs.bzl", "buf_breaking_test") + + +buf_breaking_test( + name = "barapis_proto_breaking", + # The Image file to check against. + against = "testdata/image.bin", + config = "//:buf.yaml", + module = "barapis", + # The proto_library targets to include. + # Refer to the documentation for more on this: https://docs.buf.build/build-systems/bazel#buf-breaking-test + targets = [ + "//barapis/bar/v1:bar_proto", + ], +) diff --git a/examples/v2/barapis/bar/v1/BUILD.bazel b/examples/v2/barapis/bar/v1/BUILD.bazel new file mode 100644 index 0000000..127838b --- /dev/null +++ b/examples/v2/barapis/bar/v1/BUILD.bazel @@ -0,0 +1,30 @@ +# Copyright 2021-2023 Buf Technologies, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_buf//buf:defs.bzl", "buf_lint_test") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "bar_proto", + srcs = ["bar.proto"], + strip_import_prefix = "/barapis", + visibility = ["//visibility:public"], +) + +buf_lint_test( + name = "bar_proto_lint", + config = "//:buf.yaml", + module = "barapis", + targets = [":bar_proto"], +) diff --git a/examples/v2/barapis/bar/v1/bar.proto b/examples/v2/barapis/bar/v1/bar.proto new file mode 100644 index 0000000..23d917f --- /dev/null +++ b/examples/v2/barapis/bar/v1/bar.proto @@ -0,0 +1,22 @@ +// Copyright 2021-2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package bar.v1; + +message Bar { + string id = 1; + string name = 2; +} diff --git a/examples/v2/barapis/testdata/image.bin b/examples/v2/barapis/testdata/image.bin new file mode 100644 index 0000000..cf90197 Binary files /dev/null and b/examples/v2/barapis/testdata/image.bin differ diff --git a/examples/v2/buf.lock b/examples/v2/buf.lock new file mode 100644 index 0000000..4f98143 --- /dev/null +++ b/examples/v2/buf.lock @@ -0,0 +1,2 @@ +# Generated by buf. DO NOT EDIT. +version: v2 diff --git a/examples/v2/buf.yaml b/examples/v2/buf.yaml new file mode 100644 index 0000000..c8efbe8 --- /dev/null +++ b/examples/v2/buf.yaml @@ -0,0 +1,17 @@ +version: v2 +modules: + - path: barapis + - path: fooapis +lint: + use: + - DEFAULT + except: + - FIELD_NOT_REQUIRED + - PACKAGE_NO_IMPORT_CYCLE + disallow_comment_ignores: true +breaking: + use: + - FILE + except: + - EXTENSION_NO_DELETE + - FIELD_SAME_DEFAULT diff --git a/examples/v2/fooapis/BUILD.bazel b/examples/v2/fooapis/BUILD.bazel new file mode 100644 index 0000000..39a895d --- /dev/null +++ b/examples/v2/fooapis/BUILD.bazel @@ -0,0 +1,28 @@ +# Copyright 2021-2023 Buf Technologies, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_buf//buf:defs.bzl", "buf_breaking_test") + +buf_breaking_test( + name = "fooapis_proto_breaking", + # The Image file to check against. + against = "testdata/image.bin", + config = "//:buf.yaml", + module = "fooapis", + # The proto_library targets to include. + # Refer to the documentation for more on this: https://docs.buf.build/build-systems/bazel#buf-breaking-test + targets = [ + "//fooapis/foo/v1:foo_proto", + ], +) diff --git a/examples/v2/fooapis/foo/v1/BUILD.bazel b/examples/v2/fooapis/foo/v1/BUILD.bazel new file mode 100644 index 0000000..0361bd7 --- /dev/null +++ b/examples/v2/fooapis/foo/v1/BUILD.bazel @@ -0,0 +1,31 @@ +# Copyright 2021-2023 Buf Technologies, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_buf//buf:defs.bzl", "buf_lint_test") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "foo_proto", + srcs = ["foo.proto"], + strip_import_prefix = "/fooapis", + visibility = ["//visibility:public"], + deps = ["//barapis/bar/v1:bar_proto"], +) + +buf_lint_test( + name = "foo_proto_lint", + config = "//:buf.yaml", + module = "fooapis", + targets = [":foo_proto"], +) diff --git a/examples/v2/fooapis/foo/v1/foo.proto b/examples/v2/fooapis/foo/v1/foo.proto new file mode 100644 index 0000000..c96fba4 --- /dev/null +++ b/examples/v2/fooapis/foo/v1/foo.proto @@ -0,0 +1,25 @@ +// Copyright 2021-2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package foo.v1; + +import "bar/v1/bar.proto"; + +message Foo { + string id = 1; + string name = 2; + bar.v1.Bar bar = 3; +} diff --git a/examples/v2/fooapis/testdata/image.bin b/examples/v2/fooapis/testdata/image.bin new file mode 100644 index 0000000..cf90197 Binary files /dev/null and b/examples/v2/fooapis/testdata/image.bin differ diff --git a/examples/workspace/MODULE.bazel b/examples/workspace/MODULE.bazel new file mode 100644 index 0000000..00bb183 --- /dev/null +++ b/examples/workspace/MODULE.bazel @@ -0,0 +1,6 @@ +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### diff --git a/examples/workspace/barapis/BUILD.bazel b/examples/workspace/barapis/BUILD.bazel index 36df1ec..b01b2ff 100644 --- a/examples/workspace/barapis/BUILD.bazel +++ b/examples/workspace/barapis/BUILD.bazel @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@rules_buf//buf:defs.bzl", "buf_breaking_test", "buf_push") +load("@rules_buf//buf:defs.bzl", "buf_breaking_test") # Export the buf.yaml to make it available to rules in other packages. exports_files(["buf.yaml"]) @@ -28,12 +28,3 @@ buf_breaking_test( "//barapis/bar/v1:bar_proto", ], ) - -buf_push( - name = "push_barapis", - config = ":buf.yaml", - lock = "buf.lock", - targets = [ - "//barapis/bar/v1:bar_proto", - ], -)