NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
117 stars 24 forks source link

Is NTypewriter.SourceGenerator intended to add rendered templates to compilation source? #74

Closed amdavie closed 1 year ago

amdavie commented 1 year ago

Hello @NeVeSpl

I just got around to looking at your SourceGenerator project from the last release as this is something I would be very interested in because I mainly write C# templates.

I realize this was the initial release, but looking at the source code for the SourceGenerator it does not actually add any rendered nt templates to the compilation source (i.e. AddSource) but only renders them to file. Is this intentional? And if so, what is the purpose of using a C# source generator? Is adding to the compilation source something you are open to the project supporting? I suppose there would need to be a way to distinguish between an nt template that renders C# and ones that do not.

I'm just trying to understand the intention of this project since it is in fact using a C# source generator at its core.

Thanks.

gregveres commented 1 year ago

@amdavie I am not going to try to answer for @NeVeSpl, but I will say that there are very valid use cases for generating the TS files and not having them inserted into the project.

My project generates approximately 700 TS files (enums, interfaces and service entry points). I used to use Typewriter for this, which always inserts the files into the project and it would take forever, introducing 10-20 second lockups. When I migrated to NTypewriter, I changed things up so that these files get generated into the stand alone front end directory and not into a project directory. With this setup, NTypewriter is sub 1 second and everything works great.

I just wanted to point out that there are valid reasons for not adding the files to a project.

NeVeSpl commented 1 year ago

As @gregveres already mentioned the main job of NTypewriter is to generate TypeScript files.

SourceGenerator is the youngest addition to NTypewriter family, offering the bare minimum functionality.

Detecting C# files through extensions *.cs and adding them to compilation was planned from the beginning, but since generating C# is not a base scenario, it did not make it to the first release of SG. I would not mind if any PR appeared and added that functionality.

amdavie commented 1 year ago

@gregveres of course - I also generate some non-C# content

@NeVeSpl that is good to hear! I wanted to understand your vision for the SourceGenerator before I attempted my own tool, so I'm potentially interested in doing a PR. I'll need to take some time to familiarize myself with the repo, but it seems we'll need to make an assumption about which files get added to compilation without something explicit like an config option - so do you think if the template is outputting to a .cs file (e.g. Save output "MyClass.cs") then the generator would add it to source and not output the file or do we want something more explicit? Please share any thoughts on this feature as well.

NeVeSpl commented 1 year ago

For now, I do not think that anything more explicit is necessary, just a simple check of generated file extension would be enough.