Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/test_ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,23 @@ jobs:
bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/basic_test.proto;
${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/basic.rb;
${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/implementation.rb
typecheck:
name: Typecheck
runs-on: ubuntu-latest
steps:
- name: Checkout pending changes
uses: protocolbuffers/protobuf-ci/checkout@v2
with:
ref: ${{ inputs.safe-checkout }}
- name: Run type checker
uses: protocolbuffers/protobuf-ci/bazel-docker@v2
with:
image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:7.6.1-e0df73e51131ccaf53451355d22577f377357604
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
bazel-cache: ruby_typecheck
bash:
bazel build //:protoc
cd ruby
cp typecheck.gemfile.lock Gemfile.lock
bundle exec rake steep
59 changes: 57 additions & 2 deletions protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def _PyOuts(srcs, use_grpc_plugin = False):
def _RubyOuts(srcs):
return [s[:-len(".proto")] + "_pb.rb" for s in srcs]

def _RBSOuts(srcs):
return [s[:-len(".proto")] + "_pb.rbs" for s in srcs]

def _CsharpOuts(srcs):
return [
"".join([token.capitalize() for token in src[:-len(".proto")].split("_")]) + ".cs"
Expand Down Expand Up @@ -158,6 +161,8 @@ def _proto_gen_impl(ctx):
outs.extend(_PyOuts([src.basename], use_grpc_plugin = use_grpc_plugin))
elif lang == "ruby":
outs.extend(_RubyOuts([src.basename]))
elif lang == "rbs":
outs.extend(_RBSOuts([src.basename]))

# Otherwise, rely on user-supplied outs.
args.append(("--%s_out=" + path_tpl) % (lang, gen_dir))
Expand Down Expand Up @@ -526,8 +531,6 @@ def internal_ruby_proto_library(
"""

# Note: we need to run the protoc build twice to get separate targets for
# the generated header and the source files.
_proto_gen(
name = name + "_genproto",
srcs = srcs,
Expand All @@ -552,6 +555,58 @@ def internal_ruby_proto_library(
**kwargs
)

def internal_rbs_proto_library(
name,
ruby_library,
srcs = [],
deps = [],
includes = ["."],
protoc = "@com_google_protobuf//:protoc",
testonly = None,
visibility = ["//visibility:public"],
**kwargs):
"""Bazel rule to create an RBS type definitions for Ruby from proto source files
NOTE: the rule is only an internal workaround to generate protos. The
interface may change and the rule may be removed when bazel has introduced
the native rule.
Args:
name: the name of the ruby_proto_library.
ruby_library: the ruby library rules to use.
srcs: the .proto files to compile.
deps: a list of dependency labels; must be a internal_rbs_proto_library.
includes: a string indicating the include path of the .proto files.
protoc: the label of the protocol compiler to generate the sources.
testonly: common rule attribute (see:
https://bazel.build/reference/be/common-definitions#common-attributes)
visibility: the visibility of the generated files.
**kwargs: other keyword arguments that are passed to ruby_library.
"""

_proto_gen(
name = name + "_genproto_rbs",
srcs = srcs,
deps = [s + "_genproto_rbs" for s in deps],
langs = ["rbs"],
includes = includes,
protoc = protoc,
testonly = testonly,
visibility = visibility,
tags = ["manual"],
)

ruby_library(
name = name,
srcs = [name + "_genproto_rbs"],
deps = [],
testonly = testonly,
visibility = visibility,
includes = includes,
**kwargs
)

# When canonical labels are in use, use additional "@" prefix
_canonical_label_prefix = "@" if str(Label("//:protoc")).startswith("@@") else ""

Expand Down
3 changes: 3 additions & 0 deletions src/file_lists.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ set(libprotoc_srcs
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/helpers.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/pyi_generator.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/retention.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/rbs_generator.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessor_case.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessors.cc
Expand Down Expand Up @@ -619,6 +620,7 @@ set(libprotoc_hdrs
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/helpers.h
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/pyi_generator.h
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/retention.h
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/rbs_generator.h
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator.h
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessor_case.h
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/rust/accessors/accessors.h
Expand Down Expand Up @@ -1395,6 +1397,7 @@ set(compiler_test_files
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/php/generator_unittest.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/plugin_unittest.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/retention_unittest.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/rbs_generator_unittest.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc
${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/versions_test.cc
)
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/compiler/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "google/protobuf/compiler/php/php_generator.h"
#include "google/protobuf/compiler/python/generator.h"
#include "google/protobuf/compiler/python/pyi_generator.h"
#include "google/protobuf/compiler/ruby/rbs_generator.h"
#include "google/protobuf/compiler/ruby/ruby_generator.h"
#include "google/protobuf/compiler/rust/generator.h"
#ifdef GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE
Expand Down Expand Up @@ -95,6 +96,10 @@ int ProtobufMain(int argc, char* argv[]) {
cli.RegisterGenerator("--ruby_out", "--ruby_opt", &rb_generator,
"Generate Ruby source file.");

ruby::RBSGenerator rbs_generator;
cli.RegisterGenerator("--rbs_out", "--rbs_opt", &rbs_generator,
"Generate RBS type definition.");

// CSharp
csharp::Generator csharp_generator;
cli.RegisterGenerator("--csharp_out", "--csharp_opt", &csharp_generator,
Expand Down
23 changes: 20 additions & 3 deletions src/google/protobuf/compiler/ruby/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ cc_library(

cc_library(
name = "ruby",
srcs = ["ruby_generator.cc"],
hdrs = ["ruby_generator.h"],
srcs = [
"rbs_generator.cc",
"ruby_generator.cc",
],
hdrs = [
"rbs_generator.h",
"ruby_generator.h",
],
copts = COPTS,
strip_include_prefix = "/src",
visibility = [
Expand All @@ -35,27 +41,37 @@ cc_library(
"//src/google/protobuf/compiler:retention",
"//src/google/protobuf/io",
"//src/google/protobuf/io:printer",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/container:flat_hash_set",
"@abseil-cpp//absl/log:absl_check",
"@abseil-cpp//absl/log:absl_log",
"@abseil-cpp//absl/strings",
],
)

cc_test(
name = "generator_unittest",
srcs = ["ruby_generator_unittest.cc"],
srcs = [
"rbs_generator_unittest.cc",
"ruby_generator_unittest.cc",
],
data = [
"ruby_generated_code.proto",
"ruby_generated_code_pb.rb",
"ruby_generated_code_pb.rbs",
"ruby_generated_code_proto2.proto",
"ruby_generated_code_proto2_import.proto",
"ruby_generated_code_proto2_pb.rb",
"ruby_generated_code_proto2_pb.rbs",
"ruby_generated_pkg_explicit.proto",
"ruby_generated_pkg_explicit_legacy.proto",
"ruby_generated_pkg_explicit_legacy_pb.rb",
"ruby_generated_pkg_explicit_legacy_pb.rbs",
"ruby_generated_pkg_explicit_pb.rb",
"ruby_generated_pkg_explicit_pb.rbs",
"ruby_generated_pkg_implicit.proto",
"ruby_generated_pkg_implicit_pb.rb",
"ruby_generated_pkg_implicit_pb.rbs",
"//src/google/protobuf:testdata",
],
deps = [
Expand All @@ -65,6 +81,7 @@ cc_test(
"//src/google/protobuf/io:printer",
"//src/google/protobuf/testing",
"//src/google/protobuf/testing:file",
"@abseil-cpp//absl/log:absl_check",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
Expand Down
Loading
Loading