I have a NuGet package with a .bond file. Users of my package can derive their Bond structs from the structs in my package's .bond file.
I want the user's Bond files to be compiled when they include my NuGet package. Today they must include my NuGet and the Bond.CSharp NuGet. But, my NuGet already has a reference to Bond.CSharp.
How can I author my package so that the consumers do not need to have their own <PackageReference Include="Bond.CSharp" ... />?
Bond codegen is run from the Bond.CSharp's build targets.
By default, the build targets of packages you consume do not flow to your consumers. The default value of a
PackageReference'sPrivateAssetsis "contentfiles;analyzers;build".You can override this behavior in your csproj's
PackageReference:I assume you are compiling the base struct into an assembly in your package. Bond codegen assumes that the generated code and the runtime library exactly match, so I've used an exact match version bound in the
PackageReference:[9.0.3]You said that you want your consumers to be able to derive from your Bond structs, so you'll probably also want to configure their
BondImportPathto include the .bond file inside your package. To do this, you need toBondImportPathto the package directory with said .bond files.The make sure the .bond files are included in the package, add something like this to your package's .csproj file:
This assumes that your .bond files live in a
bond\subdirectory.To automatically add something to
BondImportPath, you need to add a package .props file that will be automatically imported by consumers. Create a file namedExactNameOfPackage.propswith the following content:This .props file also needs to be packed. Add this to your project's .csproj file:
Now, the consumer can just use a
PackageReference. Any .bond files in their project will be compiled automatically, and they can useimport "you-file.bond"to refer to a .bond file in your package.Build assets do not flow transitively. The NuGet 5+ buildTransitive feature looks like it solves this, but I haven't experimented with it.
Here are the complete project files I used. The complete code is in my GitHub repository, export-bond-file-nuget.
lib-with-bond.csproj
LibWithBond.props
consume-lib.csproj
I was able to make this work for a
PackageReference. My initial experiments making it work forProjectReferencewere not successful, and I ran out of time to work more on this answer.