We use the Unchase Visual Studio extension to generate code from OpenAPI definitions with NSwag. We do this in multiple solutions and we have some requirements that are best solved by tweaking the NJsonSchema .liquid
templates used to generate C# code.
Since I don’t want various copies of the templates in different solutions across our repositories, I was hoping to add them to an internal NuGet package and use the templates in the projects that reference that NuGet package.
Reading this answer, I understand that for content items in a NuGet package, you want the files added as links to the consuming project. And then have them copied to the output folder upon building the Solution that consumes the NuGet package. However, I want the opposite. I want the files to be available (not as link!) in the Visual Studio Solution, because that is where I run Unchase to generate C# code. And preferably I don’t want the files to be copied upon build, they’ve served their purpose inside Visual Studio, I don’t need them deployed anywhere after that.
I’ve added the lib.props
file the above answer talks about as well as this answer, but the files are still added as links and NSwag can’t find them. Reading this Microsoft documentation about the .props
file it seems that this customizes how the Solution is build, so maybe that’s not where I customize how the NuGet files are handled?
Based on this answer about the nuspec file and the above answer about the build.props file I now have this in my nuspec:
<package>
<metadata>
<!-- omitted -->
<contentFiles>
<files include="**/*.liquid" buildAction="Content" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="NSwagTemplates*.liquid" target="contentFilesanyanyNSwagTemplates" />
<file src="NSwagTemplates*.liquid" target="contentNSwagTemplates" />
</files>
</package>
This in my .csproj
file:
<ItemGroup>
<None Include="NSwagTemplates*.*" Pack="true" PackagePath="NSwagTemplates" />
<None Include="Build*.*" Pack="true" PackagePath="build"></None>
</ItemGroup>
And this in my buildlib.props
file:
<Project>
<Target Name="CopyFiles" BeforeTargets="Build">
<ItemGroup>
<NSwagTemplates Include="$(MSBuildThisFileDirectory)..NSwagTemplates*.*" />
</ItemGroup>
<Copy SourceFiles="@(NSwagTemplates)" DestinationFolder="$(ProjectDir)NSwagTemplates" SkipUnchangedFiles="false"></Copy>
</Target>
</Project>
When updating the NuGet in the consuming Solution, building it and setting the TemplateDirectory in ConnectedService.json
to "..\..\NSwagTemplates"
, Unchase fails with this message:
Generating NSwag-file for OpenAPI (Swagger)…
NSwag-file “Client.nswag” for OpenAPI (Swagger) was generated.
Generating Client Proxy for OpenAPI (Swagger) Client…
Warning:Client Proxy for OpenAPI (Swagger) Client was not generated. Error: The template directory “C:PathToSolutionNSwagTemplates” does not exist.
If in stead I set "TemplateDirectory": "..\..\bin\Debug\net8.0\NSwagTemplates"
it will work, but this is suboptimal because it copies the templates to the build directory and deploys them.
What can I do to just have .liquid
files from a NuGet package available in my Visual Studio project?