-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Enable NuGet package MsBuild integration #4805
Description
It is easy to include proto compile step into MsBuild build process, bit it is hard to get it right. Is there an interest in adding this support so it is available out of the box? I'd be happy to contribute, but it has to involve both grpc and protobuf projects.
I am currently including the following targets file from my projects:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CompileDependsOn>ProtoCompile;$(CompileDependsOn)</CompileDependsOn>
</PropertyGroup>
<PropertyGroup>
<ProtoCompilerPath>..\..\ext\usr\bin\</ProtoCompilerPath>
<ProtoCsPluginPath>..\..\ext\usr\bin\</ProtoCsPluginPath>
</PropertyGroup>
<Target Name="ProtoCompile" Condition=" '@(ProtoBuf)' != '' " Inputs="@(ProtoFiles)"
Outputs="$(IntermediateOutputPath)Acme.cs;$(IntermediateOutputPath)AcmeGrpc.cs">
<PropertyGroup>
<ProtoCCommand>$(ProtoCompilerPath)protoc --csharp_out=$(ProjectDir)$(IntermediateOutputPath) --grpc_out=$(ProjectDir)$(IntermediateOutputPath) --plugin=protoc-gen-grpc=$(ProtoCsPluginPath)grpc_csharp_plugin.exe -I ..\..\src\acmeserver @(ProtoBuf)</ProtoCCommand>
</PropertyGroup>
<Message Importance="high" Text="$(ProtoCCommand)" />
<Exec Command="$(ProtoCCommand)" />
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)Acme.cs;$(IntermediateOutputPath)AcmeGrpc.cs" />
<FileWrites Include="$(IntermediateOutputPath)Acme.cs;$(IntermediateOutputPath)AcmeGrpc.cs" />
</ItemGroup>
</Target>
</Project>This compiles acme.proto and adds generated files to C# compilation (and to future cleanup via FileWrites, adding to hermeticity). Since ProtoCompile target is declared dependency on Compile (via the CompileDependsOn property), and Compile is one of the targets invoked by VS on project load, compilation happens even when VS opens the project, and therefore IntelliSense is available for generated files immediately. The generated files are not shown in the project tree, which is also much better.
There are a few problems here though that I do not really know how to approach.
- Note hardcoded paths to tools (lines 6 and 7). This is easy to obtain by including some well-scripted
.targetand/or.propfiles into NuGet packages for the corresponding tools. The issue here (or a non-issue, may I hope) is that bothGoogle.protobufandGrpc.Toolsmust include them (On this note,Grpcalso must declare an equal version dependency onGrpc.Toolsor package the C# plugin withGrpc.Coreand discardGrpc.Toolsaltogether. Either will do, but the plugin must be available for proto compilation). - Figuring out names of generated files from compiled
.protos. (Also note the hardcoded names of generated files). I kinda see a pattern, but an official word from project owners would be helpful :)
I would try to do as much as I can with this, including C++ integration as well, and I used to write quite complex build scripts (including coded MsBuild extensions, called Tasks); but this kind of cross-project coordination might be beyond my capabilities. Any implementation will be half-baked from either vantage point.
I am an ex-Google SWE, by the way--just for a fair mention of the fact.