A Visual Studio Code extension for compiling Protocol Buffers (.proto) files to gRPC code in multiple programming languages
Right-click on any .proto file in the VS Code explorer and select "Compile Proto for gRPC" to generate code in your desired language.
This extension allows you to easily compile .proto files to generate gRPC client and server code directly from the VS Code explorer context menu.
Simply right-click on any .proto file and select "Compile Proto for gRPC" to generate code in your desired language.
The Protocol Buffers compiler is required for all supported languages. Here's how to install it on different platforms:
bin
directory to
your PATH
Install using Homebrew:
brew install protobuf
Install using your package manager, for example on Ubuntu:
sudo apt-get install -y protobuf-compiler
Requirements for compiling Protocol Buffers to Go code:
Install using:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
Install using:
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
Your .proto file should include:
syntax = "proto3";
option go_package = "example.com/packagename"; // Define the Go package
package examplepackage; // Define the proto package
// Service definition
service ExampleService {
rpc ExampleMethod (ExampleRequest) returns (ExampleResponse);
}
// Message definitions
message ExampleRequest {
string input = 1;
}
message ExampleResponse {
string output = 1;
}
The compiler will generate:
*.pb.go
files containing message type definitions*_grpc.pb.go
files containing the client and server codeRequirements for compiling Protocol Buffers to Python code:
Install using:
pip install grpcio grpcio-tools
Your .proto file should include:
syntax = "proto3";
package examplepackage; // Define the proto package
// Service definition
service ExampleService {
rpc ExampleMethod (ExampleRequest) returns (ExampleResponse);
}
// Message definitions
message ExampleRequest {
string input = 1;
}
message ExampleResponse {
string output = 1;
}
The compiler will generate:
*_pb2.py
files containing message type definitions*_pb2_grpc.py
files containing the client and server codeRequirements for compiling Protocol Buffers to Java code:
For Maven (in pom.xml):
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.7</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.53.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.53.0</version>
</dependency>
For Gradle (in build.gradle):
dependencies {
implementation 'com.google.protobuf:protobuf-java:3.21.7'
implementation 'io.grpc:grpc-protobuf:1.53.0'
implementation 'io.grpc:grpc-stub:1.53.0'
}
Your .proto file should include:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "ExampleProto";
package examplepackage; // Define the proto package
// Service definition
service ExampleService {
rpc ExampleMethod (ExampleRequest) returns (ExampleResponse);
}
// Message definitions
message ExampleRequest {
string input = 1;
}
message ExampleResponse {
string output = 1;
}
The compiler will generate:
Requirements for compiling Protocol Buffers to Ruby code:
Install using:
gem install grpc grpc-tools
Your .proto file should include:
syntax = "proto3";
package examplepackage; // Define the proto package
// Service definition
service ExampleService {
rpc ExampleMethod (ExampleRequest) returns (ExampleResponse);
}
// Message definitions
message ExampleRequest {
string input = 1;
}
message ExampleResponse {
string output = 1;
}
The Ruby compiler generates the following files:
lib
directory in the same location as your .proto
file
The generated files follow Ruby naming conventions, converting snake_case.proto files to snake_case Ruby files.
Requirements for compiling Protocol Buffers to Dart code:
Install using:
dart pub global activate protoc_plugin
Make sure ~/.pub-cache/bin
is in your PATH
Your .proto file should include:
syntax = "proto3";
package examplepackage; // Define the proto package
// Service definition
service ExampleService {
rpc ExampleMethod (ExampleRequest) returns (ExampleResponse);
}
// Message definitions
message ExampleRequest {
string input = 1;
}
message ExampleResponse {
string output = 1;
}
The Dart compiler generates the following files:
lib/src/generated
directory structure in the same
location as your .proto file
The generated files follow Dart naming conventions, converting snake_case.proto files to snake_case.dart files.