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 25 forks source link

error while generating typescript services from ntypewriter runtime generator #118

Closed YoussefUrwave closed 5 months ago

YoussefUrwave commented 5 months ago

Hello I wanted to invoke my typewriter generator but from C# instead of visual studio which begins like that (.nt) file :

{{ for endpoint in data.Classes | Symbols.WhereNameEndsWith "Endpoint" 
capture controllerOutput -}}

and output the ts files like that:

{{ end; Save controllerOutput "test/" + (endpoint.BareName | String.Remove "Endpoint") + "Service.ts" ; end }}

I cannot use source generators for my case as I want to invoke on runtime so I followed the guide of build your own cli here: https://github.com/NeVeSpl/NTypewriter/blob/master/Documentation/NTypewriter.md#Build-your-own-CLI

the guide points to DocumentationGenerator/Program.cs in order to generate a simple documentation I wanted to generate the ts file the nt file has all the capabilities just wanted to invoke from c# like visual studio but from code so I have done that code but it doesn't render the result after NTypeWriter.Render(template, codeModel) method : code :

 public async Task<string> WriteEndpointServices()
 {
     AnalyzerManager manager = new AnalyzerManager();
     IProjectAnalyzer analyzer = manager.GetProject(@"..\..\CodeGenerator\CodeGenerator\CodeGenerator.csproj");
     AdhocWorkspace workspace = analyzer.GetWorkspace(false);
     var project = workspace.CurrentSolution.Projects.Where(x => x.Name == "CodeGenerator").First();

     // 2) Add xml documentation
     project = AddXmlDocumentation(project, typeof(ICodeModel));
     project = AddXmlDocumentation(project, typeof(ActionFunctions));
     project = AddXmlDocumentation(project, typeof(Scriban.Functions.StringFunctions));
     project = AddXmlDocumentation(project, typeof(Scriban.Functions.BuiltinFunctions));
     project = AddXmlDocumentation(project, typeof(Scriban.Functions.ArrayFunctions));
     project = AddXmlDocumentation(project, typeof(Scriban.Functions.RegexFunctions));

     // 3) Compile
     var compilation = await project.GetCompilationAsync();

     // 4) Create CodeModel
     var codeModelConfiguration = new CodeModelConfiguration() { OmitSymbolsFromReferencedAssemblies = false };
     var codeModel = new CodeModel(compilation, codeModelConfiguration);

     // 5) Load template
     string template = System.IO.File.ReadAllText(@"FastEndointsServiceTemplate.nt");

     // 6) Add custom functions
     var ntypewriterConfig = new Configuration();

     // 7) Render
     var result = await NTypeWriter.Render(template, codeModel);

     if (!result.HasErrors)
     {
         var renderedItem = result.Items.First();
         var path = Path.Combine(@"..\..\..\", renderedItem.Name);
         System.IO.File.WriteAllText(path, renderedItem.Content);
     }
     else
     {
         foreach (var msg in result.Messages)
         {
             Console.WriteLine(msg.Message);
         }
     }
     return "";
 }

error:

**{<input>(23,29) Value cannot be null. (Parameter 'input')}** 

I don't know what input mean here ? isn't the typewriter should read the project by its own and output the result?

NeVeSpl commented 5 months ago

You have not attached the full code of your template (*.nt), but I assume that the problem is in line 23 around the 29th character.

YoussefUrwave commented 5 months ago

Thank You @NeVeSpl the problem has been solved