Add support for additional protoc arguments in Grpc.Tools (#25374)

Using "optional" presence tracking in proto3 (before protobuf 3.15)
required the `--experimental_allow_proto3_optional` protoc option
but there was no existing Grpc.Tools feature that would allow specifying
these arguments.

This commit adds an optional `Protobuf.AdditionalProtocArguments` option
that allows you to specify arbitrary protoc arguments. For example:

```
<Protobuf Include="**\*.proto" CompileOutputs="true" AdditionalProtocArguments="--experimental_allow_proto3_optional" />
```

Fixes #22975
reviewable/pr25361/r4
Jeff Moser 4 years ago committed by GitHub
parent d861ececfe
commit e46445cb5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/csharp/Grpc.Tools.Tests/ProtoCompileCommandLineGeneratorTest.cs
  2. 15
      src/csharp/Grpc.Tools/ProtoCompile.cs
  3. 1
      src/csharp/Grpc.Tools/build/_protobuf/Google.Protobuf.Tools.targets

@ -140,6 +140,15 @@ namespace Grpc.Tools.Tests
Does.Contain("--grpc_opt=baz,quux"));
}
[Test]
public void AdditionalProtocArguments()
{
_task.AdditionalProtocArguments = new[] { "--experimental_allow_proto3_optional" };
ExecuteExpectSuccess();
Assert.That(_task.LastResponseFile,
Does.Contain("--experimental_allow_proto3_optional"));
}
[Test]
public void DirectoryArgumentsSlashTrimmed()
{

@ -278,6 +278,12 @@ namespace Grpc.Tools
/// </summary>
public string[] OutputOptions { get; set; }
/// <summary>
/// Additional arguments that will be passed unmodified to protoc (and before any file names).
/// For example, "--experimental_allow_proto3_optional"
/// </summary>
public string[] AdditionalProtocArguments { get; set; }
/// <summary>
/// Full path to the gRPC plugin executable. If specified, gRPC generation
/// is enabled for the files.
@ -428,6 +434,15 @@ namespace Grpc.Tools
}
cmd.AddSwitchMaybe("dependency_out", DependencyOut);
cmd.AddSwitchMaybe("error_format", "msvs");
if (AdditionalProtocArguments != null)
{
foreach (var additionalProtocOption in AdditionalProtocArguments)
{
cmd.AddArg(additionalProtocOption);
}
}
foreach (var proto in Protobuf)
{
cmd.AddArg(proto.ItemSpec);

@ -288,6 +288,7 @@
GrpcPluginExe="%(_Protobuf_OutOfDateProto.GrpcPluginExe)"
GrpcOutputDir="%(_Protobuf_OutOfDateProto.GrpcOutputDir)"
GrpcOutputOptions="%(_Protobuf_OutOfDateProto._GrpcOutputOptions)"
AdditionalProtocArguments="%(_Protobuf_OutOfDateProto.AdditionalProtocArguments)"
>
<Output TaskParameter="GeneratedFiles" ItemName="_Protobuf_GeneratedFiles"/>
</ProtoCompile>

Loading…
Cancel
Save