Skip to content

Enable NuGet package MsBuild integration #4805

@kkm000

Description

@kkm000

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.

  1. Note hardcoded paths to tools (lines 6 and 7). This is easy to obtain by including some well-scripted .target and/or .prop files into NuGet packages for the corresponding tools. The issue here (or a non-issue, may I hope) is that both Google.protobuf and Grpc.Tools must include them (On this note, Grpc also must declare an equal version dependency on Grpc.Tools or package the C# plugin with Grpc.Core and discard Grpc.Tools altogether. Either will do, but the plugin must be available for proto compilation).
  2. 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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions