Drizin / CodegenCS

C# Toolkit for Code Generation (T4 alternative!)
MIT License
223 stars 30 forks source link

Support for .net 8 #25

Closed kmuralidharDT closed 2 months ago

kmuralidharDT commented 2 months ago

Hi @Drizin ,

Just found CodegenCS interesting for code generation, however my application is built on .net 8 and I have tried to install dotnet-codegencs and then realized that it supports upto .net 7, is there a way to make it compatible with .net 8 or do you have a plan to do so.

Have a great day!

Drizin commented 2 months ago

Nice idea. Done. Published nugets as 3.4.0. Let me know how it goes.

kmuralidharDT commented 2 months ago

@Drizin ,

Thank you so much , will run some checks today and let you know

kmuralidharDT commented 2 months ago

Hi @Drizin ,

The solution works when i place all the models/enitites in a single file, however "using" based references from the same assembly are not picked up, in short i wanted to have my models reusable with different template.cs, is there a way to address this i think we can update the TemplateLauncher.cs and have all the assemblies loaded there (I did look at one of your suggestion in the issues), however is there are better way to address this problem, could you please provide your suggestion

Drizin commented 2 months ago

Can you elaborate?

Are you trying to use dotnet-codegencs tool? You are trying to run a CS template but it needs to use classes defined in other assemblies, correct?

kmuralidharDT commented 2 months ago

Yes that's correct I used dotnet codegen template run on template.cs referencing models defined in a separate class file within the assembly and it throws an error indicating reference missing, makes sense since it's run on a template.Cs and the reference to another file is not considered. My final objective is to have a single assembly which will contain templates and models defined, models may have a different namespace. The main method will call multiple methods, again the methods will be in different .cs files within the same assembly and will have code to generate .Cs files separately or even return a formattable string which main method will write to files (would prefer the methods to generate). I may ignore the model injection if auto injection for models have restriction of the number of models increase and have the json data read from files manually within the methods and have a dummy json to satisfy the template run command.

Drizin commented 2 months ago

@kmuralidharDT I'll close this issue (since the topic was .NET 8) and I invite you to join the open thread in #23 . If you modify TemplateLauncher.cs and add some reflection code (before "Invoking template" block) it should work.

I think this is the code I have tried once:

var baseFolder = new FileInfo(_entryPointClass.Assembly.Location).Directory;
var dlls = baseFolder.GetFiles("*.dll");
foreach (var dll in dlls)
{
    try
    {
        Assembly.LoadFrom(dll.FullName);
    }
    catch { }
}

I think the ideal solution is NOT loading all files in the folder (like above), but actually get a list of assemblies (something like /r:assembly1.dll /r:assembly2.dll etc) and get it through TemplateLauncherArgs. Feel free to create a PR if you get a working solution.