Use code generation .liquid template files from NuGet in Visual Studio

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Use code generation .liquid template files from NuGet in Visual Studio

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật