Coder Social home page Coder Social logo

Comments (2)

apm1grb avatar apm1grb commented on June 12, 2024

This need is based on sharing .proto files via NuGet packages accordingly to the described approach in SO: Sharing one gRPC proto file for all solutions

from paket.

apm1grb avatar apm1grb commented on June 12, 2024

To workaround this missing capability, these (similar) approaches are helpful.

Using a package .targets file

Using a <package ID>.targets as part of the package, located in a build folder on the package root level.

This file should have content like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target
      Name="CopyProtobufs"
      BeforeTargets="BeforeBuild">
        <CreateItem
          Include="$(MSBuildThisFileDirectory)..\content\Protos\**\*.*">
            <Output TaskParameter="Include" ItemName="ProtoFiles" /> 
        </CreateItem>

        <Copy
          SourceFiles="@(ProtoFiles)"
          DestinationFolder="$(ProjectDir)\include\%(RecursiveDir)"
          SkipUnchangedFiles="true"
          OverwriteReadOnlyFiles="true"
        />

        <WriteLinesToFile
          File="$(ProjectDir)\include\.gitignore"
          Lines=".gitignore;foundation/"
          Overwrite="true"
          Encoding="UTF-8"
        />
    </Target>
</Project>

Such a package-related .targets file is automatically weaved into the build process of a package consumer.

This approach copies the (common) .proto files to the project folder of the consuming project. In case this project is Git versioned, it makes sense to suppress the tracking of such imported files. That's the aim of the 2nd step - create dynamically a .gitignore file in the root folder of the imported stuff which ignores the .gitignore file itself and the folder with .proto files.

So the consuming project can import the (common) .proto files that way:

<ItemGroup>
    <Protobuf Include="Protos/**/*.proto" ProtoRoot="Protos" AdditionalImportDirs="include/" />
</ItemGroup>

Using a package .props file

Using a <package ID>.props as part of the package, located in a build folder on the package root level.

This file should have content like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <PkgMessages_Protobuf>$(MSBuildThisFileDirectory)/../content/Protos/</PkgMessages_Protobuf>
  </PropertyGroup>
</Project>

Again, such a package-related .props file is automatically weaved into the build process of a package consumer.

It creates an MSBuild property named PkgMessages_Protobuf which points to the folder of the .props files. Later this property can get evaluated in the build process of the package consumer.

So the consuming project can import the (common) .proto files that way:

<ItemGroup>
    <Protobuf Include="Protos/**/*.proto" ProtoRoot="Protos" AdditionalImportDirs="$(PkgMessages_Protobuf)" />
</ItemGroup>

Remarks

In both cases, these files must be part of the package. We keep them next to the .csproj file and the packaging is ensured via:

  <PropertyGroup Label="Protobuf asset root folders">
    <Assets_ProtoInclude>Protos/foundation/</Assets_ProtoInclude>
  </PropertyGroup>
  <ItemGroup Label="NuGet package Protobuf assets">
    <_ProtoAssetName Include="messages" />
    <_Asset PackagePath="content/Protos/foundation/" Include="@(_ProtoAssetName->'$(Assets_ProtoInclude)%(Identity).proto')" />
    <None Include="@(_Asset)" Pack="true" Visible="false" />
  </ItemGroup>

  <ItemGroup Label="Targets package assets">
    <None Include="XYZ.Messages.Protobuf.props" PackagePath="build/XYZ.Messages.Protobuf.props" Pack="true" Visible="false" />
  </ItemGroup>

To add additional folders (e.g. with .proto files), add them comma-separated in the variable _ProtoAssetName (e.g. Include="messages, events, commands, queries".

from paket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.