NeVeSpl / NTypewriter

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

Sync/delete renamed/deleted files #62

Closed Xriuk closed 2 years ago

Xriuk commented 2 years ago

I'm not sure if I am missing something but it seems that renamed/deleted files stays in the output folder and are not deleted. So if I rename Class1 to Class2 I now have both Class1 and Class2 in files, instead of just Class2.

Do I need to setup something or is this not supported?

NeVeSpl commented 2 years ago

It depends on a project type, not all VS projects support that. Also option AddGeneratedFilesToVSProject must be set to true.

When it works, your template and generated files should have the same id: image

Xriuk commented 2 years ago

Would it be possible to have an option just to clean a folder before generating the files? This way no old files would be left inside. Because I have 2 projects at the moment: one in C# and the other one in TypeScript, and the first one generates the files for the second, so it makes little sense to include them in the first project.

gregveres commented 2 years ago

I have a similar setup and every once in a while I delete the files manually and regenerate. The problem with deleting the files every time is that you are going to cause a lot of recompilation in your typescript project because all of the generated files will be brand new. One of the great features of NTypewriter is that it doesn't write files when they have not changed.

The ony way that I could see implementing something to solve this problem is to do something like this:

This has a huge caveat that you would need to generate the files into an empty directory because any file that isn't NTypewriter generated is going to get deleted.

NeVeSpl commented 2 years ago

There is no such thing as "output" folder in NTypewriter. Every generated file has it is own path associated.

As a quick solution I propose to use a custom function:

public static void Clean()
{
    System.IO.DirectoryInfo di = new DirectoryInfo(@"C:\\your_path");
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete();
    }
}

and invoke it somewhere from the template, outside of any loop

{{ Custom.Clean
   capture output
       for class in data.Classes 
           class.FullName | String.Append "\r\n"
      end
   end
   Save output "index.txt"
}}  

There is only one caveat to this solution, the Clean function will be invoked during rendering preview too ....

Xriuk commented 2 years ago

Could it be possible to add a flag to distinguish preview from actual generation?

NeVeSpl commented 2 years ago

Sure, v0.3.8 will have env.IsPreviewRender flag.