Skip to content

Commit f3a4efc

Browse files
Tiago CardosoTiago Cardoso
authored andcommitted
ruby codegen: support generation of rbs files
this introduces support for a new protoc option, `--rbs_out`, which points to a directory where ruby type definition files, defined in the RBS format, are stored. [rbs](https://github.com/ruby/rbs) is the type signature syntax blessed by the ruby core team, used by static analysis tools such as [steep](https://github.com/soutaro/steep), which integrates with VS Code, the `irb` console (for features such as autocompletion). and [typeprof](https://github.com/ruby/typeprof). It relies on type definitions written into `.rbs` files. The `protobuf` library already exposes type definitions in [gem_rbs_collection](https://github.com/ruby/gem_rbs_collection/tree/main/gems/google-protobuf/3.22), which is used to source type definitions for libraries which do not want, or can't maintain type definitions themselves. (`protobuf` could arguably import these into this repository, lmk if you're interested). This should fix gaps such as better IDE integration, such as the ones described [here](#9495). The plan is to do roughly the same type of integration as was done for `.pyi` annotations, which also write to separate files: add the cli option and the rbs generator. protobuf classes in ruby rely on dynamic attribution of the base class, which makes what I'm trying to achieve a bit difficult. Ideally the type hierarchy could be specified statically in the ruby source code. ```ruby class Bar < AbstractMessage class Bar < Google::Protobuf::DescriptorPool.generated_pool.lookup("Bar").msgclass ``` ```
1 parent 92382b3 commit f3a4efc

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

src/google/protobuf/compiler/main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "google/protobuf/compiler/python/generator.h"
1717
#include "google/protobuf/compiler/python/pyi_generator.h"
1818
#include "google/protobuf/compiler/ruby/ruby_generator.h"
19+
#include "google/protobuf/compiler/ruby/rbs_generator.h"
1920
#include "google/protobuf/compiler/rust/generator.h"
2021
#include "upb_generator/minitable/generator.h"
2122

@@ -93,6 +94,11 @@ int ProtobufMain(int argc, char* argv[]) {
9394
cli.RegisterGenerator("--ruby_out", "--ruby_opt", &rb_generator,
9495
"Generate Ruby source file.");
9596

97+
// Ruby
98+
ruby::RbsGenerator rbs_generator;
99+
cli.RegisterGenerator("--rbs_out", &rb_generator,
100+
"Generate Ruby rbs stub.");
101+
96102
// CSharp
97103
csharp::Generator csharp_generator;
98104
cli.RegisterGenerator("--csharp_out", "--csharp_opt", &csharp_generator,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
#include "google/protobuf/compiler/ruby/rbs_generator.h"
9+
10+
#include <string>
11+
#include <utility>
12+
#include <vector>
13+
14+
#include "absl/container/flat_hash_set.h"
15+
#include "absl/log/absl_check.h"
16+
#include "absl/log/absl_log.h"
17+
#include "absl/strings/ascii.h"
18+
#include "absl/strings/match.h"
19+
#include "absl/strings/str_split.h"
20+
#include "absl/strings/string_view.h"
21+
#include "google/protobuf/compiler/code_generator.h"
22+
#include "google/protobuf/descriptor.h"
23+
#include "google/protobuf/descriptor.pb.h"
24+
#include "google/protobuf/io/printer.h"
25+
#include "google/protobuf/io/zero_copy_stream.h"
26+
27+
namespace google {
28+
namespace protobuf {
29+
namespace compiler {
30+
namespace ruby {
31+
32+
bool RbsGenerator::Generate(const FileDescriptor* file,
33+
const std::string& parameter,
34+
GeneratorContext* context,
35+
std::string* error) const {
36+
37+
return true;
38+
}
39+
40+
} // namespace ruby
41+
} // namespace compiler
42+
} // namespace protobuf
43+
} // namespace google
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
// Author: jieluo@google.com (Jie Luo)
9+
//
10+
// Generates Ruby type definition (.rbs) for a given .proto file.
11+
12+
#ifndef GOOGLE_PROTOBUF_COMPILER_RUBY_RBS_GENERATOR_H__
13+
#define GOOGLE_PROTOBUF_COMPILER_RUBY_RBS_GENERATOR_H__
14+
15+
#include <string>
16+
17+
#include "google/protobuf/compiler/code_generator.h"
18+
19+
#include "google/protobuf/port_def.inc"
20+
21+
namespace google {
22+
namespace protobuf {
23+
namespace compiler {
24+
namespace ruby {
25+
26+
class PROTOC_EXPORT RbsGenerator : public CodeGenerator {
27+
public:
28+
RbsGenerator();
29+
RbsGenerator(const RbsGenerator&) = delete;
30+
RbsGenerator& operator=(const RbsGenerator&) = delete;
31+
~RbsGenerator() override;
32+
33+
// CodeGenerator methods.
34+
uint64_t GetSupportedFeatures() const override {
35+
return FEATURE_PROTO3_OPTIONAL;
36+
}
37+
bool Generate(const FileDescriptor* file, const std::string& parameter,
38+
GeneratorContext* generator_context,
39+
std::string* error) const override;
40+
41+
};
42+
43+
} // namespace ruby
44+
} // namespace compiler
45+
} // namespace protobuf
46+
} // namespace google
47+
48+
#include "google/protobuf/port_undef.inc"
49+
50+
#endif // GOOGLE_PROTOBUF_COMPILER_RUBY_RBS_GENERATOR_H__

0 commit comments

Comments
 (0)