Search code examples
c#.net-corenugetauto-generate

Create a .NET Core Nuget package to autogenerate c# classes


I have a .NET Framework project (A) which generates an exe which takes html file as input and outputs an autogenerated C# class file (like a template of sorts). The .csproj of A looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <AssemblyName>ProjectA</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
    <None Include="ProjectA.exe.config">
  <ItemGroup>
</Project>

In project B, we're using the exe like this: (part of csproj of project B)

<Target Name="_EX_MyGenerateFile_cs" Inputs="MyHtml.html" Outputs="$(O)\MyGeneratedFile.cs" Condition="'$(BuildingOutOfProcess)' != 'False' AND '$(BuildingProject)' == 'True'" BeforeTargets="BeforeCompile">
    <Exec WorkingDirectory="$(MSBuildProjectDirectory)" LogStandardErrorAsError="true" Command="<path to projectA exe> MyHtml.html $(O)\MyGeneratedFile.cs" />
    <Exec WorkingDirectory="$(MSBuildProjectDirectory)" LogStandardErrorAsError="true" Command="if errorlevel 1 echo $(ERR_MSG) Unable to compile MyHtml.html to $(O)\MyGeneratedFile.cs" />
</Target>

Now, I wish to create a Nuget package (.NetCore) that achieves similar functionality as projectA (i.e somehow be able to consume this new package in a different netcore application and autogenerate the files- in a similar way projectB is able to use ProjectA's exe to generate the files). As I understand, .NET Core project would create a dll (not an exe); what is the way of using this dll to achieve what I want?

Any help is appreciated!


Solution

  • As .NET Core is a cross platform framework. This DLL file works across all platforms that are supported by the .NET Core runtime (Windows, Linux, and macOS). This is known as "framework dependent" deployment.

    For your case there are 2 options :

    1. To run any .dll of .NET Core application , command dotnet yourProject.dll is used.

    Part of projectB.csproj will look like this:

    <Target Name="_EX_MyGenerateFile_cs" Inputs="MyHtml.html" Outputs="$(O)\MyGeneratedFile.cs" Condition="'$(BuildingOutOfProcess)' != 'False' AND '$(BuildingProject)' == 'True'" BeforeTargets="BeforeCompile">
        <Exec WorkingDirectory="$(MSBuildProjectDirectory)" LogStandardErrorAsError="true" Command="dotnet <path to projectA dll> MyHtml.html $(O)\MyGeneratedFile.cs" />
        <Exec WorkingDirectory="$(MSBuildProjectDirectory)" LogStandardErrorAsError="true" Command="if errorlevel 1 echo $(ERR_MSG) Unable to compile MyHtml.html to $(O)\MyGeneratedFile.cs" />
    </Target>
    
    1. If you really want exe file, generate exe file using a Self-Contained deployment.

    This will create an output that contains its own copy of the .NET Core runtime and an yourProject.exe file.
    Con of this approach:

    1. It increases the size of the published application.
    2. It needs to be updated when new versions of the runtime are released.
    3. The resulting application only works on the published operating system published.