Knapcode.ProtoRebuilder
0.4.0
Prefix Reserved
dotnet tool install --global Knapcode.ProtoRebuilder --version 0.4.0
dotnet new tool-manifest
dotnet tool install --local Knapcode.ProtoRebuilder --version 0.4.0
#tool dotnet:?package=Knapcode.ProtoRebuilder&version=0.4.0
nuke :add-package Knapcode.ProtoRebuilder --version 0.4.0
ProtoRebuilder
Generate .proto files from .NET assemblies.
This is a .NET tool that scans .NET assemblies for types that implement IMessage
and generates corresponding .proto
files.
The idea is that you used protoc (either directly or via Grpc.Tools) to generate some C# from .proto files. This was compiled into an assembly. Then you lost the .proto files! What do you do? You can generate .proto files from the assembly using this tool.
Maybe in the future, I will enhance the tool to also generate .proto files from any group of .NET types (perhaps as a way to transition from one wire format to another), but for now, it only works with assemblies containing Google.Protobuf.IMessage
types.
Usage
Install the .NET SDK from https://get.dot.net/. This allows you to run .NET tool packages easily.
With .NET SDK 10+, you can use dnx
:
dnx Knapcode.ProtoRebuilder path/to/assembly.dll dir/for/protos
With .NET SDK 8+, you can use dotnet tool install
:
dotnet tool install --global Knapcode.ProtoRebuilder
Then run the tool with:
protorebuilder path/to/assembly.dll dir/for/protos
This will scan the assembly (first argument) for types that implement Google.Protobuf.IMessage
and generate .proto
files in the specified directory (second argument). If the IMessage
types are in different namespaces, multiple proto files will be generated, one for each namespace. Only enums used by the IMessage
types will be included in the generated .proto
files.
Take a look at the test data for examples of the input and output .proto
files. Here is a sample:
Caveats
This is not a perfect round-trip (proto → .NET assembly → proto). Here is a list of problems I know about:
- Enum names may change. The values (which are what go over the wire) should stay the same.
- The well-known type
StringValue
is assumed to be astring
(no wrapper). This can cause deserialization issues because the string value is actually wrapped in a message with a singlevalue
field, but the generated .proto files only know about the inner string (no wrapper).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.