C#, .NET, How can I make a code generator download when my NuGet package is installed?

Haziel Melchor 0 Reputation points
2025-06-14T19:13:45.1366667+00:00

csproj, Engine: <Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<TargetFramework>net8.0</TargetFramework>

<ImplicitUsings>enable</ImplicitUsings>

<Nullable>enable</Nullable>

<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

<Title>JamEngine</Title>

<Authors>3l4z1o_</Authors>

<Description>Un motor pequeño, pero potente para hacer juegos de jam, de manera fácil, rápida, simple y ligera.</Description>

<Version>1.1.0</Version>

<IncludeBuildOutput>true</IncludeBuildOutput>

</PropertyGroup>

<ItemGroup>

<PackageReference Include="Engine_Window" Version="1.0.0" />

</ItemGroup>

<ItemGroup>

<PackageReference Include="Engine_SourceGenerator" Version="1.0.0">

  <IncludeAssets>all</IncludeAssets>

  <ExcludeAssets>none</ExcludeAssets>

</PackageReference>

</ItemGroup>

</Project>
csproj, Engine_SourceGenerator: <Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<TargetFramework>netstandard2.0</TargetFramework>

<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

<Title>EngineSourceGenerator</Title>

<Authors>3l4z1o</Authors>

<Description>Generador de codigo para JamEngine</Description>

<PackageType>Analyzer</PackageType>

<IncludeBuildOutput>false</IncludeBuildOutput>

<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>

<IsRoslynComponent>true</IsRoslynComponent>

</PropertyGroup>

<ItemGroup>

<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.14.0" PrivateAssets="all" />

</ItemGroup>

<ItemGroup>

<None Include="bin\$(Configuration)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" />

</ItemGroup>

</Project>
Target: When someone downloads my Engine.dll library through NuGet, for example: <ItemGroup><PackageReference Include="Engine" Version="1.1.0"/></ItemGroup>a code generator should be downloaded that takes care of creating the entry point with a couple of modifications: context.RegisterSourceOutput(context.CompilationProvider, (ctx, compilation) =>{var programSource = @"using Engine;using System;public static class Program{public static void Main(string[] args){EngineInitializer.Initialize();}}";ctx.AddSource("Program.g.cs", SourceText.From(programSource, Encoding.UTF8));});but I want to avoid having the developer install the 2 packages separately. Is it possible to achieve this?

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 245 Reputation points Microsoft External Staff
    2025-08-11T12:04:02.9833333+00:00

    Use a Single NuGet Packag 

    Instead of creating two separate NuGet packages (Engine.dll and Engine_SourceGenerator), you can bundle the source generator into the main package by including its DLL as an analyzer.

    This way, when someone installs your Engine package, the source generator automatically works.

    Modify Your .csproj of the Main Project

    Add the source generator DLL to the Analyzers section inside your main project .csproj file

    xml

    Copy code

    <ItemGroup>

        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all" />

    </ItemGroup>

    <ItemGroup>

        <None Include="path\to\Engine_SourceGenerator.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

    </ItemGroup>

    PackagePath="analyzers/dotnet/cs" is the special folder where analyzers and source generators must go.

     

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.