Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#8.0

Boilerplate Guide to Creating a Source Generator - Part 5

0.00/5 (No votes)
18 Jan 2024MIT1 min read 4.5K  
A series of 6 articles to provide you with a boilerplate guide to create Source Generators.
This tutorial outlines the process of packaging a Roslyn source generator into a NuGet Package, including setup steps in the project file, specifying package information, and creating the package, emphasizing the importance of including specific sections and generating the package in Release mode.

Table of Contents

Packaging the Source Generator into a NuGet Package

There are plenty of videos and articles out there on how to create a NuGet Package, so I will not really describe it here but will provide the basics of what is needed and the few specific changes that are necessary.

Setup

Add the following two sections of code to the Source Generator project file.

XML
<PropertyGroup>
  <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  <IncludeBuildOutput>false</IncludeBuildOutput>
  <Version>1.0.0</Version>
  <Title>Test Data Mother Object Generator</Title>
  <Authors>David Elliott</Authors>
  <Company>Webbert Solutions, LLC</Company>
  <Description>A source code generator for creating mother objects for use in creating test code</Description>
</PropertyGroup>
XML
<ItemGroup>
  <!-- Place the generator in the analyzer directory of the NuGet package -->
  <None Include="$(OutputPath)\$(AssemblyName).dll"
        Pack="true"
        PackagePath="analyzers/dotnet/cs"
        Visible="false" />
</ItemGroup>

The first just provides general information about the NuGet package and is really unimportant with the exception of the IncludeBuildOutput. This just says that the generator DLL should not be included in your project output.

The second provides information as to the fact that this is an analyzer and where to place it within the NuGet package, so when imported, it can be handled appropriately.

Create the NuGet Package

All you need to do is, build the generator in Release mode.

This will create bin -> Release -> RandomTestDataGenerator.1.0.0.nupkg

History

  • 18th January, 2024: Initial version

License

This article, along with any associated source code and files, is licensed under The MIT License