NeVeSpl / NTypewriter

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

Compile Output Path #68

Closed byte2pixel closed 1 year ago

byte2pixel commented 1 year ago

This isn't really an issue but more of a question.

I have a C# solution and when I build the solution all the necessary files are in the output path \bin\$(Configuration)...

I wrote a CLI using NTypewriter and NTypewriter.CodeModel.Roslyn this CLI takes some arguments like path to the .nt script(s) and path to the cspoj file(s) to render the script(s) against.

Everything works fine, the one issue I have is after running the CLI the assemblies that were in my \bin\$(Configuration) folder are all deleted.

My assumption is the project.GetCompilationAsync() is removing the ones that are there, new ones are generated by the CLI. Then when the CLI finishes up everything generated by it is then deleted.

I want to avoid this, by generating stuff in the CLI to some other output path so it doesn't interfere with stuff that was built outside of the CLI. I've been digging and digging but not found a way to get this to work. Is there a way or some other option for me?

Thanks! Mel

NeVeSpl commented 1 year ago

The problem is well known: daveaglick/Buildalyzer#105, unfortunately, I do no see any working solution.

byte2pixel commented 1 year ago

Thanks for the reply. I've looked through the comments there and there seemed to be some attempts at working around it. One of them, https://github.com/daveaglick/Buildalyzer/issues/105#issuecomment-516004918 seems to be the most promising.

However, I didn't have success with it.

byte2pixel commented 1 year ago

Actually, to follow up I was able to work around this! Using the link you provided and the comment I referenced. I had to remove the two environment options. I'm not sure why adding this argument works, but apparently it causes the "clean" to happen on the temp path instead so the dlls in my real OutputPath are left alone and not deleted! I guess I wouldn't say "Clean" because I removed the "Clean" targets in the options and it still was deleting files. Whatever this is doing though keeps Buildalyzer from deleting the files in my real OutputPath.

I used this to create my AdhocWorkspace:

private static AdhocWorkspace GetAdhocWorkspace(AnalyzerManager manager)
{
    var temp = Path.GetTempPath();
    var list = manager
        .Projects
        .Values
        .Select(p =>
        {
            //Set build/analysis environment options to prevent fighting with IDE, etc...
            var options = new EnvironmentOptions();
            options.Arguments.Add($"/p:OutputPath={Path.Combine(temp, "ba", "bin", p.ProjectGuid.ToString())}");
            return p.Build(options)
                .FirstOrDefault(); //You may not want to do this, may want to omit this and use SelectMany depending on your needs
        })
        .ToList();
    var adhocWorkspace = new AdhocWorkspace();
    foreach (AnalyzerResult analyzerResult in list)
        analyzerResult.AddToWorkspace(adhocWorkspace);
    return adhocWorkspace;
}
AdhocWorkspace workspace = GetAdhocWorkspace(manager);
foreach (var project in workspace.CurrentSolution.Projects)
{
    var compilation = await project.GetCompilationAsync();
   ....
}