C
C#•10mo ago
Byron

Nuget Package: Project Reference Mutli Runtime Targeting

Hey Folks, I have a nuget package that wraps an exe, which I need to run on both linux and windows. I was trying to set it up so when I build my nuget package it compiles the other project reference and generates two executables. Altought what I have works it seems a bit hacky. I was wondering if there was a cleaner way of going about this
ini
App.Tool: Dotnet cli tool
App.MSBuild: References App.Tool and should build it twice and be contained within the nuget package.
ini
App.Tool: Dotnet cli tool
App.MSBuild: References App.Tool and should build it twice and be contained within the nuget package.
<!--==== App.MSBuild =====-->

<!-- Include in generated Nuget Package -->
<Target Name="AddNugetContent">
<ItemGroup>
<TfmSpecificPackageFile Include="$(PublishDir)$(LinuxRuntime)\*.*">
<PackagePath>tool/bin/$(LinuxRuntime)/%(RecursiveDir)</PackagePath>
</TfmSpecificPackageFile>
<TfmSpecificPackageFile Include="$(PublishDir)$(WindowsRuntime)\*.*">
<PackagePath>tool/bin/$(WindowsRuntime)/%(RecursiveDir)</PackagePath>
</TfmSpecificPackageFile>
</ItemGroup>
</Target>
<!-- Double Publish -->
<Target Name="MultiRuntimeBuild" BeforeTargets="Build">
<Message Importance="high" Text="BaseOutputPath: $(BaseOutputPath)"/>
<MSBuild Projects="$(ToolProjectDirectory)App.Tool.csproj" Targets="Publish" Properties="RuntimeIdentifier=$(LinuxRuntime);SelfContained=true;TargetFramework=net6.0;PublishDir=$(PublishDir)$(LinuxRuntime)" />
<MSBuild Projects="$(ToolProjectDirectory)App.Tool.csproj" Targets="Publish" Properties="RuntimeIdentifier=$(WindowsRuntime);SelfContained=true;TargetFramework=net6.0;PublishDir=$(PublishDir)$(WindowsRuntime)" />
</Target>
<!--==== App.MSBuild =====-->

<!-- Include in generated Nuget Package -->
<Target Name="AddNugetContent">
<ItemGroup>
<TfmSpecificPackageFile Include="$(PublishDir)$(LinuxRuntime)\*.*">
<PackagePath>tool/bin/$(LinuxRuntime)/%(RecursiveDir)</PackagePath>
</TfmSpecificPackageFile>
<TfmSpecificPackageFile Include="$(PublishDir)$(WindowsRuntime)\*.*">
<PackagePath>tool/bin/$(WindowsRuntime)/%(RecursiveDir)</PackagePath>
</TfmSpecificPackageFile>
</ItemGroup>
</Target>
<!-- Double Publish -->
<Target Name="MultiRuntimeBuild" BeforeTargets="Build">
<Message Importance="high" Text="BaseOutputPath: $(BaseOutputPath)"/>
<MSBuild Projects="$(ToolProjectDirectory)App.Tool.csproj" Targets="Publish" Properties="RuntimeIdentifier=$(LinuxRuntime);SelfContained=true;TargetFramework=net6.0;PublishDir=$(PublishDir)$(LinuxRuntime)" />
<MSBuild Projects="$(ToolProjectDirectory)App.Tool.csproj" Targets="Publish" Properties="RuntimeIdentifier=$(WindowsRuntime);SelfContained=true;TargetFramework=net6.0;PublishDir=$(PublishDir)$(WindowsRuntime)" />
</Target>
6 Replies
reflectronic
reflectronic•10mo ago
i think this is the incorrect way of going about this what you want is a build task, not to put a .NET application in your nuget package. the build task lets you integrate whatever C# code you want into the build, and all you need to distribute for it is one DLL, not an entire .NET runtime per platform there is an example of writing a build task here https://learn.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-code-generation?view=vs-2022
Byron
ByronOP•10mo ago
I have written msbuild tasks before but we use the same CLI tools locally as well. So this gives us the best of both worlds. A simlar example is GitVersion.Tool which is pretty popular.
reflectronic
reflectronic•10mo ago
whatever the solution is, including two self-contained builds in your nuget package is not the correct one i would strongly prefer figuring out how to use it as a library, and if that doesn't work out for some reason, then i would just bundle the CLI application as a portable framework-dependent build (not self-contained) then you can run it as dotnet MyCli.dll, which will work on any platform, and it will not make your NuGet package huge this is what GitVersion.MsBuild appears to do
Byron
ByronOP•10mo ago
They bundle 4 versions inside their package haha. I can skip the self contained, that is a good point.
Byron
ByronOP•10mo ago
Each of the targets has one, I am unsure why they need so many.
No description
Byron
ByronOP•10mo ago
Anyway thanks for the pointers 🙂
Want results from more Discord servers?
Add your server